首页 新闻 赞助 找找看

SQL 语句问题

0
悬赏园豆:5 [已解决问题] 解决于 2013-09-05 10:26

有以下表 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

sql
风醉的主页 风醉 | 小虾三级 | 园豆:1197
提问于:2013-09-05 09:00
< >
分享
最佳答案
1
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

大概就是这么个意思。我这不方便验证。应该还可以优化。

收获园豆:5
Mr.Er | 菜鸟二级 |园豆:215 | 2013-09-05 10:06

thankyou ,经测试可用

我对也写了一个如下,测试执行结果一样,没测试执行效率:

 select  depID,Sum(unDoTh) as'age25’,Sum(dothing) as 'age>25'
 from (select depID,
      case
   when  age ='25then 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

 

风醉 | 园豆:1197 (小虾三级) | 2013-09-05 10:24
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册