删除SQL中的重复项

时间:2017-06-14 22:11:08

标签: sql sql-server duplicates inner-join having

我需要选择尚未开具发票的所有订单      and BR.isInvoiced = 0

Select DG.DateGenerated, oit.OrderIdentifier, BR.isInvoiced, BR.hash, BR.OrderNumber, UL.FirstName, UL.LastName from [Order] O INNER JOIN BillingReport BR ON O.OrderNumber = BR.OrderNumber INNER JOIN UsersLib UL ON UL.UserID = O.UserId INNER JOIN Order_Identifier_Transactional OIT ON O.OrderNumber = OIT.OrderNumber INNER JOIN (Select OP.OrderNumber, OP.DateGenerated from OrderProducts OP group by OP.OrderNumber, op.DateGenerated having op.DateGenerated in (select MAX(op.DateGenerated) from orderproducts ))as DG ON DG.OrderNumber = O.OrderNumber where O.Status = 'COMPLETED' and BR.isInvoiced = 0 and UL.UserName not like '%Support%' and UL.AccountNumber != '10000001001' and BR.invoicepdf is NULL and BR.ispaid = 0

然后仅选择每个订单号生成的最新日期 但是使用这个查询我仍然会发现一些像这样的重复:

enter image description here

4 个答案:

答案 0 :(得分:1)

这是解决问题的最快方法:

SELECT MAX(DateGenerated), OrderIdentifier, isInvoiced, hash, OrderNumber, 
    FirstName, LastName
FROM (Select DG.DateGenerated, oit.OrderIdentifier, BR.isInvoiced, BR.hash, 
    BR.OrderNumber, UL.FirstName, UL.LastName   from [Order] O
    INNER JOIN BillingReport BR
    ON O.OrderNumber = BR.OrderNumber
    INNER JOIN UsersLib UL
    ON UL.UserID = O.UserId
    INNER JOIN Order_Identifier_Transactional OIT
    ON O.OrderNumber = OIT.OrderNumber
    INNER JOIN
     (Select OP.OrderNumber, OP.DateGenerated from OrderProducts OP 
     group by OP.OrderNumber, op.DateGenerated
      having op.DateGenerated in (select MAX(op.DateGenerated) from orderproducts ))as DG
    ON DG.OrderNumber = O.OrderNumber
    where O.Status = 'COMPLETED' 
    and BR.isInvoiced = 0
    and UL.UserName not like '%Support%' 
    and UL.AccountNumber != '10000001001' 
    and BR.invoicepdf is NULL 
    and BR.ispaid = 0)
GROUP BY OrderIdentifier, isInvoiced, hash, OrderNumber, FirstName, LastName

我接受了您的整个查询,将其包装在引号中,然后使用围绕DateGenerated的MAX聚合函数从中进行选择。最后,我为所有其他字段添加了GROUP BY子句。

答案 1 :(得分:1)

你的MAX选择中有别名

(select MAX(op.DateGenerated) from orderproducts )

请改为尝试:

(select MAX(DateGenerated) from orderproducts where ordernumber = op.ordernumber )

答案 2 :(得分:0)

您是否尝试在第二个UNIQUE上使用INNER JOIN

INNER JOIN
     (Select UNIQUE OP.OrderNumber, OP.DateGenerated
      from OrderProducts OP 
      group by OP.OrderNumber, op.DateGenerated
      having op.DateGenerated in
            (select MAX(op.DateGenerated)
             from orderproducts )) as DG
ON DG.OrderNumber = O.OrderNumber

答案 3 :(得分:0)

public void launchGoogleChrome(View view) {
        Intent launchGoogleChrome = getPackageManager().getLaunchIntentForPackage("com.android.chrome");
        startActivity(launchGoogleChrome);
    }

找到最近生成日期时,您遗失Select DG.DateGenerated, oit.OrderIdentifier, BR.isInvoiced, BR.hash, BR.OrderNumber, UL.FirstName, UL.LastName from [Order] O INNER JOIN BillingReport BR ON O.OrderNumber = BR.OrderNumber INNER JOIN UsersLib UL ON UL.UserID = O.UserId INNER JOIN Order_Identifier_Transactional OIT ON O.OrderNumber = OIT.OrderNumber INNER JOIN (Select OP.OrderNumber, OP.DateGenerated from OrderProducts OP group by OP.OrderNumber, op.DateGenerated having op.DateGenerated in (select MAX(op.DateGenerated) from orderproducts OP1 WHERE OP.OrderNumber = OP1.OrderNumber))as DG ON DG.OrderNumber = O.OrderNumber where O.Status = 'COMPLETED' and BR.isInvoiced = 0 and UL.UserName not like '%Support%' and UL.AccountNumber != '10000001001' and BR.invoicepdf is NULL and BR.ispaid = 0 。我希望它会对你有所帮助。