简体   繁体   中英

Count similar entries in SQL

I have a table where a column for grade of students.
We have A+ and A and A- and B+ and B and B- .
Now we want to treat A+ and A and A- all equal to A . So as B+ and B and B- all equal to B .

How to write the expression to aggregate this? The question is count how many A and B? A+ and A- also mean A. ?

One way of many:

SELECT left(grade, 1) as base_grade, count(*) AS ct
FROM   tbl
GROUP  BY 1
ORDER  BY 1

left() requires Postgres 9.1+. Else use substring(grade, 1, 1) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM