选择json值该匹配列ID
我的数据是付款,金额,货币和JSON对象,该对象具有该特定日期的汇率。该JSON包含所有货币和所有汇率。
金额 | 货币 | 汇率 |
---|---|---|
666.00 | USD | [{“ basecurrency”:“ sek”,“ ExchangeCurrency”:“ GBP”,“费率”:0.07848,“日期”:2018-10-21},{“ Basecrency” “ ExchangeCurrency”:“ USD”,“ RATE”:0.106593,“日期”:2018-10-21] |
123.00 | GBP | [{“ basecurrency”:“ sek”,“ offerceNageCurrency”:“ fivernangecurrency”:“ “日期”:2020-03-10},{“ basecurrency”:“ sek”,“ fiffangecurrency”:“ usd”,“速率”:0.117549,“日期”:2020-03-10} |
]每行的每个对象中的正确速率?最终目标是将一列称为“ sek中的数量”。
我希望结果看起来像这样:
金额 | 货币 | 汇率 | 汇率 | 美元 |
---|---|---|---|---|
666.00 | [{{“ basecurrency”:“ sek”,“ fiffingangecurrency”:“ GBP | ”,“速率”:0.07848,“日期”:2018-- “ usd”,“速率”:0.106593,“日期”:2018-10-21}] | 0.106593 | 6248.07 |
123.00 | 10-21},{“ basecurrency”:“ sek”,“ fiffangecurrency ” | : sek“,“ fiffangecurrency”:“ GBP”,“速率”:0.08239,“日期”:2020-03-10},{“ basecurrency”:“ sek”,“ sek'',“ fivferangecurrency”:“ usd”:“ usd”,“ rate”,“ rate”:0.117549 ,“日期”:2020-03-10}] | 0.08239 | 1492.90 |
My data are of payments, amount, currency and a JSON object with the exchange rates for that particular date. This JSON contains ALL currencies and all exchange rates.
Amount | Currency | Exchange rates |
---|---|---|
666.00 | USD | [{"BaseCurrency":"SEK","ExchangeCurrency":"GBP","Rate":0.07848,"Date":2018-10-21},{"BaseCurrency":"SEK","ExchangeCurrency":"USD","Rate":0.106593,"Date":2018-10-21] |
123.00 | GBP | [{"BaseCurrency":"SEK","ExchangeCurrency":"GBP","Rate":0.08239,"Date":2020-03-10},{"BaseCurrency":"SEK","ExchangeCurrency":"USD","Rate":0.117549,"Date":2020-03-10}] |
How do I select just the correct rate in each object for each row? The final goal is to have one column called "Amount in SEK".
I want a result that looks something like this:
Amount | Currency | Exchange rates | Rate | Amount in SEK |
---|---|---|---|---|
666.00 | USD | [{"BaseCurrency":"SEK","ExchangeCurrency":"GBP","Rate":0.07848,"Date":2018-10-21},{"BaseCurrency":"SEK","ExchangeCurrency":"USD","Rate":0.106593,"Date":2018-10-21}] | 0.106593 | 6248.07 |
123.00 | GBP | [{"BaseCurrency":"SEK","ExchangeCurrency":"GBP","Rate":0.08239,"Date":2020-03-10},{"BaseCurrency":"SEK","ExchangeCurrency":"USD","Rate":0.117549,"Date":2020-03-10}] | 0.08239 | 1492.90 |
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果使用SQL Server 2016+,则可以使用
OpenJson()
来解析JSON数据。以下语句是解决问题的可能解决方案(不舍入):示例数据:
statment:
结果:
If you use SQL Server 2016+, you may parse the JSON data with
OPENJSON()
. The following statement is a possible solution to your problem (without rounding):Sample data:
Statment:
Result:
谢谢,这似乎是我想要的,我已经做了一些研究并尝试了自己的代码,但似乎行不通。我还仔细检查了服务器版本。我正在运行SQL Server 13.0.6300.2 Google告诉我的是2016年。
我将数据加载到示例临时表中。
结果
我进行您建议的代码:
我也尝试了“ Cross Apply”,但我获得了此错误代码:
不正确的语法靠近关键字“ with with”。如果此语句是一个常见的表表达式,则XMLNAMESPACES子句或更改跟踪上下文子句,
则 必须用一个半词来终止先前的语句。结肠出错后。
Thanks, That seems to be what I'm looking for and I've done a bit of research and tried on my own code but it doesn't seem to work. I've also double-checked the server version. I'm running SQL Server 13.0.6300.2 which Google tells me is 2016.
I took my data and loaded it into a sample temp table.
Result
I then do the code you suggested:
I have also tried "CROSS APPLY" but I get this error code:
Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.
So I tried to add a semicolon ( ; ) in front of WITH but then everything after the colon gets error.