AOB
Registered User.
- Local time
- Today, 21:22
- Joined
- Sep 26, 2012
- Messages
- 633
Hi guys,
I have a table of transactions (close to 1m records) from which I want to query totals by currency and direction (ins and outs) and then also show the proportion of the overall total for each currency. All amounts are absolute (i.e. ins and outs both represented by positive numbers)
To throw some added complexity into the mix, the transaction table records only refer to account (AccountID), which links to a separate account and currency tables.
I can query the sum totals by currency and direction (although I'm not totally happy with the SQL syntax - if anybody can recommend a better way of doing this, I would be extremely grateful, it seems a bit clunky to me!)
I'm struggling to get the proportions per currency though? I tried adding :
But it tells me that the specified field 'tblTransactions.USDAmount' could refer to more than one table listed in the FROM clause of the SQL statement
Any suggestions?
Thanks!
Al
I have a table of transactions (close to 1m records) from which I want to query totals by currency and direction (ins and outs) and then also show the proportion of the overall total for each currency. All amounts are absolute (i.e. ins and outs both represented by positive numbers)
To throw some added complexity into the mix, the transaction table records only refer to account (AccountID), which links to a separate account and currency tables.
I can query the sum totals by currency and direction (although I'm not totally happy with the SQL syntax - if anybody can recommend a better way of doing this, I would be extremely grateful, it seems a bit clunky to me!)
Code:
SELECT tblCurrencies.CurrencyCode,
tmp1.SumPay,
tmp1.SumPayUSD,
tmp2.SumRec,
tmp2.SumRecUSD
FROM (tblCurrencies
INNER JOIN
(SELECT tblCurrencies.CurrencyCode,
Sum(tblTransactions.Amount) AS SumPay,
Sum(tblTransactions.USDAmount) AS SumPayUSD
FROM (tblTransactions
INNER JOIN tblAccounts
ON tblTransactions.AccountID = tblAccounts.AccountID)
INNER JOIN tblCurrencies
ON tblAccounts.CurrencyID = tblCurrencies.CurrencyID
WHERE tblTransactions.Direction = "Pay"
GROUP BY tblCurrencies.CurrencyCode) AS tmp1
ON tblCurrencies.CurrencyCode = tmp1.CurrencyCode)
INNER JOIN
(SELECT tblCurrencies.CurrencyCode,
Sum(tblTransactions.Amount) AS SumRec,
Sum(tblTransactions.USDAmount) AS SumRecUSD
FROM (tblTransactions
INNER JOIN tblAccounts
ON tblTransactions.AccountID = tblAccounts.AccountID)
INNER JOIN tblCurrencies
ON tblAccounts.CurrencyID = tblCurrencies.CurrencyID
WHERE tblTransactions.Direction = "Rec"
GROUP BY tblCurrencies.CurrencyCode) AS tmp2
ON tblCurrencies.CurrencyCode = tmp2.CurrencyCode
I'm struggling to get the proportions per currency though? I tried adding :
Code:
((SumPayUSD+SumRecUSD)/Sum(tblTransactions.USDAmount)) AS Proportion
But it tells me that the specified field 'tblTransactions.USDAmount' could refer to more than one table listed in the FROM clause of the SQL statement
Any suggestions?
Thanks!
Al