-5

您好,我在这里有一个车辆检查数据库,并且我在 sql 中有一个代码,它给了我第二个和后续的 EventTime 或大于 Min(EventTime) 以及相应的 TicketStatus。表是 [Event] 和 [Car]... [Event] 表有字段 CarID (FK);事件时间;TicketStatus... 虽然在 [Car] 表上有字段 CarID (PK); 板号;包裹

注意:[Car].CarID 数据类型是唯一标识符 Ex。{16F0151E-1938-4487-BB75-FF446B4DB7FF}

执行代码会给我 CarID、EventTime、TicketStatus。我不想要 CarID 作为结果,我想要表 [Car]、TicketStatus、EventTime 和标准 [Car].Package = 4 中的 PlateNo

SELECT CarID, TicketStatus, [EventTime]  
FROM 
  (SELECT ROW_NUMBER() OVER 
       (PARTITION BY CarID ORDER BY [EventTime] ASC) AS [index]
       , CarID, TicketStatus, [EventTime] 
   FROM vehicle.dbo.Event Event) A  
 WHERE [index] > 1 
4

1 回答 1

1

如果我正确理解你想要什么,你只需要这样JOINCar表格

SELECT c.PlateNo, TicketStatus, EventTime 
  FROM 
(
  SELECT ROW_NUMBER() OVER 
       (PARTITION BY CarID ORDER BY EventTime) AS rnum
       , CarID, TicketStatus, EventTime
   FROM Event
) a JOIN Car c
   ON a.CarID = c.CarID  
 WHERE c.Package = 4
   AND a.rnum > 1

或者

SELECT PlateNo, TicketStatus, EventTime
  FROM 
(
  SELECT e.CarID, e.TicketStatus, e.EventTime, c.PlateNo,
         ROW_NUMBER() OVER 
         (PARTITION BY e.CarID ORDER BY e.EventTime) AS rnum 
    FROM Event e JOIN Car c 
      ON e.CarID = c.CarID 
   WHERE c.Package = 4
) a
 WHERE a.rnum > 1

这是SQLFiddle演示

于 2013-07-27T08:30:02.027 回答