如何将输出结果更改为sql脚本的零?

时间:2013-08-06 07:45:18

标签: sql sql-server-2000

这是我的剧本

   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

我想将第2行(392)的值设为0。我在这里使用sql 2000。

1 个答案:

答案 0 :(得分: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

但我最好在客户端而不是服务器端执行此操作