好的,我正在开发一个查询,可以隔离在其帐户上使用服务的成员两周时间(例如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部分中,但两者都有问题。任何人都可以建议吗?
答案 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 expression和not 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
)