需要SQL嵌套逻辑

时间:2017-05-15 20:54:38

标签: sql

我有一种情况,我需要构建一个查询,拉出来给我一个成员列表,该成员在时间框架中使用了他们帐户中的项目,但在时间框架2中没有使用他们帐户中的项目。

SELECT DISTINCT SS.memid
FROM SS
INNER JOIN SSUSED ON SS.ssid = SSUSED.ssid
INNER JOIN MEMBERS AS MEMBERS_1 ON SS.memid = MEMBERS_1.memid
INNER JOIN PRODUCTS ON SS.productid = PRODUCTS.productid
INNER JOIN PRODUCTCATS ON PRODUCTS.productcatid = PRODUCTCATS.productcatid
INNER JOIN EMPLOYEES ON SSUSED.employeeid = EMPLOYEES.employeeid
    AND NOT EXISTS 
        (
            SELECT DISTINCT SS_1.memid
            FROM SS AS SS_1
            INNER JOIN SSUSED AS SSUSED_1 ON SS_1.ssid = SSUSED_1.ssid
            INNER JOIN MEMBERS AS MEMBERS_1 ON SS_1.memid = MEMBERS_1.memid
            INNER JOIN PRODUCTS AS PRODUCTS_1 ON SS_1.productid = PRODUCTS_1.productid
            INNER JOIN PRODUCTCATS AS PRODUCTCATS_1 ON PRODUCTS_1.productcatid = PRODUCTCATS_1.productcatid
            INNER JOIN EMPLOYEES AS EMPLOYEES_1 ON SSUSED_1.employeeid = EMPLOYEES_1.employeeid
            WHERE 
                MEMBERS_1.siteid = @rvSite
                AND SSUSED_1.usedate BETWEEN @rvStartWeek2
                AND @rvEndWeek2
                AND PRODUCTS_1.productcatid IN (27,28,29,58,77,75,30,61,31,32,47,68)
        )
WHERE MEMBERS_1.siteid = @rvSite
    AND SSUSED.usedate BETWEEN @rvStartWeek1
    AND @rvEndWeek1
    AND PRODUCTS.productcatid IN (27,28,29,58,77,75,30,61,31,32,47,68)

我遇到的问题是,当我以这种方式尝试查询时,我收到以下错误:

0 个答案:

没有答案