SQL Logic用于隔离列表一但不列表二的记录

时间:2017-05-16 17:26:29

标签: sql sql-server-2008

好的,我正在开发一个查询,可以隔离在其帐户上使用服务的成员两周时间(例如4/1到4/14),然后将其与使用服务的成员列表进行比较在接下来的两周时间内(4/15到4 /),然后确定谁在第一阶段使用了服务,然后在第二阶段没有使用服务。

获取第一周列表的查询可以正常使用:

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
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))

第二周的查询如下:

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
WHERE        (MEMBERS_1.siteid = @rvSite) AND (SSUSED.usedate BETWEEN @rvStartWeek2 AND @rvEndWeek2) AND (PRODUCTS.productcatid IN (27, 28, 29, 58, 77, 75, 30, 
                         61, 31, 32, 47, 68))

我尝试将这两者与“NOT EXISTS”子句放在FROM部分和where部分中,但两者都有问题。任何人都可以建议吗?

1 个答案:

答案 0 :(得分:1)

您可以使用except返回第一个查询中不在第二个查询中的所有不同行:

SELECT 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
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))

except

SELECT 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
WHERE        (MEMBERS_1.siteid = @rvSite) AND (SSUSED.usedate BETWEEN @rvStartWeek2 AND @rvEndWeek2) AND (PRODUCTS.productcatid IN (27, 28, 29, 58, 77, 75, 30, 
                         61, 31, 32, 47, 68))

使用common table expressionnot exists()

的备选方案
;with cte as (
SELECT 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
WHERE        (MEMBERS_1.siteid = @rvSite) AND (SSUSED.usedate BETWEEN @rvStartWeek2 AND @rvEndWeek2) AND (PRODUCTS.productcatid IN (27, 28, 29, 58, 77, 75, 30, 
                         61, 31, 32, 47, 68))
)
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
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))
  and not exists (
    select 1
    from cte
    where ss.memid = cte.memid
    )