我希望5月到6月之间的货币汇率变动与以捷克克朗(捷克克朗)借入的贷款总额相对于欧元表示不同。 例如:由于汇率变动,5月份人们从我公司借来的款项为27M捷克克朗(相当于1M欧元),由于货币汇率的变动,6月仅为0.89M欧元。
我来了:
SELECT
SUM((
SELECT Rate
FROM s96.CurrencyRates
WHERE (YEAR(Date)=2017) AND (MONTH(Date)=5)
) * t.ApprovedLoanAmount)
-
SUM((
SELECT Rate
FROM s96.CurrencyRates
WHERE (YEAR(Date)=2017) AND (MONTH(Date)=6)
) * t.ApprovedLoanAmount)
AS currChange
FROM(
SELECT ApprovedLoanAmount
FROM dim.Contract con
JOIN dim.Calendar cal ON con.CreateDateID = cal.DateId
WHERE (cal.CalendarYear = 2017) AND (cal.MonthOfYear = 5)
) t
返回:
无法对包含聚合或子查询的表达式执行聚合函数。
任何帮助都会受到重视。
答案 0 :(得分:1)
更改查询的选项很少。 您可以计算子查询中的当前速率,并在1 = 1时使用左连接。 您还可以将总和语句更改为:
SELECT
((
SELECT SUM(Rate)
FROM s96.CurrencyRates
WHERE (YEAR(Date)=2017) AND (MONTH(Date)=5)
) * t.ApprovedLoanAmount)
-
((
SELECT SUM(Rate)
FROM s96.CurrencyRates
WHERE (YEAR(Date)=2017) AND (MONTH(Date)=6)
) * t.ApprovedLoanAmount)
AS currChange
FROM(
SELECT ApprovedLoanAmount
FROM dim.Contract con
JOIN dim.Calendar cal ON con.CreateDateID = cal.DateId
WHERE (cal.CalendarYear = 2017) AND (cal.MonthOfYear = 5)
) t
答案 1 :(得分:1)
我不知道你的确切表格如何,所以想想这个更像是一个片段而不是复制/粘贴解决方案:
SELECT
SUM(ApprovedLoanAmount) AS ApprovedLoanAmountCZK
, SUM(ApprovedLoanAmountEUR_May) AS ApprovedLoanAmountEUR_May
, SUM(ApprovedLoanAmountEUR_June) AS ApprovedLoanAmountEUR_June
, SUM(ApprovedLoanAmountEUR_May) - SUM(ApprovedLoanAmountEUR_June) AS CurrChange
FROM
(
SELECT ApprovedLoanAmount
, ApprovedLoanAmount * (SELECT Rate
FROM s96.CurrencyRates rM
WHERE rM.Date = cal.Date -- date of contract?
AND rM.Currency = 'EUR' -- if you have other currencies rates in same table
) AS ApprovedLoanAmountEUR_May
, ApprovedLoanAmount * (SELECT Rate
FROM s96.CurrencyRates rJ
WHERE rJ.Date = '20170630'
AND rJ.Currency = 'EUR'
) AS ApprovedLoanAmountEUR_June
FROM dim.Contract con
JOIN dim.Calendar cal ON con.CreateDateID = cal.DateId
WHERE (cal.CalendarYear = 2017) AND (cal.MonthOfYear = 5)
) t