0

我正在尝试对列进行垂直和水平总计,请参阅以下查询,我能够进行垂直总计。

Select
  U_POD 'City',
  Sum([Customer TS Pending])  as    CTST ,
  sum([Pending])as P,
  sum([Pending SQ])as 'PSQ',
  sum([Tracking])as Tracking,
  SUM([Open])as Openn
from 
(
  Select T1.U_POD,T2.Name 
  from OSCL T0 
  inner join OCRD T1 
    on T0.Customer=T1.cardcode 
  inner join OSCS T2 
    on T2.statusID=T0.Status
)Service
Pivot
(
  count(Name)
  for Name IN ([Pending],[Pending SQ],[Tracking],[Open],[Customer TS Pending])
)pvt
group by rollup (pvt.U_POD)
4

1 回答 1

1

group by rollup将为您提供包含总计的最后一行,但如果您正在寻找包含总计的列,您可以在查询中添加一个新的计算列:

 sum([Customer TS Pending] + [Pending] + [Pending SQ] + [Tracking] + [Open]) Total

这将使完整的查询:

Select
  U_POD 'City',
  Sum([Customer TS Pending])  as    CTST ,
  sum([Pending])as P,
  sum([Pending SQ])as 'PSQ',
  sum([Tracking])as Tracking,
  SUM([Open])as Openn,
  sum([Customer TS Pending] + [Pending] + [Pending SQ] + [Tracking] + [Open]) Total
from 
(
  Select T1.U_POD,T2.Name 
  from OSCL T0 
  inner join OCRD T1 
    on T0.Customer=T1.cardcode 
  inner join OSCS T2 
    on T2.statusID=T0.Status
)Service
Pivot
(
  count(Name)
  for Name IN ([Pending],[Pending SQ],[Tracking],[Open],[Customer TS Pending])
)pvt
group by rollup (pvt.U_POD)
于 2013-05-30T23:32:42.460 回答