Я знаю, что мне нужно использовать FOR JSON, чтобы сделать эту работу, я просто не знаю, как сделать группы по определенному столбцу для этого.
У меня есть следующий набор данных:
property confirmation exemptions taxReasonId taxId
0145 29SW8TW9F 2020-05-09: ["occupancy tax","room tax"] 5e61836ed0687c000143d77e OTA Prepaid
0145 2QWKNZM8F 2020-08-07: ["occupancy tax","room tax"] 5e61836ed0687c000143d77e OTA Prepaid
0145 2QWKNZM8F 2020-08-08: ["occupancy tax","room tax"] 5e61836ed0687c000143d77e OTA Prepaid
0145 3BM6SV9C3 2020-06-25: ["occupancy tax","room tax"] 5e61836ed0687c000143d77e OTA Prepaid
0145 3BM6SV9C3 2020-06-26: ["occupancy tax","room tax"] 5e61836ed0687c000143d77e OTA Prepaid
0145 3BM6SV9C3 2020-06-27: ["occupancy tax","room tax"] 5e61836ed0687c000143d77e OTA Prepaid
0145 3BM6SV9C3 2020-06-28: ["occupancy tax","room tax"] 5e61836ed0687c000143d77e OTA Prepaid
0145 3BM6SV9C3 2020-06-29: ["occupancy tax","room tax"] 5e61836ed0687c000143d77e OTA Prepaid
0145 3BM6SV9C3 2020-06-30: ["occupancy tax","room tax"] 5e61836ed0687c000143d77e OTA Prepaid
0145 3BM6SV9C3 2020-07-01: ["occupancy tax","room tax"] 5e61836ed0687c000143d77e OTA Prepaid
0145 3BM6SV9C3 2020-07-02: ["occupancy tax","room tax"] 5e61836ed0687c000143d77e OTA Prepaid
0145 3BM6SV9C3 2020-07-03: ["occupancy tax","room tax"] 5e61836ed0687c000143d77e OTA Prepaid
0145 3BM6SV9C3 2020-07-04: ["occupancy tax","room tax"] 5e61836ed0687c000143d77e OTA Prepaid
0145 3FY4HQ7RB 2020-05-13: ["occupancy tax","room tax"] 5e61836ed0687c000143d77e OTA Prepaid
0145 3FY4HQ7RB 2020-05-14: ["occupancy tax","room tax"] 5e61836ed0687c000143d77e OTA Prepaid
0145 3FY4HQ7RB 2020-05-15: ["occupancy tax","room tax"] 5e61836ed0687c000143d77e OTA Prepaid
0145 3FY4HQ7RB 2020-05-16: ["occupancy tax","room tax"] 5e61836ed0687c000143d77e OTA Prepaid
0145 3FY4HQ7RB 2020-05-17: ["occupancy tax","room tax"] 5e61836ed0687c000143d77e OTA Prepaid
0145 3FY4HQ7RB 2020-05-18: ["occupancy tax","room tax"] 5e61836ed0687c000143d77e OTA Prepaid
0145 3FY4HQ7RB 2020-05-19: ["occupancy tax","room tax"] 5e61836ed0687c000143d77e OTA Prepaid
0145 3FY4HQ7RB 2020-05-20: ["occupancy tax","room tax"] 5e61836ed0687c000143d77e OTA Prepaid
0145 3FY4HQ7RB 2020-05-21: ["occupancy tax","room tax"] 5e61836ed0687c000143d77e OTA Prepaid
0145 3FY4HQ7RB 2020-05-22: ["occupancy tax","room tax"] 5e61836ed0687c000143d77e OTA Prepaid
0145 3FY4HQ7RB 2020-05-23: ["occupancy tax","room tax"] 5e61836ed0687c000143d77e OTA Prepaid
0145 3FY4HQ7RB 2020-05-24: ["occupancy tax","room tax"] 5e61836ed0687c000143d77e OTA Prepaid
0145 3FY4HQ7RB 2020-05-25: ["occupancy tax","room tax"] 5e61836ed0687c000143d77e OTA Prepaid
0145 3FY4HQ7RB 2020-05-26: ["occupancy tax","room tax"] 5e61836ed0687c000143d77e OTA Prepaid
Мне нужно превратить каждую группу «подтверждения» в полезную нагрузку JSON, предпочтительно со столбцом идентификатора «подтверждение»:
{
"taxId": "OTAPrepaid",
"taxReasonId": "5e61836ed0687c000143d77e",
"exemptions":
{
"2020-08-07": ["occupancy tax", "room tax"],
"2020-08-08": ["occupancy tax", "room tax"]
}
}
Это запрос, который я использую для получения моего набор данных:
SELECT res.property, res.confirmationId, CAST(date AS VARCHAR(12)) + ': ' + tax.taxClass AS exemptions, id AS taxReasonId, tax.reasonName AS taxId
FROM
(
SELECT property, date, confirmationId
FROM dbo.tb_rguest_reservation_records
WHERE CAST(property AS INT) IN
(
50,66,74,98,105,149, --Kentucky, Room Tax Exempt
132, --South Carolina, Room and Occupancy Tax Exempt
11,160,165,167, --Colorado, Room Tax Exempt
19,46,87,88,145,169,191, --Indiana, Room and Occupancy Tax Exempt
116,142,162 --New Mexico, Room Tax Exempt
)
AND ratePlanId IN ('NOPTS','ADVOTC','ADVPLA','ADVHT','OTAPLA','OTAPAO','OTAHT','OTAHOT')
AND arrivalDate >= CAST(GETDATE() AS DATE)
AND property = '0145'
) res
LEFT JOIN
(
SELECT property, reasonName, id, taxClass
FROM dbo.tb_rguest_tax_exempt_reasons
WHERE reasonName = 'OTA Prepaid'
) tax
ON res.property = tax.property
FOR JSON PATH
Когда я запускаю FOR JSON PATH, он дает мне следующее, что я не совсем ищу.
[
{
"property": "0145",
"confirmationId": "74XD3NMWF",
"exemptions": "2020-03-06: [\"occupancy tax\",\"room tax\"]",
"taxReasonId": "5e61836ed0687c000143d77e",
"taxId": "OTA Prepaid"
},
{
"property": "0145",
"confirmationId": "T6JYFMT6P",
"exemptions": "2020-03-06: [\"occupancy tax\",\"room tax\"]",
"taxReasonId": "5e61836ed0687c000143d77e",
"taxId": "OTA Prepaid"
},
{
"property": "0145",
"confirmationId": "92DR3M7DB",
"exemptions": "2020-03-11: [\"occupancy tax\",\"room tax\"]",
"taxReasonId": "5e61836ed0687c000143d77e",
"taxId": "OTA Prepaid"
},
et c. ..