Я пытаюсь выбрать результаты из файла JSON, используя OPENJSON()
в T SQL:
ask_price, bid_price, mark_price ,high_price ,low_price ,open_price ,KEY ,value
Я могу получить значения, но они содержат дубликаты. Он должен возвращать только 3 строки, но, как вы можете видеть, это 9 строк.
Я пытался использовать отличные и сгруппированные по, но все еще есть дубликаты, и это может повлиять на производительность. Есть ли альтернатива, которую я могу использовать?
DECLARE @JSONData AS NVARCHAR(4000)
SET @JSONData = N'{ "results":[ { "ask_price":"377.360000", "open_price":"376.475000","symbol":"BCHUSD","id":"24566"},
{ "ask_price":"9668.090000","open_price":"9642.645000","symbol":"BTCUSD","id":"32555"},
{"ask_price":"262.660000", "open_price":"260.890000","symbol":"ETHUSD","id":"356789"} ] }'
SELECT Y.*, Z.[KEY],Z.[value] FROM OPENJSON(@JSONData) AS x CROSS APPLY OPENJSON(x.value) WITH ( ask_price numeric(18,12), open_price numeric(18,12) ) AS Y CROSS APPLY OPENJSON(@JSONData, '$.results') as Z
РЕЗУЛЬТАТЫ
Current results
ask_price open_price KEY value
377.360000000000 376.475000000000 0 { "ask_price":"377.360000", "open_price":"376.475000","symbol":"BCHUSD","id":"24566"}
377.360000000000 376.475000000000 1 { "ask_price":"9668.090000","open_price":"9642.645000","symbol":"BTCUSD","id":"32555"}
377.360000000000 376.475000000000 2 {"ask_price":"262.660000", "open_price":"260.890000","symbol":"ETHUSD","id":"356789"}
9668.090000000000 9642.645000000000 0 { "ask_price":"377.360000", "open_price":"376.475000","symbol":"BCHUSD","id":"24566"}
9668.090000000000 9642.645000000000 1 { "ask_price":"9668.090000","open_price":"9642.645000","symbol":"BTCUSD","id":"32555"}
9668.090000000000 9642.645000000000 2 {"ask_price":"262.660000", "open_price":"260.890000","symbol":"ETHUSD","id":"356789"}
262.660000000000 260.890000000000 0 { "ask_price":"377.360000", "open_price":"376.475000","symbol":"BCHUSD","id":"24566"}
262.660000000000 260.890000000000 1 { "ask_price":"9668.090000","open_price":"9642.645000","symbol":"BTCUSD","id":"32555"}
262.660000000000 260.890000000000 2 {"ask_price":"262.660000", "open_price":"260.890000","symbol":"ETHUSD","id":"356789"}
Expected results
ask_price open_price KEY value
377.360000000000 376.475000000000 0 { "ask_price":"377.360000", "open_price":"376.475000","symbol":"BCHUSD","id":"24566"}
9668.090000000000 9642.645000000000 1 { "ask_price":"9668.090000","open_price":"9642.645000","symbol":"BTCUSD","id":"32555"}
262.660000000000 260.890000000000 2 {"ask_price":"262.660000", "open_price":"260.890000","symbol":"ETHUSD","id":"356789"}