2016-09-29 87 views
0

我有一個MS SQL 2012數據庫中的表有6列MS SQL,子查詢和EXISTS ERROR

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

我想生成一個查詢,會給我一個單獨的行,每一天對於每個驅動程序有一天,JobNumbers的數量,最早的JobStart和最新的JobEnd(這部分很簡單),然後是總小時數和總分鐘數以及總分鐘數。第二部分,我只能分別工作,如果我每個具體的一天和驅動程序。 (伊克!)

我正在這裏「只有一個表達式可以在選擇列表時,子查詢不與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子句遠不及它的幫助。

然而,在此先感謝!

回答

1

你不能在SELECT這樣的子列表中選擇多個記錄。我認爲你在尋找什麼是CROSS APPLY

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將執行內部子查詢中的每個記錄,其結果則可以通過SELECT引用。