使用sql汇总,你有更好的方法嘛?(0分)

  • 主题发起人 主题发起人 yvtong
  • 开始时间 开始时间
Y

yvtong

Unregistered / Unconfirmed
GUEST, unregistred user!
使用rollup汇总不向compute by 可以带非分组字段,我改造如下,不知道是否还有其他
方法
--一个使用T-Sql实现汇总的例子,带原始数据
use northwind
select orderid,customerid,employeeid, year(orderdate) as "year" ,month(orderdate) as "Month",day(orderdate) as "day",sum(freight) as freight ,
grouping(customerid)+grouping(orderid) + grouping(year(orderdate))+grouping(month(orderdate))+grouping(day(orderdate))+grouping(employeeid) as grouped
into #TempOrders
from orders
group by year(orderdate) ,month(orderdate) ,day(orderdate),customerid,freight,orderid,employeeid
with rollup

select
convert(char(5),customerid) as customerid ,
convert (char(2),employeeid)as employeeid ,
convert(char(6),orderid) as orderid,
convert(char(4), "year") as "year" ,
convert(char(4), "month") as "month" ,
convert(char(4), "day") as "day" ,
freight ,grouped
Into #TempOrders2
from #TempOrders
where grouped =0 or grouped>2

update #temporders2
set orderid = '' ,customerid='',employeeid='',"year" = '' , "month" ='' ,"day" ='日计' where grouped= 3
update #temporders2
set orderid = '' ,customerid='',employeeid='',"year" = '' , "month" ='月计' ,"day" = '' where grouped= 4
update #temporders2
set orderid = '' , customerid='',employeeid='',"year"='年计' , "month" ='' ,"day" ='' where grouped= 5
update #temporders2
set orderid = '' ,customerid='',employeeid='',"year"='总计' , "month" ='' ,"day" ='' where grouped= 6


select * from #temporders2
 
我的报表都是用DevExpressQuantumGrid3.2.1来做,多快好省
 
后退
顶部