我正在访问我的一个表,它在单独的行的列中显示vchCategory
(电子序列号#)的数据(esn
),但我想在同一行中看到它,我该怎么办?
我想显示vchEsn, dtTransactionDatetime, vchCategory1, vchCategory2
select *
from tblDailySalesRpt_History s
where vchEsn in (select vchEsn
from tblDailySalesRpt_History s
where vchOrigSalesMonth = 'mar 2014'
group by s.vchEsn
having count(vchesn) >= 2)
order by 1
返回以下内容:
答案 0 :(得分:1)
您可以使用PIVOT:
将多行中的数据一起收集到列中select
vchEsn, [Returns], [IW New Activation], [Deactivated]
from
(select vchEsn, dtTransactionDateTime, vchCategory from tblDailySalesRpt_History) s
pivot
(
MAX(dtTransactionDateTime)
FOR vchCategory in ([Returns], [IW New Activation], [Deactivated])
) pvt
order by vchEsn
示例输出(DEMO):
| VCHESN | RETURNS | IW NEW ACTIVATION | DEACTIVATED |
|--------------------|------------------------------|------------------------------|------------------------------|
| 256691413606894729 | March, 01 2014 11:34:24+0000 | March, 10 2014 16:20:51+0000 | (null) |
| 256691413607342471 | March, 12 2014 16:29:06+0000 | March, 17 2014 18:05:17+0000 | (null) |
| 256691413607372883 | March, 04 2014 10:46:18+0000 | (null) | March, 10 2014 20:01:33+0000 |