我有以下SQL
查询:
SELECT Date AS timestamps, Seasons1 AS seasons_1, Seasons2 AS seasons_2,
Seasons3 AS seasons_3, Seasons4 AS seasons_4, Seasons5 AS seasons_5,
Seasons6 AS seasons_6, Seasons7 AS seasons_7
FROM SeasonsTable WITH(NOLOCK)
ORDER BY timestamps DESC
有没有办法返回包含所有seasons
作为数组的单列(称为seasons_*
)?
例如,当前返回:
timestamps seasons_1 ... seasons_7
2019-04-28 1.0 2.0 3.0 4.0 5.0 6.0 7.0
但我希望它返回:
timestamps seasons
2019-04-28 [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0]
答案 0 :(得分:1)
select Date as timestamps, '[' + cast(Seasons1 as nvarchar) + ',' + cast(Seasons2 as nvarchar) + ... + ']' as seasons
答案 1 :(得分:1)
如评论中所述,TSQL没有数组。 要将数据作为定界字符串返回,只需连接列:
SELECT Date AS timestamps, Seasons1 + ',' + Seasons2 + ',' + Seasons3 + ',' + Seasons4 + ',' + Seasons5 + ',' + Seasons6 + ',' + Seasons7 AS season
FROM SeasonsTable WITH(NOLOCK)
ORDER BY timestamps DESC
取决于季节列的数据类型,您可能需要使用cast(seasonsX作为varchar),而不仅仅是列名。