sql 查询金额和汇率
sql quest with amount and exchange rate
考虑到汇率如何选择2018年12月大额付款的客户
我有一个 table:
- 交易日期 - 交易日期
- Transum numeric (20,2) - 支付金额
- CurrencyRate numeric (20,2) - 货币汇率
ID_Client Trandate Transum CurrencyRate Currency
--------------------------------------------------------
1 2018.12.01 100 1 UAH
1 2018.12.02 150 2 USD
2 2018.12.01 200 1 UAH
3 2018.12.01 250 3 EUR
3 2018.12.02 300 1 UAH
3 2018.12.03 350 2 USD
7 2019.01.08 600 1 UAH
但我认为"max"根本不是我需要的
SELECT ID_Client, MAX(Transum*CurrencyRate)
FROM `Payment.TotalPayments`
WHERE YEAR(Trandate) = 2018
AND MONTH(Trandate) = 12
我需要这个
ID_Client Transum
3 1750
其中1750是"UAH"和350USD + 300UAH + 250EUR,美元汇率为2,欧元汇率为3。
我想你想要 sum()
。然后就可以order by
结果:
SELECT ID_Client, SUM(Transum*CurrencyRate) as total
FROM `Payment.TotalPayments`
WHERE Trandate >= '2018-12-01' AND Transdate < '2019-01-01'
GROUP BY ID_Client
ORDER BY total DESC;
如果您要获取客户在 2018 年和 12 月份的交易金额总和,您可以这样写:
SELECT ID_Client, SUM(Transum*CurrencyRate) as payment_total_converted
FROM `Payment.TotalPayments`
WHERE YEAR(Trandate) = 2018
and MONTH(Trandate) = 12
group by ID_Client
如果您希望在给定日期范围内按每个客户、年份和月份分组,您可以这样写:
SELECT ID_Client, YEAR(Trandate) as tran_year, MONTH(Trandate) as tran_month,
SUM(Transum*CurrencyRate) as payment_total_converted
FROM `Payment.TotalPayments`
WHERE Trandate between '2018-12-01' and '2019-01-01'
group by ID_Client, YEAR(Trandate), MONTH(Trandate)
我为您的计算列添加了一个列名,这样结果集仍然是相关的(列需要不同的名称)。
我建议您阅读 SQL 'group by' 子句 (https://www.w3schools.com/sql/sql_groupby.asp) and aggregate (https://www.w3schools.com/sql/sql_count_avg_sum.asp, https://www.w3schools.com/sql/sql_min_max.asp) 运算符。
考虑到汇率如何选择2018年12月大额付款的客户
我有一个 table:
- 交易日期 - 交易日期
- Transum numeric (20,2) - 支付金额
- CurrencyRate numeric (20,2) - 货币汇率
ID_Client Trandate Transum CurrencyRate Currency
--------------------------------------------------------
1 2018.12.01 100 1 UAH
1 2018.12.02 150 2 USD
2 2018.12.01 200 1 UAH
3 2018.12.01 250 3 EUR
3 2018.12.02 300 1 UAH
3 2018.12.03 350 2 USD
7 2019.01.08 600 1 UAH
但我认为"max"根本不是我需要的
SELECT ID_Client, MAX(Transum*CurrencyRate)
FROM `Payment.TotalPayments`
WHERE YEAR(Trandate) = 2018
AND MONTH(Trandate) = 12
我需要这个
ID_Client Transum
3 1750
其中1750是"UAH"和350USD + 300UAH + 250EUR,美元汇率为2,欧元汇率为3。
我想你想要 sum()
。然后就可以order by
结果:
SELECT ID_Client, SUM(Transum*CurrencyRate) as total
FROM `Payment.TotalPayments`
WHERE Trandate >= '2018-12-01' AND Transdate < '2019-01-01'
GROUP BY ID_Client
ORDER BY total DESC;
如果您要获取客户在 2018 年和 12 月份的交易金额总和,您可以这样写:
SELECT ID_Client, SUM(Transum*CurrencyRate) as payment_total_converted
FROM `Payment.TotalPayments`
WHERE YEAR(Trandate) = 2018
and MONTH(Trandate) = 12
group by ID_Client
如果您希望在给定日期范围内按每个客户、年份和月份分组,您可以这样写:
SELECT ID_Client, YEAR(Trandate) as tran_year, MONTH(Trandate) as tran_month,
SUM(Transum*CurrencyRate) as payment_total_converted
FROM `Payment.TotalPayments`
WHERE Trandate between '2018-12-01' and '2019-01-01'
group by ID_Client, YEAR(Trandate), MONTH(Trandate)
我为您的计算列添加了一个列名,这样结果集仍然是相关的(列需要不同的名称)。
我建议您阅读 SQL 'group by' 子句 (https://www.w3schools.com/sql/sql_groupby.asp) and aggregate (https://www.w3schools.com/sql/sql_count_avg_sum.asp, https://www.w3schools.com/sql/sql_min_max.asp) 运算符。