2016-09-06 2 views
0

特定の時間範囲からCustomerNameInvoiceDateを選択するCustomerOrderCopyテーブルがあります。同じ結果は、指定された他の時間範囲内にあることはできません。SQLクエリを定式化する問題

どうすればこの問題を解決できますか?

SELECT CustomerName, InvoiceDate 
FROM CustomerOrderCopy 
WHERE (InvoiceDate BETWEEN '08.09.2016' AND '08.10.2016') 
AND (InvoiceDate NOT BETWEEN '08.11.2016' AND '09.06.2016') 
+0

AND NOT(InvoiceDate .11.2016' '08 BETWEEN AND '09 .06.2016' )? –

+0

@JanDoggen Ronnyは、除外範囲内で注文した顧客を除外したいと考えています。 – JohnHC

+0

テーブル構造とダミーデータでSQLフィーダを作成します。 http://sqlfiddle.com/ – mikeb

答えて

1
select C1.CustomerName, C1.InvoiceDate 
from CustomerOrderCopy C1 
left join CustomerOrderCopy C2 
    on C1.CustomerName = C2.CustomerName -- use ID if you have it 
    and C2.InvoiceDate between '08.11.2016' AND '09.06.2016' 
where C1.InvoiceDate between '08.09.2016' AND '08.10.2016' 

and C2.CustomerName is null -- This will exclude all those where there is a match 
3

EXISTS NOTを使用してください:

SELECT CustomerName, InvoiceDate 
    FROM CustomerOrderCopy 
    WHERE (InvoiceDate BETWEEN '08.09.2016' AND '08.10.2016') 
    AND NOT EXISTS (SELECT 1 
         FROM CustomerOrderCopy as t2 
         WHERE t2.CustomerName=CustomerName 
         AND (InvoiceDate BETWEEN '08.11.2016' AND '09.06.2016') 
        );