gpt4 book ai didi

sql - 将多列数据转换为单行降序排序

转载 作者:行者123 更新时间:2023-12-05 05:42:36 24 4
gpt4 key购买 nike

我正在尝试将包含类别(在本例中为车辆)的总计的原始来源查询到第二个表中。

<表类="s-表"><头>摩托车自行车汽车<正文>132

期望的输出:

<表类="s-表"><头>车辆数量<正文>自行车3汽车2摩托车1

此外,我需要数量按如上所示的降序排序。

到目前为止,我已尝试执行 Unpivot,但 Unpivot 函数中存在语法错误。是否有其他方法可以达到相同的结果?

到目前为止我的代码:

SELECT Vehicle_Name 
FROM
(
SELECT [Motorcycle], [Bycycle], [Car] from Data
) as Source
UNPIVOT
(
Vehicle FOR Vehicle_Name IN ([Motorcycle], [Bycycle], [Car])
) as Unpvt

最佳答案

编辑:添加排序要求。

你也可以在这里使用CROSS APPLY

select vehicle, amnt
from test
cross apply(
VALUES('motorcycle', motorcycle)
,('bicycle', bicycle)
,('car', car)) x (vehicle, amnt)
order by amnt desc

fiddle here

关于sql - 将多列数据转换为单行降序排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72005397/

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