I have a table with 3 columns as shown on Pic1.
Pic1:
My goal is to return a nested array as a JSON string:
"[
[7157688981.272619,7290098.188727271,null,null,null,null,null],
[9331221970.409422,-187354647.1071058,2949162.807512622,null,null,null,null],
[11015646818.20274,-482410203.4928556,14040634.58841678,-112035.0684939814,null,null,null],
...
]"
(in this case, 1 array wrapping 6 arrays of 7 floats/nulls each)
I managed to get results shown on Pic2 using this query:
Pic2:
SELECT FF,
[0] AS DO0 ,
[1] AS DO1 ,
[2] AS DO2 ,
[3] AS DO3 ,
[4] AS DO4 ,
[5] AS DO5 ,
[6] AS DO6
FROM (
SELECT TOP 36 FF, DO, Value
FROM MyTable WITH (NOLOCK)
WHERE Id = 100
ORDER BY FF, DO
) AS SourceTable
PIVOT (
MAX(value)
FOR DO IN ([0], [1], [2], [3], [4], [5], [6])
) AS PivotTable
Adding FOR JSON AUTO
I get this result, which is the closer I came to the desired result:
"[
{
"FF": 0,
"DO0": 8099996673.580311,
"DO1": -11940671.04204195
},
{
"FF": 1,
"DO0": 10575727464.91492,
"DO1": -233647906.0869318,
"DO2": 3359200.530983179
},
{
"FF": 2,
"DO0": 11693775141.49235,
"DO1": -429492903.0961588,
"DO2": 10721235.27578629,
"DO3": -74363.98732124352
},
...
]"
Could anyone give me some pointers? Thanks a lot.
Edit: Adding sample data:
| FF | D0 | D1 | D2 | D3 | D4 | D5 | D6 |
|:---:|--------:|--------:|-----------:|---------:|--------:|------:|-----:|
| 0 | 809973 | -1191 | NULL | NULL | NULL | NULL | NULL |
| 1 | 1057564 | -23366 | 3359200 | NULL | NULL | NULL | NULL |
| 2 | 1169341 | -42943 | 1079235 | -74363 | NULL | NULL | NULL |
| 3 | 1071256 | -15127 | -7915416 | 362620 | -3310 | NULL | NULL |
| 4 | 1409215 | -153899 | 13408335 | -5336555 | 93451 | -586 | NULL |
| 5 | -328619 | 804878 | -125937545 | 774136 | -226559 | 30247 | -155 |