加入三个表并根据条件选择记录

时间:2017-06-19 07:52:14

标签: sql-server

#TransportationMode1 (indicates Road)
ShipmentID NOT NULL,
Origin NULL,
Stop1 NULL,
Stop2 NULL,
Destination NULL,
Distance NULL,
Cost  NULL

-------------------------------------------------------------------------------------
ShipmentID  Origin      Stop1           Stop2       Destination     Distance    Cost
1           Chicago     NULL            NULL        Atlanta         500         1000
2           Portland    Newyork         NULL        Atlanta         2500        8000    
3           Portland    Minneapolis     NULL        NULL            5500        8000   
3           NULL        Minneapolis     Newyork     NULL            4500        2000   
3           NULL        NULL            Newyork     Atlanta         3500        6000 
-------------------------------------------------------------------------------------
  

ShipmentID 1的成本总和为1000,距离为成本的1000 SUM   对于ShipmentID 2是8000,距离是ShipmentID的成本的8000 SUM   3是16000,距离是16000

#TransportationMode2 (indicates Rail)
ShipmentID NOT NULL,
Origin NULL,
Stop1 NULL,
Stop2 NULL,
Destination NULL,
Distance NULL,
Cost  NULL
-----------------------------------------------------------------------------------------
ShipmentID  Origin      Stop1           Stop2       Destination         Distance    Cost
1           Chicago     Newyork         NULL        NULL                1500        500
1           NULL        Newyork         NULL        Atlanta             500         13000   
2           Portland    Chicago         NULL        NULL                3000        1100        
2           NULL        Chicago         NULL        Atlanta             6000        1200    
3           Portland    Minneapolis     NULL        NULL                1500        8000   
3           NULL        Minneapolis     Newyork     NULL                2500        2000   
3           NULL        NULL            Newyork     Atlanta             500         6500 
-----------------------------------------------------------------------------------------
  

ShipmentID 1的成本总和为13500,距离为成本的2000 SUM   对于ShipmentID 2,2300,距离是ShipmentID成本的9000 SUM   3是16500,距离是4500

#TransportationMode3 (indicates Water)
ShipmentID NOT NULL,
Origin NULL,
Stop1 NULL,
Stop2 NULL,
Destination NULL,
Distance NULL,
Cost  NULL
---------------------------------------------------------------------------------
ShipmentID  Origin          Stop1           Stop2           Destination     Distance    Cost
1           Chicago         Newyork         NULL            NULL            1500        500
1           NULL            Newyork         Minneapolis     NULL            100         300 
1           NULL            NULL            Minneapolis     Atlanta         2000        1300    
2           Portland        Chicago         NULL            NULL            3000        11000       
2           NULL            Chicago         Newyork         NULL            100         130         
2           NULL            NULL            Newyork         Atlanta         500         220 
3           Portland        NULL            NULL            Atlanta         15000       12020 
---------------------------------------------------------------------------------
  

ShipmentID 1的成本总和为2100,距离为成本的3600 SUM   对于ShipmentID 2是11350,距离是3100 SUM of Costs for   ShipmentID 3是12020,距离是15000

选择成本最低的路线。距离并不重要。所有其他路由显示为NULL。

#TempResults表中的

预期结果

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
ShipmentID  Origin      Stop1       Stop2       Destination     TransportationMode1     Distance1   Cost1       TransportationMode2     Distance2   Cost2       TransportationMode3     Distance3   Cost3
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1           Chicago     NULL        NULL        Atlanta         Road                    500         1000        NULL                    NULL        NULL        NULL                    NULL        NULL
2           Portland    Chicago     NULL        Atlanta         NULL                    NULL        NULL        Rail                    9000        2300        NULL                    NULL        NULL
3           Portland    NULL        NULL        Atlanta         NULL                    NULL        NULL        NULL                    NULL        NULL        Water                   15000       12020

这里是#TempResults defintion:

#TempResults
ShipmentID NOT NULL,
Origin NOT NULL,
Stop1 NULL,
Stop2 NULL,
Destination NOT NULL,
TransportationMode1 NULL,
TransportationDistance1 NULL,
TransportationCost1 NULL,
TransportationMode2 NULL,
TransportationDistance2 NULL,
TransportationCost2 NULL,
TransportationMode3 NULL,
TransportationDistance3 NULL,
TransportationCost3 NULL

如何使用单个sql语句实现此目的。

INSERT INTO TempResults(
    ShipmentID,
    Origin,
    Stop1,
    Stop2,
    Destination,
    TransportationMode1,
    Distance1,
    Cost1,
    TransportationMode2,
    Distance2,
    Cost2,
    TransportationMode3,
    Distance3,
    Cost3) 
SELECT 
    RoadCosts.ShipmentID, 
    RoadCosts.OriginSite, 
    RoadCosts.Stop1, 
    RoadCosts.Stop2, 
    RoadCosts.DestinationSite, 
    ????
FROM ((#TransportationMode1 RoadCosts
INNER JOIN  #TransportationMode2 RailCosts ON RoadCosts.ShipmentID = RailCosts.ShipmentID) 
INNER JOIN #TransportationMode3 WaterCosts ON RoadCosts.ShipmentID = WaterCosts.ShipmentID)

1 个答案:

答案 0 :(得分:1)

如果我完全理解了这个问题,你可以这样做。

WITH CTE1
AS
(
    SELECT
        RoadCosts.Scenario,
        RoadCosts.ShipmentID,
        SUM(RoadCosts.Distance) AS Distance,
        SUM(RoadCosts.Cost) AS Cost
    FROM 
        #TransportationMode1 RoadCosts 
    GROUP BY 
        RoadCosts.Scenario,
        RoadCosts.ShipmentID

    UNION ALL 

    SELECT
        RailCosts.Scenario,
        RailCosts.ShipmentID,
        SUM(RailCosts.Distance) AS Distance,
        SUM(RailCosts.Cost) AS Cost
    FROM 
        #TransportationMode2 RailCosts 
    GROUP BY 
        RailCosts.Scenario,
        RailCosts.ShipmentID

    UNION ALL

    SELECT
        WaterCosts.Scenario,
        WaterCosts.ShipmentID,
        SUM(WaterCosts.Distance) AS Distance,
        SUM(WaterCosts.Cost) AS Cost
    FROM 
        #TransportationMode3 WaterCosts 
    GROUP BY 
        WaterCosts.Scenario,
        WaterCosts.ShipmentID
), CTE2
AS
(
    SELECT 
        A.ShipmentID,
        MIN(Distance) MinDistance,
        MIN(Cost) MinCost,
    FROM
        CTE1 A
    GROUP BY
        A.ShipmentID
)

SELECT
    *
FROM
    CTE1 A INNER JOIN 
    CTE2 B ON A.ShipmentID = B.ShipmentID AND
              A.Distance = B.MinDistance AND
              A.Cost = B.MinCost