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')
)
精彩评论