提问人:Lavie 提问时间:3/31/2023 最后编辑:Lavie 更新时间:3/31/2023 访问量:77
对多行上的非数字值进行 SQL 透视
SQL Pivot on non-numeric values on multiple rows
问:
我有以下数据:
员工 | 日期 | 量 |
---|---|---|
Emp 1 (英语) | 01/01/2023 | 1.50 |
Emp 1 (英语) | 01/01/2023 | 7.5 磅 |
Emp 1 (英语) | 02/01/2023 | 0.00 |
Emp 1 (英语) | 02/01/2023 | 大小:1.2 kB |
Emp 1 (英语) | 03/01/2023 | 7.5 磅 |
我想对表格进行透视,以便在有多条记录的一天中,每条记录都应该在自己的行上,如下所示:
员工 | 01/01/2023 | 02/01/2023 | 03/01/2023 |
---|---|---|---|
Emp 1 (英语) | 1.50 | 0.00 | |
Emp 1 (英语) | 7.5 磅 | 1.5磅 | 7.5 磅 |
在我每天有多个值之前,我的查询如下:
Select Employee, Date, case when Type = 'A' then Amount else Amount + ' ' + 'LB' end as Amount from Table1
Pivot (max(Amount) for Dates in ([1],[2],[3],....[30],[31]) as PivotTable
您能协助我如何实现这一目标吗?
答: 暂无答案
评论
Amount
select p.* from ( select Employee, Date, case when Type = 'A' then Amount else Amount + ' ' + 'LB' end as Amount, case when Type = 'A' then 1 else 2 end as tprn from table1 ) as t pivot(max(Amount) for Date in ([01/01/2023], [02/01/2023], [03/01/2023])) as p
sql-server-2008-r2