select sum(value) from table where tag in ('a','b','c','d' ) group by tag order by tag
select tag,sum(value) from #t where tag ='d' group by tag union all select tag,sum(value) from #t where tag ='b' group by tag union all select tag,sum(value) from #t where tag ='c' group by tag union all select tag,sum(value) from #t where tag ='a' group by tag
方法二:select tag, case when tag='d' then 1 when tag='b' then 2 when tag='c' then 3 when tag='a' then 4 end Sn,sum(value) from #t where tag in ('d','b','c','a' ) group by tag order by Sn
order by charindex(tag,'a,b,c,d')or 如果就是 a b c d e f g order by tag
order by charindex(tag,'a,b,c,d') --- 想法不错啊,小弟向干部你学习了。
union all
select tag,sum(value) from #t where tag ='b' group by tag
union all
select tag,sum(value) from #t where tag ='c' group by tag
union all
select tag,sum(value) from #t where tag ='a' group by tag
case
when tag='d' then 1
when tag='b' then 2
when tag='c' then 3
when tag='a' then 4
end Sn,sum(value) from #t where tag in ('d','b','c','a' ) group by tag order by Sn
如果就是 a b c d e f g
order by tag
---
想法不错啊,小弟向干部你学习了。