Home > database >  SQL / SQLite Query that counts the number of text occurrences based on date
SQL / SQLite Query that counts the number of text occurrences based on date

Time:04-29

Below I have an example of a query that I am trying to perform. The INPUT is the table that I have access to. The OUTPUT is how I would like to organize the data. I am trying to group the data by the date and have an output for the number ups and the number of downs, so the output will give a string, int, int.

This query is a little more advanced than I've done before, but I think I will need something similar to this:

SELECT date, COUNT(*), COUNT(*) FROM Input GROUP BY date

I am not sure how to format the COUNTs to look for only UP or DOWN on a particular date. Could someone help point me in the right direction

INPUT

date time status
2022-01-01 12:12:12 UP
2022-01-01 13:12:12 DOWN
2022-01-01 14:12:12 UP
2022-02-04 12:12:12 UP
2022-02-04 13:12:12 DOWN
2022-02-04 14:12:12 DOWN
2022-03-05 12:12:12 UP
2022-03-05 13:12:12 UP
2022-03-05 14:12:12 DOWN

OUTPUT

date # of UP # of DOWN
2022-01-01 2 1
2022-02-04 1 2
2022-03-05 2 1

CodePudding user response:

You can try sum with a conditional case expression

select date, 
  Sum(case when status='Up' then 1 else 0 end) NumUps, 
  Sum(case when status='Down' then 1 else 0 end) NumDowns
from t
group by date;
  • Related