0

样本数据:

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

预期输出:

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

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

更新

尝试使用此 SQL 语句:

SELECT DISTINCT(OrderNo), LogID, MaxDate, AnotherDate, Status
FROM Logs

但仍显示 3 个 1 号订单

4

4 回答 4

5

我想select distinct * from <your table>会为你工作

在不同时间的情况下,您可以使用:

 select distinct 
     LogID,
     OrderNo,
     cast(MaxDate as date) as MaxDate,
     AnotherDate,
     Status
 from <your table>
于 2013-07-30T05:54:50.227 回答
2
   Select LogId,
           OrderNo,
          Cast( MaxdATE as Date) as MaxdATE  ,
           AnotherDate,
           Status from
    (
            Select LogId,
            OrderNo,
            MaxdATE,
            AnotherDate,
            Status,
           Row_Number() Over( Partition by OrderNo Order by MaxDate Desc) as Row
            from TableName
    ) T where T.Row=1
于 2013-07-30T05:57:14.423 回答
2

我认为你正在寻找的是这样的:

SELECT [LOGID], 
       [ORDERNO], 
       Max([MAXDATE]) MaxDate, 
       [ANOTHERDATE], 
       [STATUS] 
FROM   Logs
GROUP  BY [LOGID], 
          [ORDERNO], 
          [ANOTHERDATE], 
          [STATUS] 

以这个SQL Fiddle为例。

于 2013-07-30T06:26:17.893 回答
1

我认为这足以满足您的要求

SELECT DISTINCT 
       LogID ,
       OrderNo,
       MaxDate ,
       AnotherDate,
       Status
FROM table

或者如果你想根据你的外键来区分你可以使用这个

;WITH cte AS
(
   SELECT
     ROW_NUMBER() OVER (PARTITION BY LogID ,OrderNo ORDER BY MaxDate ) AS rno,
      LogID ,
           OrderNo,
           MaxDate ,
           AnotherDate,
           Status
    FROM table
 )
SELECT * FROM cte WHERE rno =1
于 2013-07-30T06:03:46.907 回答