gpt4 book ai didi

sql-server - SQL Server : Examples of PIVOTing String data

转载 作者:行者123 更新时间:2023-12-01 16:44:09 25 4
gpt4 key购买 nike

尝试找到一些简单的 SQL Server PIVOT 示例。我发现的大多数例子都涉及对数字进行计数或求和。我只想旋转一些字符串数据。例如,我有一个查询返回以下内容。

Action1 VIEW  
Action1 EDIT
Action2 VIEW
Action3 VIEW
Action3 EDIT

我想使用 PIVOT(如果可能的话)来得到如下结果:

Action1 VIEW EDIT  
Action2 VIEW NULL
Action3 VIEW EDIT

这是否可以通过 PIVOT 功能实现?

最佳答案

请记住,MAX 聚合函数适用于文本和数字。该查询只需要扫描一次表。

SELECT Action,
MAX( CASE data WHEN 'View' THEN data ELSE '' END ) ViewCol,
MAX( CASE data WHEN 'Edit' THEN data ELSE '' END ) EditCol
FROM t
GROUP BY Action

关于sql-server - SQL Server : Examples of PIVOTing String data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24470/

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