Select as JSON 连接表
Select as JSON joined tables
我想将 2 table 秒的数据 select 转换成一个 json 字符串。第一个 table 应该列出来自第二个 table 的所有 linked table,例如
第一table:
SELECT [orderNumber], [tcpState]
FROM [Tracking]
第二个table:
SELECT [startdate], [enddate], [tcpState], [orderNumber], [name]
FROM [Stations]
跟踪可以link几个站点。
预期结果:
[{
"orderNumber": 123455,
"tcpState": 3,
"Stations": [{
"startdate": "2011-05-06",
"enddate": "2012-09-15",
"tcpState": 3,
"name": "Roger"
},
{
"startdate": "2011-02-06",
"enddate": "2012-05-15",
"tcpState": 4,
"name": "Hans"
}
]
},
{
"orderNumber": 1566,
"tcpState": 3,
"Stations": [{
"startdate": "2011-06-06",
"enddate": "2012-08-15",
"tcpState": "6",
"name": "Mike"
},
{
"startdate": "2011-03-06",
"enddate": "2012-03-15",
"tcpState": "6",
"name": "Tom"
}
]
}
]
好吧,首先你强迫我在我的 linux 上安装 sql-server 2017 只是为了测试查询,因为没有可用的在线测试器,所以我恨你有一点,但是谢谢,Docker 在那里...
有两种方法:
简单的,你在困难之后找到的
SELECT
[Tracking].[orderNumber],
[Tracking].[tcpState],
[Stations].[startdate],
[Stations].[enddate],
[Stations].[tcpState],
[Stations].[name]
FROM [Tracking]
LEFT JOIN [Stations]
ON [Tracking].[orderNumber] = [Stations].[orderNumber]
FOR JSON AUTO
最难的,自己打造JSON
/!\这个使用了STRING_AGG
函数,SQL-server 2017及以后只有/!\
SELECT
'['+iif(STRING_AGG(t1.[orderNumber],',') IS NOT NULL,
STRING_AGG(
'{'+
'"orderNumber":'+CONVERT(varchar(10), t1.[orderNumber])+','+
'"tcpState":'+CONVERT(varchar(10), t1.[tcpState])+','+
'"Stations":'+t1.Stations+
'}'
, ','), '')+
']' json
FROM
(
SELECT
t.[orderNumber],
t.[tcpState],
'['+ iif(STRING_AGG(s.[orderNumber],',') IS NOT NULL,
STRING_AGG(
'{'+
'"startdate":"'+s.[startdate]+'",'+
'"enddate":"'+s.[enddate]+'",'+
'"tcpState":'+CONVERT(varchar(10), s.[tcpState])+','+
'"name":"'+s.[name]+'"'+
'}'
, ','), '')+']' Stations
FROM [Tracking] t
LEFT JOIN [Stations] s ON t.[orderNumber] = s.[orderNumber]
GROUP BY t.[orderNumber], t.[tcpState]
) t1
GROUP BY t1.[orderNumber]
我想将 2 table 秒的数据 select 转换成一个 json 字符串。第一个 table 应该列出来自第二个 table 的所有 linked table,例如
第一table:
SELECT [orderNumber], [tcpState]
FROM [Tracking]
第二个table:
SELECT [startdate], [enddate], [tcpState], [orderNumber], [name]
FROM [Stations]
跟踪可以link几个站点。
预期结果:
[{
"orderNumber": 123455,
"tcpState": 3,
"Stations": [{
"startdate": "2011-05-06",
"enddate": "2012-09-15",
"tcpState": 3,
"name": "Roger"
},
{
"startdate": "2011-02-06",
"enddate": "2012-05-15",
"tcpState": 4,
"name": "Hans"
}
]
},
{
"orderNumber": 1566,
"tcpState": 3,
"Stations": [{
"startdate": "2011-06-06",
"enddate": "2012-08-15",
"tcpState": "6",
"name": "Mike"
},
{
"startdate": "2011-03-06",
"enddate": "2012-03-15",
"tcpState": "6",
"name": "Tom"
}
]
}
]
好吧,首先你强迫我在我的 linux 上安装 sql-server 2017 只是为了测试查询,因为没有可用的在线测试器,所以我恨你有一点,但是谢谢,Docker 在那里...
有两种方法:
简单的,你在困难之后找到的
SELECT
[Tracking].[orderNumber],
[Tracking].[tcpState],
[Stations].[startdate],
[Stations].[enddate],
[Stations].[tcpState],
[Stations].[name]
FROM [Tracking]
LEFT JOIN [Stations]
ON [Tracking].[orderNumber] = [Stations].[orderNumber]
FOR JSON AUTO
最难的,自己打造JSON
/!\这个使用了STRING_AGG
函数,SQL-server 2017及以后只有/!\
SELECT
'['+iif(STRING_AGG(t1.[orderNumber],',') IS NOT NULL,
STRING_AGG(
'{'+
'"orderNumber":'+CONVERT(varchar(10), t1.[orderNumber])+','+
'"tcpState":'+CONVERT(varchar(10), t1.[tcpState])+','+
'"Stations":'+t1.Stations+
'}'
, ','), '')+
']' json
FROM
(
SELECT
t.[orderNumber],
t.[tcpState],
'['+ iif(STRING_AGG(s.[orderNumber],',') IS NOT NULL,
STRING_AGG(
'{'+
'"startdate":"'+s.[startdate]+'",'+
'"enddate":"'+s.[enddate]+'",'+
'"tcpState":'+CONVERT(varchar(10), s.[tcpState])+','+
'"name":"'+s.[name]+'"'+
'}'
, ','), '')+']' Stations
FROM [Tracking] t
LEFT JOIN [Stations] s ON t.[orderNumber] = s.[orderNumber]
GROUP BY t.[orderNumber], t.[tcpState]
) t1
GROUP BY t1.[orderNumber]