有以下表 TableA:
depID useName Age
1 TOMA 25
1 TOMB 26
1 TOMF 26
2 TOMC 26
3 TOMD 27
3 TOME 27
现在我想得到以下结果 怎么写SQL 语句啊 () 谢谢!
depID Age25 Age>25
1 1 2
2 0 1
3 1 2
select depId,(select count(*) from tablea t1 where t1.age==25 and t1.depId==t.depId )age25, (select count(*) from tablea t1 where t1.age>25 and t1.depId==t.depId )age>25 from tablea t group by depId
大概就是这么个意思。我这不方便验证。应该还可以优化。
thankyou ,经测试可用
我对也写了一个如下,测试执行结果一样,没测试执行效率:
select depID,Sum(unDoTh) as'age25’,Sum(dothing) as 'age>25' from (select depID, case when age ='25‘ then 1 else 0 end as unDoTh, case when instance_task_state >25 then 1 else 0 end as dothing from tablea) as tablea_Result Group by depID