A pupil can pass or fail in test.
Total Marks = 100.
0-30 marks means fail.
31-100 marks means passed exam.
Example:
I have table called pupil:
CREATE TABLE pupil
(id integer primary key,
marks integer,
maxmarks integer)
Now insert some values like
insert into pupil (id, marks, maxmarks) values (1,11,100),(2,21,100),(3, 60,100);
Now the table looks like this:
id | marks | maxmarks
----+-------+----------
1 | 11 | 100
2 | 21 | 100
3 | 60 | 100
I want to see how many have got between 0-30 and how many have got between 31-100 marks
So I try to think of using group by and then getting count.
I wrote this query:
select marks, count(*) from pupil group by marks;
which returns me this result:
marks | count
-------+-------
60 | 1
11 | 1
21 | 1
But I don't want that result, I am expecting this result.
marks | count
-------+-------
0-30 | 2
30-100 | 1
0-30 can be just 0 or 30 also, I don't care, I am interested in count column mostly.
I tried various group by and case clauses and also heard about something called "pivot", but I am not sure how to do this?
question from:
https://stackoverflow.com/questions/65846447/getting-count-and-group-by-in-psql-using-case-or-roundoff-and-pivot-based-on-a-p 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…