开发者

Postgres query problem, need to select unique values

开发者 https://www.devze.com 2023-01-24 19:57 出处:网络
HI, I have the following query, but it\'s not quite doing what I expect it to do SELECT DISTINCT(c.id) AS \"CLIENT CODE\",c.name AS \"CLIENT NAME\", count(cmp.id) as \"NUMBER OF CAMPAIGNS ON LIVE AND

HI, I have the following query, but it's not quite doing what I expect it to do

SELECT DISTINCT(c.id) AS "CLIENT CODE",c.name AS "CLIENT NAME", count(cmp.id) as "NUMBER OF CAMPAIGNS ON LIVE AND PENDING" FROM clients AS c ,campaigns AS cmp WHERE cmp.clientid = c.id AND cmp.status NOT IN('S','C','X','?') GROUP BY c.name,c.id ORDER BY c.name;

What I want to achieve is the following. I have a clients table and a campaigns table in postgres. Clients have campaigns, so one client could have 100 campaigns. A client could have campaigns with status L,C, X or only L,C,P . There a开发者_StackOverflowre only L,P,C,X,S,? fot the statusses. Now I want the query to return only clients who have campaigns with status L and P and not the others.

So in other words only clients with campaign status L and P should be return, if the client has a X , C and L and P, it should not be returned.

Hope this makes sence and is possible


SELECT  *
FROM    clients
WHERE   id IN
        (
        SELECT  clientid
        FROM    campaigns
        WHERE   status IN ('L', 'P')
        )
        AND id NOT IN
        (
        SELECT  clientid
        FROM    campaigns
        WHERE   status NOT IN ('L', 'P')
        )
0

精彩评论

暂无评论...
验证码 换一张
取 消