PostgreSQL – Counting Per Week Per Column

countgroup bypostgresql

Let's say I have a PostgreSQL table with 10 columns and 2 weeks (w1 w2) with 5 countries. Something that would look like this :

Data

What I would like to do is a count per week and per column like this :

Result

Best Answer

As count() counts only the non-NULL values, this is a simple GROUP BY query:

SELECT week, count(col1), ..., count(col10)
  FROM your_table
 GROUP BY week;