2013-08-06 52 views
0

這是我的腳本如何將輸出結果更改爲零的sql腳本?

SELECT a.iPriority_num AS [TransNUM],b.sItem_nm as [Item],a.mSellingPrice_amt AS [aMT], 
     SUM(c.mCash_amt) AS [CASH] 
     ,SUM(c.mCCard_amt) AS [CC] 
     ,SUM(c.mEPS_amt) AS [CEPS] 
     ,SUM(c.mGCheque_amt) AS [GC] 
     ,SUM(c.mCheque_amt) AS [OTHERS] 
    FROM Table1 a 
    LEFT JOIN Table2 b 
    ON a.sPlu_cd=b.sPlu_cd 
    LEFT JOIN Table3 c 
    ON a.iPriority_num=c.iPriority_num AND 
    a.iBranch_num=c.iBranch_num 
    AND b.iCompany_typ=c.iCompany_typ 
    WHERE a.IAPPLY_DT BETWEEN 20130701 AND 20130731 
    AND a.iBranch_num=14 
    AND a.iStatus_typ=1 
    AND a.sSellUnit_CD='PC' 
    AND a.IPRIORITY_NUM=118271 
    GROUP BY 
    a.iPriority_num,b.sItem_nm,a.mSellingPrice_amt 

結果是

TransNUM Item Price  Cash  CC  EPS GC  others   
    118271 Item1 204.0000 392.0000 .0000 .0000 .0000 .0000 
    118271 Item2 188.0000 392.0000 .0000 .0000 .0000 .0000 

我想讓第二行(392)的值只有0。我在這裏使用SQL 2000。

+0

我很好奇。爲什麼你希望查詢輸出爲零,而不是在你的後臺修改它 –

+0

任何特定的RDBMS? –

+0

@RowlandShaw我相信這是MSSQL,因爲在查詢中有方括號 –

回答

0

該查詢會給你想要的結果:

SELECT a.iPriority_num AS [TransNUM],b.sItem_nm as [Item],a.mSellingPrice_amt AS [aMT], 
     [CASH] = CASE WHEN ROW_NUMBER() OVER 
        (ORDER BY a.iPriority_num,b.sItem_nm,a.mSellingPrice_amt) = 2 
       THEN 0 ELSE SUM(c.mCash_amt) END 
     ,SUM(c.mCCard_amt) AS [CC] 
     ,SUM(c.mEPS_amt) AS [CEPS] 
     ,SUM(c.mGCheque_amt) AS [GC] 
     ,SUM(c.mCheque_amt) AS [OTHERS] 
FROM Table1 a 
LEFT JOIN Table2 b 
ON a.sPlu_cd=b.sPlu_cd 
LEFT JOIN Table3 c 
ON a.iPriority_num=c.iPriority_num AND 
a.iBranch_num=c.iBranch_num 
AND b.iCompany_typ=c.iCompany_typ 
WHERE a.IAPPLY_DT BETWEEN 20130701 AND 20130731 
AND a.iBranch_num=14 
AND a.iStatus_typ=1 
AND a.sSellUnit_CD='PC' 
AND a.IPRIORITY_NUM=118271 
GROUP BY 
a.iPriority_num,b.sItem_nm,a.mSellingPrice_amt 

可是我最好還是要做到這一點的客戶端,而不是服務器端

+0

我得到錯誤! 'ROW_NUMBER'不是一個可識別的函數名稱,我在這裏使用mssql2000。 –

+0

哦,我錯過了!此解決方案適用於MSSQL 2005及更高版本 –