# sql按照总和进行按降和按升序排序?

0
[已解决问题] 解决于 2008-09-26 10:55

Item    YearMonth Num

Item   200801  200802  200803  200804  200805  200806  200807 200808  200809 200810  200811  200812

----

select Item,
sum(case YearMonth when '200801' then Num else 0 end) as '200801',
sum(case YearMonth when '200802' then Num else 0 end) as '200802',
sum(case YearMonth when '200803' then Num else 0 end) as '200803',
sum(case YearMonth when '200804' then Num else 0 end) as '200804',
sum(case YearMonth when '200805' then Num else 0 end) as '200805',
sum(case YearMonth when '200806' then Num else 0 end) as '200806',
sum(case YearMonth when '200807' then Num else 0 end) as '200807',
sum(case YearMonth when '200808' then Num else 0 end) as '200808',
sum(case YearMonth when '200809' then Num else 0 end) as '200809',
sum(case YearMonth when '200810' then Num else 0 end) as '200810',
sum(case YearMonth when '200811' then Num else 0 end) as '200811',
sum(case YearMonth when '200812' then Num else 0 end) as '200812'
from T_TEST group by Item

itman020 | 初学一级 | 园豆：0

0

Item_Time　　　　Number

……

Distinct_Item_Time　 SumNumber

……

0

select Item,
sum(case YearMonth when '200801' then Num else 0 end) as '200801',
sum(case YearMonth when '200802' then Num else 0 end) as '200802',
sum(case YearMonth when '200803' then Num else 0 end) as '200803',
sum(case YearMonth when '200804' then Num else 0 end) as '200804',
sum(case YearMonth when '200805' then Num else 0 end) as '200805',
sum(case YearMonth when '200806' then Num else 0 end) as '200806',
sum(case YearMonth when '200807' then Num else 0 end) as '200807',
sum(case YearMonth when '200808' then Num else 0 end) as '200808',
sum(case YearMonth when '200809' then Num else 0 end) as '200809',
sum(case YearMonth when '200810' then Num else 0 end) as '200810',
sum(case YearMonth when '200811' then Num else 0 end) as '200811',
sum(case YearMonth when '200812' then Num else 0 end) as '200812',
SUM(Num) as 'Total'
from T_TEST group by Item order by Total desc

您需要登录以后才能回答，未注册用户请先注册