一个表有名叫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语句``````请高手帮个忙
两种.我要统计在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语句``````请高手帮个忙
type num money
1 10 100
2 10 200
3 10 300
4 10 400
最好拿最早先的表结构和数据看看,是不是A,B是从最早先那个表里面出来的?
(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
不知行不!
...
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
......
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
想不出好一点的办法,只能用这个了
不知道行不行
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 试试,应该符合你的要求