A
A
AlmazKayum2020-07-12 15:19:37
PostgreSQL
AlmazKayum, 2020-07-12 15:19:37

How to make a SQL fetch?

Hello. The comments
table has a task_id numeric field that stores non-unique entries. You need to make a unique selection of all task_id entries that have the same value less than 3 . Example: if the task_id contains such entries (1, 1, 2, 3, 3, 3, 4, 5, 5, 5, 5), then you need to get (1, 2, 4) the values ​​3 and 5 are not included, because entries 3 and 5 are greater than three. I tried to solve it like this, but it's definitely wrong. because it returns an empty tuple


SELECT task_id FROM comments GROUP BY task_id HAVING COUNT(task_id)<=3

Can you please tell me how to build a SQL construct?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dimonchik, 2020-07-12
@dimonchik2013

SELECT task_id, COUNT(task_id) as ct FROM comments GROUP BY task_id HAVING ct<=3

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question