我有两张桌子,
Invoice (ordernumber, status(which assign to InvoiceStatuses), etc)
和
InvoiteStatuses(status name, statusid)
我需要仅针对OrderNumber检查Invoice中的重复项,但是where status != statusID
(其中名称为“CANCELED”)
SELECT A.*
FROM Invoices A
JOIN InvoiceStatuses b
(
SELECT * FROM InvoiceStatuses
WHERE InvoiceStatus = 'CANCELLED'
) B
ON A.InvoiceStatus!=b.InvoiceStatusID
WHERE exists (SELECT OrderNumber
FROM Invoices c
WHERE a.OrderNumber = c.OrderNumber
GROUP BY OrderNumber
HAVING COUNT(*)>1)
答案 0 :(得分:0)
我认为您需要在查询中进行以下更改
JOIN子句过滤取消的状态out和where子句以仅检索重复的
SELECT A.*
FROM Invoices A
WHERE exists (SELECT OrderNumber
FROM Invoices c
JOIN InvoiceStatuses b
ON c.InvoiceStatus=b.InvoiceStatusID
AND InvoiceStatus != 'CANCELLED'
WHERE a.OrderNumber = c.OrderNumber
GROUP BY OrderNumber
HAVING COUNT(*)>1)