1

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

4
  • If a client has only L or only P, should they be returned? Commented Nov 11, 2010 at 17:56
  • @Quassnoi - If a client has L and P then they should be returned, but if a client has L,P and C then they should not be returned. C,L,P are status values, status is a column in the campaigns table. Commented Nov 11, 2010 at 18:52
  • you did not answer my question. What if client has only L? Commented Nov 11, 2010 at 18:58
  • @Quassnoi, appologies, did not read it properly, then it shoul be returned Commented Nov 11, 2010 at 19:00

1 Answer 1

4
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')
        )
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.