Vertica使用NOT IN子句关联子查询

时间:2018-03-11 04:07:43

标签: vertica

我已将所有DDL和查询放在

下面的sqlfiddel链接中

http://sqlfiddle.com/#!9/89c76/2

同样的事情

create table players(name varchar(32),playerid int);
insert into players values("a",1);
insert into players values("b",2);
insert into players values("c",3);
insert into players values("d",4);
insert into players values("e",5);
insert into players values("f",6);

select * from players;

create table matches(playerid int,game varchar(32));
insert into matches values(1,"game1");
insert into matches values(2,"game1");
insert into matches values(3,"game1");
insert into matches values(1,"game2");
insert into matches values(2,"game2");
insert into matches values(3,"game2");
insert into matches values(4,"game3");
insert into matches values(5,"game2");

select * from matches;

commit;

,查询是

select p.playerid,m.game
    from players p, (select distinct game from matches) m
    where p.playerid not in (select playerid from matches where game=m.game)

我收到以下错误

[Vertica][VJDBC](2795) ERROR: Correlated subquery with NOT IN is not supported [SQL State=0A000, DB Errorcode=2795]

vertica doc中提到了同样的东西。

我怎样才能重写此查询?

我需要结果

Result:
    ------------------------
    game1 | 4,5,6  
    ------------------------
    game2 | 4,6
    ------------------------
    game3 | 1,2,3,5,6
    ------------------------
    ....

我在vertica论坛上发布了这个问题,并通过Kim_nicely找到了解决方案 https://forum.vertica.com/discussion/comment/240673#Comment_240673

select p.playerid, m.game from players p cross join (select distinct game from matches) m
minus
select * from matches
order by 1, 2;

1 个答案:

答案 0 :(得分:0)

根据查询,逻辑是让所有未参与游戏的玩家和未玩任何游戏的玩家参与。你也可以通过以下方式让玩家不在game1中。注意m.game ='game1'左边连接的区别。 我只能做一个游戏,因为新的要求需要在vertica中存储过程。我没有经验,但是sql。

    select 'game1', group_concat(t.playerid) playerid
    from ( 
    select  p.playerid 
    from players p 
    left join matches m 
    on p.playerid = m.playerid and m.game='game1'
    where m.playerid is null
    UNION
    select p.playerid 
    from players p 
    left join matches m 
    on p.playerid = m.playerid 
    where m.playerid is null) t