gpt4 book ai didi

SQL 查询到 JSON

转载 作者:行者123 更新时间:2023-12-04 21:39:06 24 4
gpt4 key购买 nike

您好,我是 JSON 的新手,想问这个问题:

所以我的目标是让结果看起来像这样:

{
"userid": "10293304" , "segments": ["Online_Flag"]
}

{
"userid": "10292298" , "segments": ["schedule_Appointment", "Enrolled_Order","Complete_Order"]

}

我能够使用 T SQL 查询结果如下所示:

[{"userid":"10293159","segments":[{"segment":"Schedule_Appointment"}]},
{"userid":"10293056","segments":[{"segment":"Schedule_Appointment"}]},
{"userid":"10292838","segments":[{"segment":"Schedule_Appointment"}]},

问题是我只需要显示 JSON 值(Schedule_Appointment),而不是键(段)

有没有办法(使用 TSQL)隐藏键并只显示 JSON 数组中的值?

我曾尝试寻找使用 SQL 创建 JSON 数组的方法,但没有成功,如果有人能解释一下,我将不胜感激。

SQL 服务器 2016

数据

CREATE TABLE [dbo].[Lotame_JSON](
[lead_id] [varchar](100) NULL,
[dist_date_key] [date] NULL,
[online_flag] [int] NULL,
[sched_appt_qty] [int] NULL,
[enroll_order_qty] [int] NULL,
[compl_order_qty] [int] NULL)

insert into [dbo].[Lotame_JSON]
values('105646','2016-12-1',1,0,0,1)

insert into [dbo].[Lotame_JSON]
values('125646','2016-12-1',0,0,1,0)

insert into [dbo].[Lotame_JSON]
values('112646','2016-12-1',0,1,1,0)

insert into [dbo].[Lotame_JSON]
values('106446','2016-12-1',0,0,1,0)

我有查询:

select 
[lead_id] AS 'userid',
(
SELECT segment
from
(
SELECT
CASE WHEN [online_flag] > 0 THEN '1' else null end as 'online_flag',
CASE WHEN sched_appt_qty > 0 THEN '2' else null end as 'Schedule_Appointment',
CASE WHEN enroll_order_qty > 0 THEN '3' else null end as 'Enrolled_Order',
CASE WHEN compl_order_qty > 0 THEN '4' else null end as 'Complete_Order'
FROM [dbo].[Lotame_JSON] as sub
WHERE sub.lead_id = main.lead_id
) t
UNPIVOT
(
segment1
for segment in (online_flag,Schedule_Appointment,Enrolled_Order,Complete_Order)
) as UnPvot
for JSON PATH
) AS 'segments'
from [dbo].[Lotame_JSON] as main
where online_flag = 0 and (sched_appt_qty > 0 or enroll_order_qty > 0 or compl_order_qty > 0)
and dist_date_key >= '2016-9-1'
FOR JSON PATH

谢谢

最佳答案

你非常接近。在 SQL 中正确格式化 JSON 数组有一个技巧。您对数组中所需的数据使用 JSON_QUERYSTUFFFOR XML PATH 的组合。要获得没有最外层方括号的结果,请使用 FOR JSON PATH, WITHOUT_ARRAY_WRAPPER。将它们放在一起,您会得到如下所示的查询:

SELECT 
userid = [lead_id]
,segments = JSON_QUERY(
'[' + STUFF(
(
SELECT ',' + '"' + segment + '"'
FROM
(
SELECT
CASE WHEN [online_flag] > 0 THEN '1' ELSE NULL END AS 'online_flag',
CASE WHEN sched_appt_qty > 0 THEN '2' ELSE NULL END AS 'Schedule_Appointment',
CASE WHEN enroll_order_qty > 0 THEN '3' ELSE NULL END AS 'Enrolled_Order',
CASE WHEN compl_order_qty > 0 THEN '4' ELSE NULL END AS 'Complete_Order'
FROM [dbo].[Lotame_JSON] AS sub
WHERE sub.lead_id = main.lead_id
) t
UNPIVOT
(
segment1 FOR segment IN (online_flag, Schedule_Appointment, Enrolled_Order, Complete_Order)
) AS UnPvot
FOR XML PATH ('')
)
, 1, 1, '') + ']'
)
FROM [dbo].[Lotame_JSON] AS main
WHERE online_flag = 0
AND dist_date_key >= '2016-9-1'
AND (
sched_appt_qty > 0
OR enroll_order_qty > 0
OR compl_order_qty > 0
)
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER

关于SQL 查询到 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41071084/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com