最小/最大子查询问题

时间:2016-07-29 10:22:06

标签: sql sql-server

有一些子查询错误的问题,因为它希望得出多个结果: 完整查询:

 SELECT TOP 1000
       Visit.BookingNumber AS 'Booking Number'
      ,Car.Name AS 'Car Name'
      ,Car.REGNumber AS 'REG Number'
      ,Type.Name AS 'Car Type'
      ,Car.[Length]
      ,Car.GrossTonnage
      ,Loc.Name AS 'Space'
      ,CAST ( 
            CASE WHEN Loc.Name = 'Zone 6' or Loc.Name = 'Zone 7'
                THEN 'Terminal 2' ELSE 'Terminal 1'
                END AS nvarchar) AS 'Terminal'

      ,(SELECT  MIN (VisitMove.ActualStopDateUTC) FROM [CarDB].[dbo].[CarMovement] 

      INNER JOIN  dbo.Visit ON CarMovement.Visit_GUID = Visit.Visit_GUID 
      INNER JOIN dbo.Location AS Loc ON CarMovement.MovementToLocation_GUID = Loc.Location_GUID

      WHERE  Visit.Visit_GUID = CarMovement.Visit_GUID and CarMovement.MovementType_ID = '1' and  Loc.Name in ('Zone 8' , 'Zone 9' , 'Zone 7' , 'Zone 6') 
      GROUP BY CarMovement.Visit_GUID)AS 'Arrive Time'

      --,MAX (VisitMove.ActualStopDateUTC) AS 'Finish Time'

  FROM [CarDB].[dbo].[Visit] AS Visit 

  INNER JOIN dbo.Car AS Car ON Visit.Car_GUID = Car.Car_GUID
  INNER JOIN dbo.CarMovement AS CarMove ON Visit.Visit_GUID = CarMove.Visit_GUID
  INNER JOIN dbo.Location AS Loc ON CarMove.MovementToLocation_GUID = Loc.Location_GUID
  INNER JOIN dbo.CarType AS Type ON Car.CarType_ID = Type.CarType_ID

  WHERE Loc.Name in ('Zone 8' , 'Zone 9' , 'Zone 7' , 'Zone 6')

  GROUP BY 
  Visit.BookingNumber, 
  Car.Name, 
  Car.IMONumber, 
  Type.Name, 
  Car.[Length], 
  Car.GrossTonnage,
  Loc.Name, 
  Visit.Visit_GUID, 
  CarMove.MovementType_ID, 
 CarMove.ActualStopDateUTC

我遇到问题的子查询:

(SELECT  MIN (VisitMove.ActualStopDateUTC) FROM [CarDB].[dbo].[CarMovement] 

  INNER JOIN  dbo.Visit ON CarMovement.Visit_GUID = Visit.Visit_GUID 
  INNER JOIN dbo.Location AS Loc ON CarMovement.MovementToLocation_GUID = Loc.Location_GUID

  WHERE  Visit.Visit_GUID = VisitMovement.Visit_GUID and CarMovement.MovementType_ID = '1' and  Loc.Name in ('Zone 8' , 'Zone 9' , 'Zone 7' , 'Zone 6') 
  GROUP BY CarMovement.Visit_GUID)AS 'Arrive Time'

我们的桌子设置是一个访问表和一个具有一对多关系的汽车运动表(访问可以有很多汽车运动)我正在尝试创建两个子查询(只关注让一个现在工作)根据visit_guid从汽车运动列表和最大(最后)停止日期中拉出最小(最早)停止日期。不幸的是,我认为我的子查询无法识别每个visit_id只有一个字段/记录,而是尝试将所有停止日期拉入表中。 (我通过自己运行查询来确定这一点,并将所有日期都拉回来。)

有什么想法吗?

3 个答案:

答案 0 :(得分:2)

标量子查询中的GROUP BY非常可疑。基本上,它永远不应该存在。为什么?标量子查询最多可以返回一行。 GROUP BY表示存在多个组 - 因此存在多个行。

所以,我建议:

(SELECT  MIN(VisitMove.ActualStopDateUTC)
 FROM [CarDB].[dbo].[CarMovement] INNER JOIN
      dbo.Visit
      ON CarMovement.Visit_GUID = Visit.Visit_GUID INNER JOIN
      dbo.Location AS Loc
      ON CarMovement.MovementToLocation_GUID = Loc.Location_GUID
 WHERE Visit.Visit_GUID = VisitMovement.Visit_GUID and 
       CarMovement.MovementType_ID = '1' and
       Loc.Name in ('Zone 8' , 'Zone 9' , 'Zone 7' , 'Zone 6') 
) as [Arrive Time]

我不是100%确定这会解决你所有的问题。但这很可能就是其中之一。

答案 1 :(得分:1)

我建议您使用OUTER APPLY

SELECT V.*, MOVE.* FROM VISIT V
                OUTER APPLY 
                   (SELECT TOP 1 M.* CarMovement M 
                                       WHERE M.GUID = V.GUID 
                                         ORDER BY M.YourDate ASC) AS MOVE

那说,从VISIT拿走你的单个记录,然后尝试找到按日期排序的TOP 1(如果有任何记录 - 否则为null)

然后,您可以通过创建第二个“外部应用”来对MAX日期执行相同的操作。按DATE DESC排序

不是所有的查询,只是如何将单个访问链接到第一个运动的骨架

答案 2 :(得分:0)

问题在于不需要子查询。

这是我对记录进行分组的方法,因此我在之前的评论中说过为什么我会成对收到一些记录。

我将记录分组,查看唯一ID。

我的坏:)

感谢您提供的所有帮助。