sql - Select rows as different columns in SQL Server
问题描述
I have table with following structure:
I want to show it like this:
Maximum 5 users will be there per LocIC and AuditID
解决方案
你想要条件聚合:
select LocID, AuditID,
max(case when Seq = 1 then userID end) User1,
max(case when Seq = 2 then userID end) User2,
max(case when Seq = 3 then userID end) User3,
max(case when Seq = 4 then userID end) User4,
max(case when Seq = 5 then userID end) User5
from (select *,
row_number() over (partition by LocID, AuditID order by userID) Seq
from table a
) t
group by LocID, AuditID;
推荐阅读
- sql - 如何仅获取记录的最大行数行?
- postgresql - 按日期范围分区 PostgreSQL 扫描所有分区
- c# - 如何在 SwaggerUI 中使用查询字符串传递对象数组
- c - openmp 中嵌套 for 循环的性能改进失败
- arrays - 类 stdClass 的对象无法在 Laravel 中转换为字符串?
- html - 如何在Angular Material stepper中用mat-chip替换下一个按钮?
- python - 在 Pandas UDF 中找不到用于多节点的模块
- php - PhpOffice\PhpSpreadsheet 无法为列或整个电子表格设置默认格式
- sql - 将字符升序排序,然后数字升序
- spring-mvc - 如何使用 springMvc 从表单(“multipart/form-data”)中获取数据?