MS SQL,子查询和EXISTS错误

时间:2016-09-29 18:02:00

标签: sql sql-server subquery exists

我有一个MS SQL 2012数据库,该表有6列

ID
JobDate DATETIME
DriverID INT
JobNUmber NVARCHAR(20)
JobStart TIME
JobEnd TIME

我正在尝试生成一个查询,每天为每个具有该日期的驱动程序,JobNumbers的计数,最早的JobStart和最新的JobEnd(这部分很容易),然后总小时数和总分钟和总分钟。第二部分我只能在每个特定的日子和驱动程序分别开始工作。 (EEK!)

我收到的错误是“当EXISTS没有引入子查询时,只能在选择列表中指定一个表达式。

这是我想要做的工作。

DECLARE @mnth NVARCHAR(20) = 'Jan-16'
DECLARE @FROM NVARCHAR(20) = '2015-12-28'
DECLARE @TO NVARCHAR(20) = '2016-01-31'

SELECT @mnth AS 'Month',
x.DriverID As 'Driver',
DAY(x.JobDate) AS 'Day' ,
COUNT(x.JobNumber) AS 'Stops',
MIN(x.JobStart) AS 'Start',
MAX(x.JobEnd) AS 'End',
(SELECT
    SUM(hrs) AS 'Hours', SUM(mins) AS 'Minutes', SUM(ABS(hrs * 60) + (ABS(mins))) AS 'Total Minutes'
    FROM
    ( SELECT
        ABS(SUM(E_hrs - S_hrs)) AS hrs,
        ABS(SUM(E_mins - S_mins)) AS mins
        FROM
            (SELECT
                DATEPART(HOUR,MIN(j.JobEnd)) AS E_hrs,
                DATEPART(HOUR,MAX(j.JobStart)) AS S_hrs,
                DATEPART(MINUTE,MAX(j.JobEnd)) AS E_mins,
                DATEPART(MINUTE,MIN(j.JobStart)) AS S_mins
                FROM Xora_Job_Time j
                WHERE j.JobDate = x.JobDate
                AND j.DriverID = x.DriverID
            ) a
        ) b
) c
FROM Xora_Job_Time x
WHERE x.JobDate >= @FROM
AND x.JobDate <= @TO
GROUP BY DriverID, DAY(JobDate), JobDate
ORDER BY DriverID, DAY(JobDate)

这部分效果很好!

DECLARE @mnth NVARCHAR(20) = 'Jan-16'
DECLARE @FROM NVARCHAR(20) = '2015-12-28'
DECLARE @TO NVARCHAR(20) = '2016-01-31'

SELECT @mnth AS 'Month',
x.DriverID As 'Driver',
DAY(x.JobDate) AS 'Day' ,
COUNT(x.JobNumber) AS 'Stops',
MIN(x.JobStart) AS 'Start',
MAX(x.JobEnd) AS 'End'
FROM Xora_Job_Time x
WHERE x.JobDate >= @FROM
AND x.JobDate <= @TO
GROUP BY DriverID, DAY(JobDate)
ORDER BY DriverID, DAY(JobDate)

并向我提供了我正在寻找的信息,但是尝试添加时间计数导致此操作因EXISTS错误而失败。

这有效,但我需要每天都做自己的司机......

SELECT SUM(hrs)AS 'Hours', SUM(mins) AS 'Minutes', SUM(ABS(hrs * 60) + (ABS(mins))) AS 'Total Minutes'
FROM
( SELECT
    ABS(SUM(E_hrs - S_hrs)) AS hrs,
    ABS(SUM(E_mins - S_mins)) AS mins
    FROM
        (SELECT
            DATEPART(HOUR,MIN(JobEnd)) AS E_hrs,
            DATEPART(HOUR,MAX(JobStart)) AS S_hrs,
            DATEPART(MINUTE,MAX(JobEnd)) AS E_mins,
            DATEPART(MINUTE,MIN(JobStart)) AS S_mins
            FROM Xora_Job_Time
            WHERE JobDate = '2016-01-06'
            AND DriverID = '3') a
    ) b

我得到的错误是带有EXIST的子查询,我没有看到我想要一个EXIST子句的地方,更不用说它会有什么帮助。

尽管如此,请提前致谢!

1 个答案:

答案 0 :(得分:1)

您不能在datetime2列表的子选项中拥有多条记录。我认为你要找的是CROSS APPLY

SELECT

Declare @Mnth NVarchar(20) = 'Jan-16'; Declare @From NVarchar(20) = '2015-12-28'; Declare @To NVarchar(20) = '2016-01-31'; Select @Mnth As 'Month' , x.DriverID As 'Driver' , Day(x.JobDate) As 'Day' , Count(x.JobNumber) As 'Stops' , Min(x.JobStart) As 'Start' , Max(x.JobEnd) As 'End' , c.Hours , c.Minutes , c.[Total Minutes] From Xora_Job_Time x Cross Apply (Select Sum(b.hrs) As 'Hours' , Sum(b.mins) As 'Minutes' , Sum(Abs(b.hrs * 60) + (Abs(b.mins))) As 'Total Minutes' From (Select Abs(Sum(a.E_hrs - a.S_hrs)) As hrs , Abs(Sum(a.E_mins - a.S_mins)) As mins From (Select DatePart(Hour, Min(j.JobEnd)) As E_hrs , DatePart(Hour, Max(j.JobStart)) As S_hrs , DatePart(Minute, Max(j.JobEnd)) As E_mins , DatePart(Minute, Min(j.JobStart)) As S_mins From Xora_Job_Time j Where j.JobDate = x.JobDate And j.DriverID = x.DriverID ) a ) b ) c Where x.JobDate >= @From And x.JobDate <= @To Group By DriverID , Day(JobDate) , JobDate Order By DriverID , Day(JobDate); 将为每条记录执行内部子查询,然后CROSS APPLY可以引用其结果。