2017-10-16 88 views
0

目前查詢:MSSQL選擇總和從加盟加盟選擇

Select 
    FORMAT(Tracking.[start],'dd-MM-yyy H:m:s') as [start], 
    FORMAT(Tracking.[deliver],'yyy-MM-dd') as dateSort, 
    FORMAT(Tracking.[deliver],'dd-MM-yyyy') as deliver, 
    RTrim(Tracking.[orderNumber]) as orderNumber, 
    Tracking.[tcpState] as oTcpState, 
    Tracking.[transport], 
    Tracking.[orderType], 
    (SELECT Cast(SUM(Cast(stations.estimatedTime as decimal(18,2))/60) as decimal(18,2)) FROM stations WHERE stations.orderNumber = Tracking.orderNumber) AS oEstimatedTime, 
    JSON_VALUE(Tracking.[orderObj],'$."0".Referenztext') as Referenztext, 
    JSON_VALUE(Tracking.[orderObj],'$."0".TotalSW') as TotalSW, 
    stations.[id], 
    FORMAT(stations.[startdate],'dd-MM-yyyy') as startdate, 
    FORMAT(stations.[enddate],'dd-MM-yyyy') as enddate, 
    stations.[tcpState], 
    stations.[name], 
    stations.[estimatedTime], 
    stations.[pieces], 
    stations.[piecesDone], 
    FORMAT(stations.[estimatedStart],'dd-MM-yyyy') as estimatedStart, 
    FORMAT(stations.[estimatedEnd],'dd-MM-yyyy') as estimatedEnd, 
from Tracking left JOIN(

    Select * from Stations left JOIN (

      Select dummy.id as dId, dummy.orderNumber as dOrderNumber, SUM(dummy.elapsedTime) as elapsedTimeStationObj From (
       select orderNumber, id, DATEDIFF(mi,startdate,ISNULL(enddate,GETDATE())) as elapsedTime FROM StationObj 
      ) dummy group by dummy.orderNumber, dummy.id 
    ) as stationObj 
    on Stations.orderNumber = stationObj.dOrderNumber and Stations.id = stationObj.dId 
    where stations.deleted=0 

) as stations 
on Tracking.orderNumber = stations.orderNumber where Tracking.tcpState != 3 

想有從elapsedTimeStationObj總和爲好。

試圖做這樣的低於其他(SELECT CAST(SUM(CAST(stations.estimatedTime ....:

 (SELECT Cast(SUM(Cast(stations.elapsedTimeStationObj as decimal(18,2))) as decimal(18,2)) FROM stations WHERE stations.orderNumber = Tracking.orderNumber) AS requiredTime, 

,但它是不可能的,因爲elapsedTimeStationObj是不是從表中站有屬性。

回答

0

使用它作爲一個子查詢,並可以作爲CTE使用,使其可讀和reusble,像這樣:

WITH CTE1 
AS 
(
    SELECT DUMMY.id AS dId 
     ,DUMMY.orderNumber AS dOrderNumber 
     ,SUM(DUMMY.elapsedTime) AS elapsedTimeStationObj 
    FROM (
     SELECT orderNumber 
      ,id 
      ,DATEDIFF(mi, startdate, ISNULL(enddate, GETDATE())) AS elapsedTime 
     FROM StationObj 
     ) DUMMY 
    GROUP BY DUMMY.orderNumber 
     ,DUMMY.id 
), CTE2 
AS 
(
    SELECT orderNumber, Cast(SUM(Cast(stations.estimatedTime as decimal(18,2))/60) as decimal(18,2)) AS oEstimatedTime 
    FROM stations 
    GROUP BY stations.orderNumber 
) 
SELECT FORMAT(Tracking.[start], 'dd-MM-yyy H:m:s') AS [start] 
    ,FORMAT(Tracking.[deliver], 'yyy-MM-dd') AS dateSort 
    ,FORMAT(Tracking.[deliver], 'dd-MM-yyyy') AS deliver 
    ,RTrim(Tracking.[orderNumber]) AS orderNumber 
    ,Tracking.[tcpState] AS oTcpState 
    ,Tracking.[transport] 
    ,Tracking.[orderType] 
    ,s2.oEstimatedTime AS oEstimatedTime 
    ,JSON_VALUE(Tracking.[orderObj], '$."0".Referenztext') AS Referenztext 
    ,JSON_VALUE(Tracking.[orderObj], '$."0".TotalSW') AS TotalSW 
    ,stations.[id] 
    ,FORMAT(stations.[startdate], 'dd-MM-yyyy') AS startdate 
    ,FORMAT(stations.[enddate], 'dd-MM-yyyy') AS enddate 
    ,stations.[tcpState] 
    ,stations.[name] 
    ,stations.[estimatedTime] 
    ,stations.[pieces] 
    ,stations.[piecesDone] 
    ,FORMAT(stations.[estimatedStart], 'dd-MM-yyyy') AS estimatedStart 
    ,FORMAT(stations.[estimatedEnd], 'dd-MM-yyyy') AS estimatedEnd 
    , 
FROM Tracking 
LEFT JOIN (
    SELECT * 
    FROM Stations 
    LEFT JOIN CTE1 AS stationObj ON Stations.orderNumber = stationObj.dOrderNumber 
     AND Stations.id = stationObj.dId 
    WHERE stations.deleted = 0 
) AS stations ON Tracking.orderNumber = stations.orderNumber 
LEFT JOIN CTE2 s2 ON stations.orderNumber = s2.orderNumber 
WHERE Tracking.tcpState != 3 
+0

但我怎樣才能從「elapsedTimeStationObj」得到的總和主要選擇?用 - > SUM(stations.elepsedTimeStationObj)作爲t是不可能的。 CTE1不可訪問 –