SQL:非空时包含结果集中的列

时间:2017-07-27 10:31:14

标签: sql-server

ShipmentRouteInfo

ShipmentID      OriginSite      Stop1       Stop2       DestinationSite     Quantity
----------------------------------------------------------------------------------
    1           A               B           C           D                   10          //Taking all stops
    2           E               B           NULL        F                   20          //Shipment goes from E to F via B
    3           G               NULL        NULL        H                   30          //Shipment goes from G to H, no stops taken

我希望将此表转换为。

ShipmentID      OriginStopDestination           Site        PathID          Quantity
----------------------------------------------------------------------------------
1               Origin                          A           A_D             10
1               Stop1                           B           A_D             10
1               Stop2                           C           A_D             10
1               Destination                     D           A_D             10
2               Origin                          E           E_F             20  
2               Stop1                           B           E_F             20
2               Destination                     F           E_F             20
3               Origin                          G           G_H             30  
3               Destination                     H           G_H             30  

我希望为此格式Path ID的每件货件生成OriginSite_DestinationSite,并为每个来源网站stop1,stop2,目标网站生成一个单独的行。

这是我到目前为止所做的。

SELECT TOP 100 PERCENT Z.ShipmentID, Z.OriginStopDestination, Z.Site, Z.PathID, Z.Quantity
FROM (   
    SELECT A.ShipmentID, 'Origin' as OriginStopDestination, A.OriginSite as Site, CONCAT(CONCAT(A.OriginSite, '_'),A.DestinationSite) as PathID, A.Quantity
    FROM ShipmentRouteInfo A
    UNION
    SELECT B.ShipmentID, 'Destination' as OriginStopDestination, B.DestinationSite as Site, CONCAT(CONCAT(B.OriginSite, '_'),B.DestinationSite) as PathID, B.Quantity
    FROM ShipmentRouteInfo B
) AS Z 
ORDER BY Z.ShipmentID

给我......

ShipmentID      OriginStopDestination           Site        PathID          Quantity
----------------------------------------------------------------------------------
1               Origin                          A           A_D             10
1               Destination                     D           A_D             10
2               Origin                          E           E_F             20  
2               Destination                     F           E_F             20
3               Origin                          G           G_H             30  
3               Destination                     H           G_H             30  

我应该如何修改我的查询以包含停止信息?

请不要从我的查询中删除SELECT TOP 100 PERCENT Z.ShipmentID, Z.OriginStopDestination, Z.Site, Z.PathID, Z.Quantity我需要它用于其他目的。建议我如何修改内部查询块。

2 个答案:

答案 0 :(得分:1)

必须有一个问题,因为这对我来说似乎很明显,但你似乎想要的答案是:

SELECT TOP 100 PERCENT Z.ShipmentID, Z.OriginStopDestination, Z.Site, Z.PathID, Z.Quantity
FROM (   
    SELECT A.ShipmentID, 'Origin' as OriginStopDestination, A.OriginSite as Site, CONCAT(CONCAT(A.OriginSite, '_'),A.DestinationSite) as PathID, A.Quantity
    FROM ShipmentRouteInfo A
    UNION
SELECT B.ShipmentID, 'Stop1' as OriginStopDestination, B.OriginSite as Site, CONCAT(CONCAT(B.OriginSite, '_'),B.DestinationSite) as PathID, B.Quantity
FROM ShipmentRouteInfo B
WHERE B.Stop1 IS NOT NULL
UNION
SELECT C.ShipmentID, 'Stop2' as OriginStopDestination, C.DestinationSite as Site, CONCAT(CONCAT(C.OriginSite, '_'),C.DestinationSite) as PathID, C.Quantity
FROM ShipmentRouteInfo C
WHERE C.Stop2 IS NOT NULL
UNION 
SELECT D.ShipmentID, 'Destination' as OriginStopDestination, D.DestinationSite as Site, CONCAT(CONCAT(D.OriginSite, '_'),D.DestinationSite) as PathID, D.Quantity
FROM ShipmentRouteInfo D
) AS Z 
ORDER BY Z.ShipmentID

至少,此查询会继续您开始使用的逻辑,否则会对现有查询进行最少的更改。

答案 1 :(得分:0)

您可以使用UnPivot,如下所示:

Select ShipmentId, originstopdestination, [Site], [Path], Quantity from 
(Select *, [Path] = concat(originsite,'-',destinationsite) from #shipments ) a
unpivot ([Site] for [OriginStopDestination] in ([Originsite],[stop1],[stop2], [DestinationSite])) p

输出如下:


+------------+-----------------------+-----------------+------+----------+
| ShipmentId | originstopdestination |      Site       | Path | Quantity |
+------------+-----------------------+-----------------+------+----------+
|          1 | A                     | Originsite      | A-D  |       10 |
|          1 | B                     | stop1           | A-D  |       10 |
|          1 | C                     | stop2           | A-D  |       10 |
|          1 | D                     | DestinationSite | A-D  |       10 |
|          2 | E                     | Originsite      | E-F  |       20 |
|          2 | B                     | stop1           | E-F  |       20 |
|          2 | F                     | DestinationSite | E-F  |       20 |
|          3 | G                     | Originsite      | G-H  |       30 |
|          3 | H                     | DestinationSite | G-H  |       30 |
+------------+-----------------------+-----------------+------+----------+