如何在MSSQL中对列值求和

时间:2018-07-09 05:33:39

标签: sql-server sql-server-2014

我是mssql的新手,在这里我需要对列值求和。

但是在我的情况下,表之间有一些连接,最后我有一列带有两行输出。

我想做的是求和查询的最终输出。

这是我的查询:

 SELECT 
               SUM(ESCD.ITEM_QTY) * ((SELECT COLOC_PROD_PRICE
               FROM LOM_LNK_PROD_COMP 
               WHERE  COLOC_PROD_CODE = ITEM_ID)
               /
             ((SELECT LMUL.UOL_CONV_QTY 
               FROM LOM_MST_UOM_LINK AS LMUL

               JOIN LOM_MST_PRODUCT AS LMP 
               ON LMUL.UOL_MAIN_UOM_CODE = LMP.PROD_STOCK_UOM
               AND LMP.PROD_CODE = ESCD.ITEM_ID)/LMUL.UOL_CONV_QTY )) AS 'TOTAL_AMOUNT'



               FROM EC_SHOPPING_CART_DETAIL AS ESCD

               JOIN LOM_MST_UOM_LINK AS LMUL
               ON LMUL.UOL_MAIN_UOM_CODE = ITEM_PACK_SIZE

               WHERE CREATED_BY = 'xyz'
               AND  CHECK_OUT = 'FALSE'
               GROUP BY ITEM_ID,LMUL.UOL_CONV_QTY

输出:

enter image description here

预期AS:

enter image description here

谁能帮我解决这个问题。

1 个答案:

答案 0 :(得分:1)

尝试以下操作:

SELECT
  SUM(X.TOTAL_AMOUNT) AS 'TOTAL_AMOUNT'
FROM
(
 SELECT 
           SUM(ESCD.ITEM_QTY) * ((SELECT COLOC_PROD_PRICE
           FROM LOM_LNK_PROD_COMP 
           WHERE  COLOC_PROD_CODE = ITEM_ID)
           /
         ((SELECT LMUL.UOL_CONV_QTY 
           FROM LOM_MST_UOM_LINK AS LMUL

           JOIN LOM_MST_PRODUCT AS LMP 
           ON LMUL.UOL_MAIN_UOM_CODE = LMP.PROD_STOCK_UOM
           AND LMP.PROD_CODE = ESCD.ITEM_ID)/LMUL.UOL_CONV_QTY )) AS 'TOTAL_AMOUNT'



           FROM EC_SHOPPING_CART_DETAIL AS ESCD

           JOIN LOM_MST_UOM_LINK AS LMUL
           ON LMUL.UOL_MAIN_UOM_CODE = ITEM_PACK_SIZE

           WHERE CREATED_BY = 'xyz'
           AND  CHECK_OUT = 'FALSE'
           GROUP BY ITEM_ID,LMUL.UOL_CONV_QTY
) X;