【问题标题】:Display Columns To Rows将列显示为行
【发布时间】:2020-08-05 20:46:42
【问题描述】:

我有一张如下所示的表格:

AccountNumber, Warning01, Warning01ExpirationDate, Warning02, Waring02ExpirtionDate, .....
1234, 3,'2017-09-06',0, null
78976, 1,'2015-04-03',2,null

我想显示如下结果:

AccountNumber,Warning,ExpirationDate
1234,2,'2017-09-06'
78976,1,'2015-04-03'
78976,2,null

如果警告为 0 或 null,我想忽略它。

有什么想法吗?

【问题讨论】:

  • 为什么Warning01的值3变成了2?

标签: sql tsql where-clause unpivot


【解决方案1】:

在 T-SQL 中,只需使用 cross apply 和 values() 即可取消透视数据集:

select x.*
from mytable t
cross apply (values 
    (t.accountNumber, t.warning01, t.warning01expirationDate),
    (t.accountNumber, t.warning02, t.warning02expirationDate)
) as x(accountNumber, warning, expirationDate)
where x.warning <> 0

【讨论】:

  • 欢迎@MoeMoney。如果这正确回答了您的问题,请点击复选标志accept it。谢谢。
猜你喜欢
  • 1970-01-01
  • 2016-11-25
  • 2012-08-31
  • 2011-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-24
  • 2014-03-16
相关资源
最近更新 更多