SQL:从列显示不同的记录

abr*_*pin 0 sql sql-server sql-server-2008

样本数据:

LogID     OrderNo     MaxDate              AnotherDate     Status
NULL      1           2013-07-30 12:01:00  NULL            Pending
NULL      1           2013-07-30 12:01:01  NULL            Pending
NULL      1           2013-07-30 12:01:02  NULL            Pending
NULL      2           2013-07-30 12:02:00  NULL            Pending
NULL      3           2013-08-01 12:30:00  NULL            Pending
Run Code Online (Sandbox Code Playgroud)

预期产出:

LogID     OrderNo     MaxDate     AnotherDate     Status
NULL      1           2013-07-30  NULL            Pending
NULL      2           2013-07-30  NULL            Pending
NULL      3           2013-08-30  NULL            Pending
Run Code Online (Sandbox Code Playgroud)

LogIDOrderNo都是外键.MaxDate的数据类型是DateTime

UPDATE

尝试使用此SQL语句:

SELECT DISTINCT(OrderNo), LogID, MaxDate, AnotherDate, Status
FROM Logs
Run Code Online (Sandbox Code Playgroud)

但仍然显示3个1号订单

Rom*_*kar 5

我认为select distinct * from <your table>会对你有用

对于不同时间的情况,您可以使用:

 select distinct 
     LogID,
     OrderNo,
     cast(MaxDate as date) as MaxDate,
     AnotherDate,
     Status
 from <your table>
Run Code Online (Sandbox Code Playgroud)