我尝试了几种不同的方法来消除重复。我已经提交了查询结果,并显示了最终结果。
Page.MaintainScrollPositionOnPostback = true
我在上面查询列表中的结果:
WITH CTE AS (
SELECT DISTINCT
a.SiteID
,a.SiteDescription
,iv.VendorNumber
,iv.VendorName
,mrh.DateReceived
,mrh.DocumentNumber
,mrh.InvoiceDate
,mrh.InvoiceNumber
,i.StockNumber
,i.StockDescription
,ia.UnitsPerPack
,mrh.ReceivingComment
,ia.BrokenQuantityAdjustment
,ia.BasePackCost
,mri.BrokenQuantityAdjusted
,ia.fee
FROM AdmSites a, InvVendors iv, InvItems i, InvManualReceivingHeader mrh, InvAdjustments ia, InvManualReceivingItems mri, InvManualReceivingItemsAdjustments ria
WHERE a.AdmSiteID = mrh.AdmSiteId
AND a.AdmSiteID = ia.AdmSiteID
AND i.InvItemID = ia.InvItemID
AND iv.InvVendorID = ia.InvVendorID
AND mrh.DateReceived = ia.AdjustmentDate
AND ria.DateReceived = mrh.DateReceived
AND ia.InvoiceNumber = mrh.InvoiceNumber
AND mrh.InvoiceNumber = 'deleted'
AND ia.InvoiceDate BETWEEN '2017-05-01' AND '2018-08-22'
AND (ia.InvoiceNumber LIKE '%' + ia.InvoiceNumber + '%' OR ia.InvoiceNumber IS NULL)
)
SELECT t1.*, t2.PackQty, t1.BasePackCost * t2.PackQty as TotalCost, t1.BasePackCost * t2.PackQty as TotalValue
FROM CTE t1 INNER JOIN
(SELECT SUM(BrokenQuantityAdjustment)PackQty, BasePackCost
FROM CTE
GROUP BY BasePackCost
) t2 on t1.BasePackCost = t2.BasePackCost
ORDER BY StockNumber, BasePackCost
如果可以使此查询正常工作,则为预期结果:
SiteID SiteDescription VendorNumber VendorName DateReceived DocumentNumber InvoiceDate InvoiceNumber StockNumber StockDescription UnitsPerPack ReceivingComment BrokenQuantityAdjustment BasePackCost BrokenQuantityAdjusted fee PackQty TotalCost TotalValue
-------- ----------------- -------------- ------------ -------------- ---------------- ------------- --------------- ------------- --------------------------- -------------- ------------------ -------------------------- -------------- ------------------------ ----- --------- ----------- ------------
69 Madison High 4 DAIRY 05/03/18 939 06/04/18 deleted 1451 MILK WHITE 1% 1 -161 0.1615 0 0 89 14.3735 14.3735
69 Madison High 4 DAIRY 05/03/18 939 06/04/18 deleted 1451 MILK WHITE 1% 1 250 0.1615 0 0 89 14.3735 14.3735
69 Madison High 4 DAIRY 05/03/18 939 06/04/18 deleted 1451 MILK WHITE 1% 1 -89 0.1617 0 0 -89 -14.3913 -14.3913
69 Madison High 4 DAIRY 05/03/18 939 06/04/18 deleted 1580 MILK LACTAID FREE SKIM PF 1 -20 0.6185 0 0 0 0 0
69 Madison High 4 DAIRY 05/03/18 939 06/04/18 deleted 1580 MILK LACTAID FREE SKIM PF 1 20 0.6185 0 0 0 0 0
这是创建表并插入数据的代码,如果有帮助的话。
SiteID SiteDescription VendorNumber VendorName DateReceived DocumentNumber InvoiceDate InvoiceNumber StockNumber StockDescription UnitsPerPack ReceivingComment BrokenQuantityAdjustment BasePackCost BrokenQuantityAdjusted fee PackQty TotalCost TotalValue
-------- ---------------- -------------- ------------ -------------- ---------------- ------------- --------------- ------------- --------------------------- -------------- ------------------ -------------------------- -------------- ------------------------ ----- --------- ----------- ------------
69 Madison High 4 DAIRY 05/03/18 939 06/04/18 deleted 1451 MILK WHITE 1% 1 -161 0.1615 0 0 89 14.3735 14.3735
69 Madison High 4 DAIRY 05/03/18 939 06/04/18 deleted 1451 MILK WHITE 1% 1 -89 0.1617 0 0 -89 -14.3913 -14.3913
69 Madison High 4 DAIRY 05/03/18 939 06/04/18 deleted 1580 MILK LACTAID FREE SKIM PF 1 -20 0.6185 0 0 0 0 0
答案 0 :(得分:1)
我并没有为您的查询全部写出来,但这是您要排除所有在“ Field3”上重复的记录的方法
ElementRef
看起来您实际上想要在此字段上具有重复项的记录,而您只想要其中之一。
MockElementRef
答案 1 :(得分:0)
由于您将CTE与另一张表一起加入,因此您得到的重复项:
SELECT t1.*, t2.PackQty, t1.BasePackCost * t2.PackQty as TotalCost, t1.BasePackCost * t2.PackQty as TotalValue
FROM CTE t1 INNER JOIN
(SELECT SUM(BrokenQuantityAdjustment)PackQty, BasePackCost
FROM CTE
GROUP BY BasePackCost
) t2 on t1.BasePackCost = t2.BasePackCost
ORDER BY StockNumber, BasePackCost
尽管CTE中的行是不同的,除非您可以保证与内部联接子查询的1x1映射,否则您将获得重复项。