如何使用同一列中的

时间:2019-02-25 04:20:05

标签: mysql sql data-warehouse

表1列出了帐户信息

AccountTable

+--------------+----------------+----------+----------+
| Account_Name | Account_Number | Currency |   Date   |
+--------------+----------------+----------+----------+
| Test 1       |           1234 | USD      | 20190228 |
| Test 2       |          5678  | JPY      | 20190228 |
| Test 3       |           9012 | AUS      | 20190228 |
| Test 4       |           3456 | MEX      | 20190228 |
+--------------+----------------+----------+----------+

然后表2包含货币信息

货币表

CurrencyName|CurrencyRate|Date
USD         |50.9999     |20190228
KRW         |1.52422     |20190228
JPY         |11.2232     |20190228
MEX         |6.2323      |20190228
AUS         |5.1111      |20190228

以此类推...

我需要类似的输出

Account_Name|Account_Number|CurrencyName|USCurrency|Date
Test1       |1234          |US          |1.0000    |20190228
Test2       |5678          |JPY         |572.3820  |20190228
Test3       |9012          |AUS         |260.6655  |20190228
Test4       |3456          |MEX         |317.8466  |20190228

在USCurrency列上,我需要将另一种货币乘以/计算为美元汇率。我对加入两个表没有任何问题。我不知道如何计算同一列中的费率。谢谢!!

3 个答案:

答案 0 :(得分:1)

除了您的输出对我而言没有多大意义(请参阅下文中的更多内容),诀窍是使用两次 getMarkers: function() { let bounds = this.map.getBounds(); let southWest = bounds.getSouthWest(); let northEast = bounds.getNorthEast(); var from_lat = ""; if(southWest.lat() != null) { from_lat = southWest.lat()-0.05; } axios.get('/ajax', { params: { fromLat: from_lat, toLat: northEast.lat()-0.05, fromLng: southWest.lng()+0.05, toLng: northEast.lng()+0.05, } }).then((response) => { this.estates = response.data; this.updateMarkers(); }); },

CurrencyTable

您会在结果中注意到,行{SELECT Account_Name, Account_Number, A.Currency, C2.CurrencyRate * C1.CurrencyRate AS USCurrency, A.Date FROM AccountTable A JOIN CurrencyTable C1 ON A.Date = C1.Date AND A.Currency=C1.CurrencyName JOIN (SELECT * FROM CurrencyTable WHERE CurrencyName='USD') C2 ON A.Date = C2.Date 的{​​{1}}不会返回1。它需要将两个费率相互才能得出此结果,但由于某种原因,您正在要求的产品中提供所需的产品。

我相信您想要的是USCurrencyUSD,它们当然会更改所有记录。

答案 1 :(得分:0)

首先将美元存储在变量中,然后运行选择查询:

SET @US_CURRENCY := (SELECT CurrencyRate FROM CurrencyTable WHERE CurrencyName = 'USD');

SELECT AccountTable.Account_Name, 
AccountTable.Account_Number, 
CurrencyTable.CurrencyName,
IF( CurrencyTable.CurrencyName = 'USD', CurrencyTable.CurrencyRate/@US_CURRENCY, CurrencyTable.CurrencyRate*@US_CURRENCY ) AS USCurrency, 
AccountTable.Date
FROM AccountTable
LEFT JOIN CurrencyTable
ON AccountTable.Currency = CurrencyTable.CurrencyName;

答案 2 :(得分:0)

您需要在SQL中使用CASE语句来计算非美国货币的USCurrency。 选择 Account_Name,Account_Number,CurrencyName, 当CurrencyName ='JPY'时 THEN CurrencyTable.CurrencyRate *(从CurrencyTable中选择CurrencyRate,其中CurrencyName ='US') 当CurrencyName ='MEX' THEN CurrencyTable.CurrencyRate *(从CurrencyTable中选择CurrencyRate,其中CurrencyName ='US') 当CurrencyName ='AUS' THEN CurrencyTable.CurrencyRate *(从CurrencyTable中选择CurrencyRate,其中CurrencyName ='US') END US货币 日期 来自帐户表A 将货币表C1加入A.Date = C1.Date AND A.Currency = C1.CurrencyName