1

私はロールアップ機能を使用していますが、必要なものがほぼ完璧になりました。小さなものを1つ削除するだけです。

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を合計で非表示にするにはどうすればよいですか?

4

2 に答える 2

1

これを行う1つの方法は、クエリをaでラップしてから、値を置き換える列でSELECT使用することです。COALESCEnull

SELECT [store #], 
       COALESCE([tender type], '') [Tender Type], 
       total 
FROM   
(
    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
) src 

COALESCE()または、サブクエリなしで列をラップすることができます。

SELECT 
    CASE 
        WHEN ( Grouping(s.storeid) = 1 ) 
        THEN 'All Stores' 
        ELSE Isnull(s.storeid, 'UNKNOWN') 
    END               AS [Store #], 
    COALESCE(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 
于 2013-02-04T15:57:13.247 に答える
0

あなたの問題は次の行にあるようです:

CASE WHEN (GROUPING(b.currencydesc) = 1) THEN 'Store #' + s.StoreID + ' TOTAL' ELSE ISNULL(b.currencydesc, 'UNKNOWN') END as [Tender Type], 

このことを考慮:

SELECT '1' + NULL 

これはNULLを返します。代わりにCOALESCEを使用してください。

SELECT COALESCE('1' + NULL,'') 

これは''を返します。上記のシナリオでは、StoreIdはNULLだと思います。完全なコードは次のとおりです。

CASE WHEN (GROUPING(b.currencydesc) = 1) THEN COALESCE('Store #' + s.StoreID + ' TOTAL','') ELSE ISNULL(b.currencydesc, 'UNKNOWN') END as [Tender Type], 

幸運を。

于 2013-02-04T16:05:47.937 に答える