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子句遠不及它的幫助。
然而,在此先感謝!