2014-05-17 106 views
0

enter image description here選擇不等於條件

如何僅在擴展價格不等於小計時才顯示invoicenumber?

下面的查詢是無法運行和錯誤組功能在這裏不允許使用

SELECT a.INVOICENUMBER,SUM(a.EXTENDEDPRICE),SUM(a.SUBTOTAL) 
FROM CUSTOMERORDERHISTORYVIEW a 
WHERE EXISTS 
(SELECT a1.INVOICENUMBER FROM CUSTOMERORDERHISTORYVIEW a1 WHERE 
a.INVOICENUMBER=a1.INVOICENUMBER AND SUM(a1.EXTENDEDPRICE) <> SUM(a1.SUBTOTAL)) 
GROUP BY a.INVOICENUMBER; 

回答

2

您正在尋找having條款:

SELECT a.INVOICENUMBER, SUM(a.EXTENDEDPRICE), SUM(a.SUBTOTAL) 
FROM CUSTOMERORDERHISTORYVIEW a 
GROUP BY a.INVOICENUMBER; 
HAVING SUM(a.EXTENDEDPRICE) <> SUM(a.SUBTOTAL);