在下面的查询中,我想根据最大版本对产品的特定日期范围之间的BatchReleaseSourceProductDetails的批准数量求和。我试过但我无法得到结果.pls帮助我这样做。
表BatchReleaseSourceProductDetails:
BatchReleaseSourceProductDetailID |BatchReleaseID |SourceProductID|ApprovedQty|CreatedOn|VersionID
1|10|15|10|21-02-2015|0
1|10|15|15|21-02-2015|1
表2: BatchRelease:
BatchReleaseID|LocationID
10|1
The result should be 15 for date range 21-02-2015 and 21-02-2015
SELECT @nSouceProductQuantity = Isnull(Sum(Isnull(ApprovedQuantity, 0.0)), 0.0)
FROM BatchReleaseSourceProductDetails BRSD
LEFT OUTER JOIN BatchRelease BR
ON BR.BatchReleaseID = BRSD.BatchReleaseID
WHERE ( SourceProductID = @i_ProductID
AND BR.LocationID = @i_LocationID
AND Cast(BRSD.CreatedOn AS DATE) >= @i_StartDate
AND Cast(BRSD.CreatedOn AS DATE) <= @i_EndDate
AND BRSD.VersionID = (SELECT Max(VersionID)
FROM BatchRelease
WHERE SourceProductID = @i_ProductID
AND BR.LocationID = @i_LocationID
AND Cast(BRSD.CreatedOn AS DATE) >= @i_StartDate
AND Cast(BRSD.CreatedOn AS DATE) <= @i_EndDate) )
答案 0 :(得分:0)
进行一些故障排除的时间...... 首先运行最简单的查询:
SELECT @nSouceProductQuantity = Isnull(Sum(Isnull(ApprovedQuantity, 0.0)), 0.0)
FROM BatchReleaseSourceProductDetails BRSD
取回0以外的东西?如果没有解决问题(在获得所需结果之前,请为每个步骤执行此操作)。完成了吗?好的,继续前进:
SELECT @nSouceProductQuantity = Isnull(Sum(Isnull(ApprovedQuantity, 0.0)), 0.0)
FROM BatchReleaseSourceProductDetails BRSD
LEFT OUTER JOIN BatchRelease BR
ON BR.BatchReleaseID = BRSD.BatchReleaseID
取回0以外的东西?好的,继续前进:
SELECT @nSouceProductQuantity = Isnull(Sum(Isnull(ApprovedQuantity, 0.0)), 0.0)
FROM BatchReleaseSourceProductDetails BRSD
LEFT OUTER JOIN BatchRelease BR
ON BR.BatchReleaseID = BRSD.BatchReleaseID
WHERE SourceProductID = 15
取回0以外的东西?好的,继续前进:
SELECT @nSouceProductQuantity = Isnull(Sum(Isnull(ApprovedQuantity, 0.0)), 0.0)
FROM BatchReleaseSourceProductDetails BRSD
LEFT OUTER JOIN BatchRelease BR
ON BR.BatchReleaseID = BRSD.BatchReleaseID
WHERE SourceProductID = 15
AND BR.LocationID = 1
取回0以外的东西?好的,继续前进:
SELECT @nSouceProductQuantity = Isnull(Sum(Isnull(ApprovedQuantity, 0.0)), 0.0)
FROM BatchReleaseSourceProductDetails BRSD
LEFT OUTER JOIN BatchRelease BR
ON BR.BatchReleaseID = BRSD.BatchReleaseID
WHERE SourceProductID = 15
AND BR.LocationID = 1
AND Cast(BRSD.CreatedOn AS DATE) >= '21-02-2015'
取回0以外的东西?好的,继续前进:
SELECT @nSouceProductQuantity = Isnull(Sum(Isnull(ApprovedQuantity, 0.0)), 0.0)
FROM BatchReleaseSourceProductDetails BRSD
LEFT OUTER JOIN BatchRelease BR
ON BR.BatchReleaseID = BRSD.BatchReleaseID
WHERE SourceProductID = 15
AND BR.LocationID = 1
AND Cast(BRSD.CreatedOn AS DATE) >= '21-02-2015'
AND Cast(BRSD.CreatedOn AS DATE) >= '21-02-2015'
取回0以外的东西?好的,继续前进:
SELECT @nSouceProductQuantity = Isnull(Sum(Isnull(ApprovedQuantity, 0.0)), 0.0)
FROM BatchReleaseSourceProductDetails BRSD
LEFT OUTER JOIN BatchRelease BR
ON BR.BatchReleaseID = BRSD.BatchReleaseID
WHERE SourceProductID = 15
AND BR.LocationID = 1
AND Cast(BRSD.CreatedOn AS DATE) >= '21-02-2015'
AND Cast(BRSD.CreatedOn AS DATE) >= '21-02-2015'
AND BRSD.VersionID = 1
如果到目前为止一切正常,则问题出在子查询或变量中。以相同的方式对它们进行故障排除。对于子查询,使其变得非常简单,然后逐渐添加WHERE子句,直到您有完整的查询。在那之后,当你在这里时:
SELECT @nSouceProductQuantity = Isnull(Sum(Isnull(ApprovedQuantity, 0.0)), 0.0)
FROM BatchReleaseSourceProductDetails BRSD
LEFT OUTER JOIN BatchRelease BR
ON BR.BatchReleaseID = BRSD.BatchReleaseID
WHERE SourceProductID = 15
AND BR.LocationID = 1
AND Cast(BRSD.CreatedOn AS DATE) >= '21-02-2015'
AND Cast(BRSD.CreatedOn AS DATE) >= '21-02-2015'
AND BRSD.VersionID = (SELECT Max(VersionID)
FROM BatchRelease
WHERE SourceProductID = 15
AND BR.LocationID = 1
AND Cast(BRSD.CreatedOn AS DATE) >= '21-02-2015'
AND Cast(BRSD.CreatedOn AS DATE) <= '21-02-2015')
开始一次更换一个变量,直到找到问题的根源。