表中数据如下,ag    y    countid
            23     3     701
            23     4     646
            23     5     734
            30     3     3355
            30     4     3325
            30     5     2980
我想要的查询结果是这样的           Y  23    30
           3  701  3355     
          4  646  3325
          5  734  2980请问如何写这个语句啊

解决方案 »

  1.   

    SELECT y,sum(iif(ag=23,countid,0)) as a23,
    sum(iif(ag=30,countid,0)) as a30
     from ttq group by y
      

  2.   

    select your_table.y,t1.countid,t2.countid from your_table,(select y,countid from your_table where ag=23) as t1,(select y,countid from your_table where ag=30) as t2 where your_table.y=t1.y and t1.y=t2.y group by your_table.y;
    试试吧,感觉挺复杂的,不能无限个ag
      

  3.   

    呵呵,我试试啊
    我的原表是这样的, id    date       age
    1   2008-4-3    22
    1  2008-5-1     33
    1   2008-6-1     50而我想要的结果是这样的
                23岁以下, 23-29岁 30-35岁  36-45 。
    2008年4月   count次数
    2008年5月
    2008年6月

    我通过这个语句select * from (select 23 as ag, month(date) as y,count(id)  from fdc.cc where age <=23 group by month(date)
    union all
    select 30 as ag, month(date) as y,count(id)  from fdc.cc where age >=23 and age<=30 group by month(date)) as t我这样写的也就只能返回表中数据如下,
                 ag    y    countid 
                23    3    701 
                23    4    646 
                23    5    734 
                30    3    3355 
                30    4    3325 
                30    5    2980 
    就是不知道前面的select 怎么写