从子查询中排除结果的问题

时间:2016-05-23 20:10:36

标签: sql sql-server

我正在尝试检索在指定期间(一个月)内从三种产品类别(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)

1 个答案:

答案 0 :(得分:0)

试试这个......

dpt2[key_t] = dpt2.get(key_t, 0) + rat