我正在使用 Rollup 功能,我几乎达到了我所需要的完美点,只需要删除一个小东西。
declare @FromDate datetime
declare @ToDate datetime
declare @StoreNumber varchar(10)
declare @AllStores varchar(1)
set @FromDate = '1/1/12'
set @ToDate = '1/1/13'
set @StoreNumber = '01'
set @AllStores = 'y'
If @AllStores = 'Y'
Begin
select
CASE WHEN (GROUPING(s.StoreID) = 1) THEN 'All Stores' ELSE ISNULL(s.StoreID, 'UNKNOWN') END as 'Store #',
CASE WHEN (GROUPING(b.currencydesc) = 1) THEN 'Store #' + s.StoreID + ' TOTAL' ELSE ISNULL(b.currencydesc, 'UNKNOWN') END as 'Tender Type', SUM(amount) as Total
from RPTrs s
join rpPay p on s.storeid = p.StoreID and s.ReceiptNO = p.ReceiptNo
join Currencies b on b.POSCurrency = LEFT(p.paytype,1)
where trsdate between @FromDate and @ToDate
group by s.storeid, b.currencydesc with rollup
End
Else
Begin
select
s.StoreID as 'Store #',
CASE WHEN (GROUPING(b.currencydesc) = 1) THEN 'Store #' + s.StoreID + ' TOTAL' ELSE ISNULL(b.currencydesc, 'UNKNOWN') END as 'Tender Type', SUM(amount) as Total
from RPTrs s
join rpPay p on s.StoreID = p.StoreID and s.ReceiptNO = p.ReceiptNo
join Currencies b on b.POSCurrency = LEFT(p.paytype,1)
where trsdate between @FromDate and @ToDate
and s.StoreID = @StoreNumber
group by grouping sets((s.StoreID),(s.StoreID, b.CurrencyDesc))
End
有了这个输出。
Store # Tender Type Total
01 Amex 250.00
01 C.Card 3138.00
01 Cash 53553.17
01 Gift Card 1595.35
01 MasterCard 813.10
01 Off Line C.Card 247.53
01 Str Cr -544.45
01 Visa/MC 437.35
01 Store #01 TOTAL 59490.05
02 Cash 238.15
02 Store #02 TOTAL 238.15
All Stores NULL 59728.20
如何使所有商店中的 NULL 总计消失?