0

这是我的脚本

   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。

4

1 回答 1

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

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

于 2013-08-06T07:51:32.387 回答