MSSQL从连接选择中选择加入的总和

时间:2017-10-16 16:48:42

标签: sql-server

当前查询:

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获得SUM。

试图在另一个下面这样做(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不是表格站的属性。

1 个答案:

答案 0 :(得分:0)

将其用作子查询,您可以使用CTE使其可读并可重复使用,如下所示:

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