2016-05-23 10 views
0

特定の期間(1か月)に3つの製品カテゴリ(PRODUCTCATSテーブル)のいずれかから製品を購入(ARTRANSテーブル)したメンバー(MEMBERSテーブル)のリストを取得しようとしています。次の期間(月)にそのカテゴリから製品を購入していない。サブクエリから結果を除外する際の問題

両方のクエリは単独で動作しますが、それらをまとめても結果は得られません。助けてください!

SELECT MEMBERS.lname 
    , MEMBERS.fname 
    , MEMBERS.email 
    , PRODUCTS.scancode 
    , PRODUCTS.description 
    , ARTRANS.datetrans 
    , ARTRANS.memid 
    , ARTRANS.invoice 
    , ARTRANSITEMS.price 
    , ARTRANSITEMS.quantity 
FROM PRODUCTS 
INNER JOIN ARTRANSITEMS 
    ON PRODUCTS.productid = ARTRANSITEMS.productid 
INNER JOIN ARTRANS 
    ON ARTRANSITEMS.transid = ARTRANS.transid 
INNER JOIN PRODUCTCATS 
    ON PRODUCTS.productcatid = PRODUCTCATS.productcatid 
INNER JOIN MEMBERS 
    ON ARTRANS.memid = MEMBERS.memid 
     AND NOT EXISTS (
      SELECT M2.memid 
      FROM PRODUCTS AS P2 
      INNER JOIN ARTRANSITEMS AS ATI 
       ON P2.productid = ATI.productid 
      INNER JOIN ARTRANS AS ART2 
       ON ATI.transid = ART2.transid 
      INNER JOIN PRODUCTCATS AS PC2 
       ON P2.productcatid = PC2.productcatid 
      INNER JOIN MEMBERS AS M2 
       ON ART2.memid = M2.memid 
      WHERE (
        P2.productcatid = '20' 
        OR P2.productcatid = '21' 
        OR P2.productcatid = '78' 
        ) 
       AND (ART2.datetrans BETWEEN DATEADD(month, 1, @rvMonthOneStart) AND DATEADD(month, 1, @rvMonthOneEnd)) 
      ) 
WHERE (
     PRODUCTS.productcatid = '20' 
     OR PRODUCTS.productcatid = '21' 
     OR PRODUCTS.productcatid = '78' 
     ) 
    AND (ARTRANS.datetrans BETWEEN @rvMonthOneStart AND @rvMonthOneEnd) 
+0

あなたはARTRANSITEMS(クエリ)のメンバー(質問)またはリストのリストがありますか? – Serg

+1

クエリを区切って表示できますか? –

答えて

0

はこれを試してみてください....

SELECT MEMBERS.lname, MEMBERS.fname, MEMBERS.email, PRODUCTS.scancode 
     , PRODUCTS.description, ARTRANS.datetrans, ARTRANS.memid 
     , ARTRANS.invoice, ARTRANSITEMS.price, ARTRANSITEMS.quantity 
FROM PRODUCTS 
INNER JOIN ARTRANSITEMS ON PRODUCTS.productid = ARTRANSITEMS.productid 
INNER JOIN ARTRANS  ON ARTRANSITEMS.transid = ARTRANS.transid 
INNER JOIN PRODUCTCATS ON PRODUCTS.productcatid = PRODUCTCATS.productcatid 
INNER JOIN MEMBERS  ON ARTRANS.memid = MEMBERS.memid 
WHERE PRODUCTS.productcatid IN ('20' ,'21','78') 
    AND (ARTRANS.datetrans BETWEEN @rvMonthOneStart AND @rvMonthOneEnd) 
AND NOT EXISTS (SELECT 1 
       FROM PRODUCTS AS P2 
       INNER JOIN ARTRANSITEMS AS ATI ON P2.productid = ATI.productid 
       INNER JOIN ARTRANS  AS ART2 ON ATI.transid = ART2.transid 
       INNER JOIN PRODUCTCATS AS PC2 ON P2.productcatid = PC2.productcatid 
       INNER JOIN MEMBERS  AS M2 ON ART2.memid = M2.memid 
       WHERE PRODUCTS.productcatid IN ('20' ,'21','78') 
        AND (ART2.datetrans BETWEEN DATEADD(month, 1, @rvMonthOneStart) AND DATEADD(month, 1, @rvMonthOneEnd)) 
        AND M2.memid = MEMBERS.memid) 
関連する問題