SQL Server子查询根据另一个子查询返回任何内容

时间:2017-06-07 17:36:11

标签: sql sql-server subquery ssrs-2012

我有一个2个子查询,根据第一个子查询,第二个子查询的结果发生变化,我不想发生这种情况。

2个查询与用于日期的变量完全相同。

第一个查询始终正常工作,但第二个查询似乎有时会受到第一个查询的影响。

如果我在第二个查询之前有第一个查询,那么它们将返回相同的日期。如果我在第一个查询之前有第二个查询,那么它有时会返回任何内容。

declare @curDate date = DATEADD(week,0,'2016/02/01')
declare @shortDate date = DATEADD(week,-2,@curDate)

SELECT -- Select Columns
    quere1.Location,
    quere1.Product,
    quere1.Short,
    MAX( quere1.Date ) [Date],
    MAX( quere1.Volume ) [Volume],
    MAX( quere1.Cost ) [Cost],
    MAX( quere2.Date ) [DateAgo],
    MAX( quere2.Volume ) [VolumeAgo],
    MAX( quere2.Cost ) [CostAgo]
FROM

    (SELECT --subQuery 1
        cst_mac_a.loc [Location],                 --Branch
        cst_mac_a.product [Product],             --Product
        pro_duct.desc4 [Short],
        MAX( cst_mac_a.datecreated ) [Date],  --Date
        MAX( cst_mac_a.ohvol ) [Volume],        --Volume
        MAX( cst_mac_a.ohextcost ) [Cost]     --Cost
    FROM cst_mac as cst_mac_a
        JOIN pro_type ON pro_type.proType = cst_mac_a.proType
        JOIN pro_duct ON pro_duct.proType = cst_mac_a.proType AND pro_duct.product = cst_mac_a.product
    WHERE
        cst_mac_a.protype = 'HW' AND

        cst_mac_a.datecreated in (SELECT MAX( sub.datecreated ) from cst_mac as sub WHERE 
                                    cst_mac_a.product = sub.product AND sub.datecreated <= @curDate) AND

        cst_mac_a.timecreated in (SELECT MAX( sub.timecreated ) from cst_mac as sub WHERE
                                    cst_mac_a.product = sub.product AND sub.datecreated in 
                                                (SELECT MAX( sub2.datecreated ) from cst_mac as sub2 WHERE 
                                                            sub2.datecreated <= @curDate and sub2.product = sub.product)) AND

        cst_mac_a.PROGRESS_RECID in (SELECT MAX( sub.PROGRESS_RECID ) from cst_mac as sub WHERE 
                                    cst_mac_a.product = sub.product AND sub.datecreated in 
                                                (SELECT MAX( sub2.datecreated ) from cst_mac as sub2 WHERE
                                                            sub2.datecreated <= @curDate AND sub2.product = sub.product)) AND

        cst_mac_a.ohvol <> 0 AND
        cst_mac_a.product = 'A4ROUP'
    GROUP BY
        cst_mac_a.loc,
        cst_mac_a.product,
        pro_duct.desc4)
    as quere1

    LEFT OUTER JOIN (SELECT --Basically same code, subquery 2
        cst_mac_a2.loc [Location],                 --Branch
        cst_mac_a2.product [Product],             --Product
        pro_duct.desc4 [Short],
        MAX( cst_mac_a2.datecreated ) [Date],  --Date
        MAX( cst_mac_a2.ohvol ) [Volume],        --Volume
        MAX( cst_mac_a2.ohextcost ) [Cost]     --Cost
    FROM cst_mac as cst_mac_a2
        JOIN pro_type ON pro_type.proType = cst_mac_a2.proType
        JOIN pro_duct ON pro_duct.proType = cst_mac_a2.proType AND pro_duct.product = cst_mac_a2.product
    WHERE
        cst_mac_a2.protype = 'HW' AND

        cst_mac_a2.datecreated in (SELECT MAX( sub3.datecreated ) from cst_mac as sub3 WHERE 
                                    cst_mac_a2.product = sub3.product AND sub3.datecreated <= @shortDate) AND

        cst_mac_a2.timecreated in (SELECT MAX( sub3.timecreated ) from cst_mac as sub3 WHERE
                                    cst_mac_a2.product = sub3.product AND sub3.datecreated in 
                                                (SELECT MAX( sub4.datecreated ) from cst_mac as sub4 WHERE 
                                                            sub4.datecreated <= @shortDate and sub4.product = sub3.product)) AND

        cst_mac_a2.PROGRESS_RECID in (SELECT MAX( sub3.PROGRESS_RECID ) from cst_mac as sub3 WHERE 
                                    cst_mac_a2.product = sub3.product AND sub3.datecreated in 
                                                (SELECT MAX( sub4.datecreated ) from cst_mac as sub4 WHERE
                                                            sub4.datecreated <= @shortDate AND sub4.product = sub3.product)) 
    GROUP BY
        cst_mac_a2.loc,
        cst_mac_a2.product,
        pro_duct.desc4)
    as quere2

    ON quere1.Product = quere2.Product AND quere1.Location = quere2.Location
GROUP BY
    quere1.Location,
    quere1.Product,
    quere1.Short
Order BY
    quere1.Location,
    quere1.Product,
    quere1.Short

示例输出:

curDate = Today
shortDate = 2 weeks ago:
Location | Product | Short | Date | Volume | Cost | DateAgo | VolumeAgo | CostAgo
Mill     | A4ROUP  | BN.   | 2/1/2016|40   | 36   |         |           |

curDate = 1 week ago
shortDate = 2 weeks ago
Location | Product | Short | Date | Volume | Cost | DateAgo | VolumeAgo | CostAgo
Mill     | A4ROUP  | BN.   |1/25/2016|27   | 25   |1/18/2016|   29      | 26

1 个答案:

答案 0 :(得分:0)

这就是LEFT JOIN的工作方式。它从第一个查询中获取所有行,但只从第二个查询中获取匹配的行(根据ON子句中的JOIN条件)。

如果要始终从两个查询中获取所有行,即使相反查询中没有匹配的行,也需要使用FULL OUTER JOIN。