V
V
Viktoria Smirnova2019-07-01 09:28:38
PostgreSQL
Viktoria Smirnova, 2019-07-01 09:28:38

How to insert aggregate function Max and sort descending?

Guys good afternoon.
I make a request to get the number of 'no' from the new_1answers table for each question recorded in the new_question table . At the same time, it is not possible to implement a descending grouping and how to display a question with the maximum value (everywhere aggregate functions are given only in simple examples).

SELECT COUNT(a), q.title
FROM new_1answers a
     JOIN new_question q
ON a.id_question = q.id_question
WHERE a.a = 'no'
group by q.title

DDL tables:
create table new_1answers
(
  id_report   integer
    constraint new_1answers_id_report_fkey
      references new_1report,
  a           varchar(8),
  id_question integer
);

and
create table new_question
(
  id_question serial not null,
  title       text
);

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Konstantin Tsvetkov, 2019-07-01
@Vika7

SELECT COUNT(a), q.title
FROM new_1answers a
     JOIN new_question q
ON a.id_question = q.id_question
WHERE a.a = 'no'
group by q.title
order by 1 desc limit 1

A
Andrey, 2019-07-01
@VladimirAndreev

it will work exactly
but it will most likely work

select count(*) as _cnt, a.b_id as _id from a join b on a.b_id=b.id group by _id order by _cnt desc

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question