我有两个子查询,其中基于第一个子查询,第二个子查询的结果发生了我不希望发生的变化。
这两个查询完全相同,只是它们用于日期的变量不同。
第一个查询总是正确工作的,但第二个查询似乎有时会受到第一个查询的影响。
如果我的第一个查询先于第二个查询,那么它们都将返回相同的日期。如果我在第一个查询之前有第二个查询,那么它有时什么也不会返回。
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 | 26https://stackoverflow.com/questions/44419334
复制相似问题