获得每个项目的计算(美元货币转换)总计

时间:2017-07-26 02:41:34

标签: sql sql-server-2008 group-by sum aggregate-functions

我有一个查询来获取每个项目的总和,但是当我将SUM(d.AMT)更改为(SUM(d.AMT) * e.EXCHANGE_RATE) [USD Amount]以将总数转换为美元时,我会遇到错误:

  

列'EXCHANGE_RATE.EXCHANGE_RATE'在选择列表中无效   因为它不包含在聚合函数或   GROUP BY子句。

。有人可以解决我的问题吗?谢谢大家!

SELECT d.ITEM_NO, h.ITEM_TYPE, h.ITEM_STATUS, d.CURRENCY,SUM(d.AMT)--(SUM(d.AMT) * e.EXCHANGE_RATE) [USD Amount]
FROM ITEM_HDR h
JOIN ITEM_DET d ON h.ITEM_NO = d.ITEM_NO
JOIN EXCHANGE_RATE e ON d.CURRENCY = e.CURRENCY_FROM
GROUP BY d.ITEM_NO, h.ITEM_TYPE, h.ITEM_STATUS, d.CURRENCY

2 个答案:

答案 0 :(得分:1)

你需要在之前乘以

SELECT d.ITEM_NO, h.ITEM_TYPE, h.ITEM_STATUS, d.CURRENCY,
       SUM(d.AMT * e.EXCHANGE_RATE) as [USD Amount]
FROM ITEM_HDR h JOIN
     ITEM_DET d
     ON h.ITEM_NO = d.ITEM_NO JOIN
     EXCHANGE_RATE e 
     ON d.CURRENCY = e.CURRENCY_FROM
GROUP BY d.ITEM_NO, h.ITEM_TYPE, h.ITEM_STATUS, d.CURRENCY;

但是,如果您想为每个项目添加此项,请简化SELECTGROUP BY

SELECT d.ITEM_NO, h.ITEM_TYPE,
       SUM(d.AMT * e.EXCHANGE_RATE) as [USD Amount]
FROM ITEM_HDR h JOIN
     ITEM_DET d
     ON h.ITEM_NO = d.ITEM_NO JOIN
     EXCHANGE_RATE e 
     ON d.CURRENCY = e.CURRENCY_FROM
GROUP BY d.ITEM_NO, h.ITEM_TYPE;

答案 1 :(得分:1)

更改 (SUM(d.AMT) * e.EXCHANGE_RATE) [USD Amount]SUM(d.AMT * e.EXCHANGE_RATE) AS [USD Amount]