0

My problem is to write a query (PostgreSQL) to get a 'propno' where 'newsname' is not equal 'NYT' (the answer is 40).

       newsname       | propno | dateadvert | cost 
----------------------+--------+------------+------
 NYT                  |     30 | 2018-01-01 | 10.2
 NYT                  |     30 | 2018-01-10 | 15.2
 NYT                  |     10 | 2018-01-02 | 20.2
 NYT                  |     20 | 2018-02-01 | 10.2
 Guardian             |     40 | 2018-02-10 |  100
 Guardian             |     10 | 2018-01-02 | 13.2
 Guardian             |     30 | 2018-01-10 | 10.8

Thanks for help

3
  • 2
    Do you want the maximum propno? Otherwise the result was 40, 10 and 30. Commented Dec 28, 2019 at 23:20
  • 1
    Why did you put in sqlite and mariadb tags if this is for postgres? Commented Dec 28, 2019 at 23:24
  • Nah, you see that NYT already has 10 and 30. I need to query which would print 40 because the 'NYT' does not have 40. Commented Dec 28, 2019 at 23:24

3 Answers 3

1

You can use aggregation and having:

select propno
from t
group by propno
having count(*) filter (where newsname = 'NYT') = 0;

If you have a separate table with one row per propno, then I would recommend not exists:

select p.propno
from props p
where not exists (select 1
                  from t
                  where t.propno = p.propno and
                        t.newname = 'NYT'
                 );
Sign up to request clarification or add additional context in comments.

Comments

0

You can use exists:

select propno
from props p
where not exists (
  select 1
  from props t
  where t.propno = p.propno
  and t.newname = 'NYT'
);

Comments

0

HAVING could be used to filter out 'NYT':

SELECT propno
FROM tab_name
GROUP BY propno
HAVING SUM(newsname='NYT')=0;

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.