SQL JOIN以某种方式或其他方式

时间:2015-02-13 14:22:37

标签: sql sql-server join

当第一个返回NULL时,是否可以执行另一个JOIN查询? 例如:

SELECT 
    tr.TripId,
    mvP.MovementId as FirstMovementId,
FROM
    [dbo].[Trips] AS tr 
    LEFT JOIN (
        SELECT 
            mvP.TripId,
            mvP.MovementId,
            mvP.PickUp_PlannedTime,
            DENSE_RANK() OVER (
                PARTITION BY 
                    mvP.TripId 
                ORDER BY 
                    mvP.PickUp_PlannedTime,
                    mvP.PickUp_Sequence,
                    mvP.MovementId
            ) AS RNK
        FROM 
            [dbo].[Movements] AS mvP
        WHERE 
            mvP.Deleted = 0 AND COALESCE (mvP.AbsenceStateName, 'Available') = 'Available'

     LEFT JOIN (
            SELECT 
                mvP.TripId,
                mvP.MovementId,
                mvP.PickUp_PlannedTime,
                DENSE_RANK() OVER (
                    PARTITION BY 
                        mvP.TripId 
                    ORDER BY 
                        mvP.PickUp_PlannedTime,
                        mvP.PickUp_Sequence,
                        mvP.MovementId
                ) AS RNK
            FROM 
                [dbo].[Movements] AS mvP
            WHERE 
                mvP.Deleted = 0
            ) mvP ON mvP.TripId = tr.TripId AND mvP.RNK = 1
       WHERE tr.Deleted = 0

更准确地说。我希望第二个JOIN填满第一个JOIN可能返回的可能NULL。我不知道如何做到这一点。

2 个答案:

答案 0 :(得分:2)

如果我理解正确,你只需在外部`select:

中使用coalesce()
SELECT tr.TripId,
       COALESCE(mvP1.MovementId, mvp2.Movementid) as FirstMovementId,

答案 1 :(得分:0)

您可以同时加入2个子查询,并使用isnull(SQL SERVER)或NVL(Oracle)函数来选择正确的查询。我假设tripId是进行连接的字段。

SELECT 
    tr.TripId,
    isnull(mvPav.MovementId, mvPdel.MovementId) as FirstMovementId,
FROM
    [dbo].[Trips] AS tr 
    LEFT OUTER JOIN (
        SELECT 
            mvP.TripId,
            mvP.MovementId,
            mvP.PickUp_PlannedTime,
            DENSE_RANK() OVER (
                PARTITION BY 
                    mvP.TripId 
                ORDER BY 
                    mvP.PickUp_PlannedTime,
                    mvP.PickUp_Sequence,
                    mvP.MovementId
            ) AS RNK
        FROM 
            [dbo].[Movements] AS mvP
        WHERE 
            mvP.Deleted = 0 AND COALESCE (mvP.AbsenceStateName, 'Available') = 'Available') AS mvPav on tr.TripId = mvPav.TripId
     LEFT OUTER JOIN (
            SELECT 
                mvP.TripId,
                mvP.MovementId,
                mvP.PickUp_PlannedTime,
                DENSE_RANK() OVER (
                    PARTITION BY 
                        mvP.TripId 
                    ORDER BY 
                        mvP.PickUp_PlannedTime,
                        mvP.PickUp_Sequence,
                        mvP.MovementId
                ) AS RNK
            FROM 
                [dbo].[Movements] AS mvP
            WHERE 
                mvP.Deleted = 0
            ) mvP ON mvP.TripId = tr.TripId AND mvP.RNK = 1
       WHERE tr.Deleted = 0) as mvPdel ON tr.TripId = mvPdel.TripId