S
S
sha-man2020-03-08 19:13:07
PostgreSQL
sha-man, 2020-03-08 19:13:07

How to filter data by counted count?

A simple query that runs without problems in MySql in Postgres gives an error. Request:

SELECT "t".*, 
(SELECT COUNT(*) FROM "user_archive_view" WHERE user_tariff_id = t.id) AS "count_archive"
  FROM "user_tariff" "t"  HAVING "count_archive"='100';


Error:
ERROR: column "count_archive" does not exist
LINE 3: FROM "user_tariff" "t" HAVING "count_archive"='100';

How to make it work?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Oleg Koltunov, 2020-03-08
@i_olega

HAVING is done before SELECT so it doesn't know anything about count_archive, here's a working one

SELECT "t".*, 
(SELECT COUNT(*) FROM "user_archive_view" WHERE user_tariff_id = t.id) AS "count_archive"
  FROM "user_tariff" "t"  
HAVING (SELECT COUNT(*) FROM "user_archive_view" WHERE user_tariff_id = t.id)  = '100';

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question