2015-07-10 37 views
-5

我有一个像如何编写SQL

ID  USER_ID type 
1   1   2 
2   1   1 
3   3   3 
4   3   1 
5   6   2 
6   6   3 

一个表,我想所有的USER_ID = 1之和,和USER_ID = 3 和USER_ID = 1每种类型的总和USER_ID = 3,它可以是两个SQL 结果是

sum 
4 

type sum 
1 2 
2 1 
3 1 
+0

我相信你已经知道了。但是你必须发布迄今为止已经尝试过的SQL。 – sstan

回答

0

你需要达到的结果第一个功能是Count()SUM()功能

对于第一个结果

select count(*) as sum from user1 where user_id in(1,3) 

对于第二个结果,你需要选择类型也和它应归

select type, count(*) as sum from user1 where user_id in(1,3) 
    group by type 

Fiddle for second你可以通过查询

接下来检查的第一个也没有时间发布你的努力。猜你是天真的SQL。

+0

谢谢我试图编写复杂的SQL如下:select test.user_scale_result a test_user_scale_result select d.remote_scale_name,d.remote_scale_id,e.remote_scale_id,e.scaleCount test_scale d inner join(SELECT count(a.remote_scale_id)as scaleCount,a.remote_scale_id FROM test_user_scale_result a 内部联接( )SELECT user_id FROM consult.en_org_user_department d内部联接(从user_id = 21的en_org_user_department选择department_id)f在d.department_id = f.department_id )b在a.user_id = b.user_id group by a.remote_scale_id)e在d.remote_scale_id = e.remote_scale_id; –

+0

ok..next时间包括什么你试过的问题,即使它给出了错误或不符合逻辑正确否则我们会认为你只是要求一个答案与努力 – Sachu

+0

好吧,我会粘贴我的尝试。 –