一个表有名叫table里面有id ,type,title,money 四个字段.现在我要做一条统计语句...只能用一条语句,type里面有1,2,3,4四个值.select [type],count(id) as num,sum(money) as money from table group by type order by type 这样我就可以把table表下按照type分类来统计了....现在我要实现的是这个问题....在type值等2的时候.title表里面会有两个值"A"和"B"
两种.我要统计在type等于2的时候下面的"A"与"B"的id总数与money总数.而且这两条语句放排在type=2后面.紧跟着后面是type=3的记录.
type  num  money 
1     10    100
2     10    200
3     10    300
4     10    400我要做出的效果是
type num money
1     10  100
2     10  200
2     5   100   (这为title为A的记录)
2     5   100   (这为title为B的记录)
3     10  300
4     10  400 
  请问这个问题可以实现吗?...只能用一条SQL语句``````请高手帮个忙

解决方案 »

  1.   

    这个表是经过你第一次select出来的吧?
    type  num  money 
    1     10    100
    2     10    200
    3     10    300
    4     10    400
    最好拿最早先的表结构和数据看看,是不是A,B是从最早先那个表里面出来的?
      

  2.   

    是的.....这是我第一次统计出来的..但是现在要加入type=2类而且title为a与title为b的统计``而且要同时放在一个记录集中```用一条sql语句`
      

  3.   

    select * from
    (select type,count(id) as num,sum(money) as money from table group by type order by type  union all
    (select type,count(id) as num,sun(money) as money from table group by type,title) t
    order by type
    不知行不!
      

  4.   

    我只想要type=2里面所有总数与type=a与type=b的总数显示出来
      

  5.   

    加個條件
    ...
    select type,count(id) as num,sun(money) as money from table group by type,title where type=2 and title in ('a','b')) t
    ......
      

  6.   

    select [type],count(id) as num,sum(money) as money from table where type=1 group by type order by type 
    union
    select [type],count(id) as num,sum(money) as money from table where type=2 group by type,title order by type
    union
    select [type],count(id) as num,sum(money) as money from table where type in ('3','4') group by type order by type 
    想不出好一点的办法,只能用这个了
    不知道行不行
      

  7.   

    (select testtype,count(testid) as num,sum(money) as money from testtable group by testtype)
    union 
    (select testtype,count(testid) as num,sum(money) as money from testtable group by testtype,title )
    order by testtype,num desc
    这个语句你把testid 改成 id ,testtype 改成type 试试,应该符合你的要求