使用子查询在同一表上左联接

时间:2018-07-13 07:58:10

标签: sql sql-server

select *
from (select *
    from TableO to
    where to.week='22'
    and to.year='2018'
    ) as O 
   left join TableO lto on O.user_id=lto.user_id
where O.user_id IS NULL

此查询出现问题。谁能看到错误所在?

我正在尝试在同一张桌子上保留联接,试图找到这一部分:

Venn Diagram showing a left join

2 个答案:

答案 0 :(得分:0)

这将为您提供在2018年订购的user_id,但自第22周以来没有订购

select user_id 
from   TableO
where  year = 2018
group by user_id 
having max(week) <= 22

答案 1 :(得分:0)

SELECT userid FROM TABLE1 
EXCEPT 
(SELECT userid FROM Table2 INTERSECT SELECT userid from table2)