我知道我没有正确写出问题,所以将在这里详细解释。
我有具有以下结构的表 TrnOutlet
Retailer Flag Date
------------------------------------------------------------
26901032 3 2012-05-05 00:00:00
26901032 1 2012-04-28 00:00:00
26901032 2 2012-04-21 00:00:00
26901032 3 2012-04-20 00:00:00
26901033 1 2012-04-28 00:00:00
26901033 3 2012-04-28 00:00:00
26901033 1 2012-04-21 00:00:00
26901033 1 2012-04-20 00:00:00
我希望输出是
RetailerId Flag Date flag date flag date....
------------------------------------------------------------
RetailerID1 flag1 Date1 flag2 Date2 ...... flagn Daten
RetailerID2 flag1 Date1 flag2 Date2 ...... flagn Daten
.
.
.
RetailerIDn flag1 Date1 flag2 Date2 ...... flagn Daten
输出将是
RetailerId Flag Date flag date flag date flag date
----------------------------------------------------------------------------------
26901032 3 2012-05-05 1 2012-04-28 2 2012-04-21 3 2012-04-20
26901032 1 2012-04-28 3 2012-04-28 1 2012-04-21 1 2012-04-20
注意:日期和标志可以重复,就像我们在上面的例子中看到的那样
编辑: 我用过的查询
select retailerId,BeatId,
MAX(case when TransactDate <> getDate() then TransactDate else '' end ) as Transactdate,
MAX(case when CompleteFlag in ('1','2','3') then completeFlag else '' end) as completeFlag
from TrnOutletHistory
group by TransactDate,RetailerID,BeatID
order by retailerID desc