我知道我需要使用 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"
},
ETC...