How many days a week (Monday, Tuesday...) do you have in a 1-month period?

Asked

Viewed 88 times

0

I need help to return in an SQL the amount you have of each day of the week in a period of 1 month, for example in the month 06/2021 has 4 Mondays,5 Tuesdays,5 Wednesdays...etc. I need to know the amount of every day of the week, because I need to average that amount. I tried several ways but it didn’t work, I need it to be in SQL Firebird. Thank you. Below this one of the forms I tried, it returns to every day of the week the amount of 560:

SELECT sum(iif(EXTRACT(WEEKDAY FROM N.DTEMISSAO) = 1, 1, 0)) qt_segunda,
        sum(iif(EXTRACT(WEEKDAY FROM N.DTEMISSAO) = 1, 1, 0)) qt_terca,
        sum(iif(EXTRACT(WEEKDAY FROM N.DTEMISSAO) = 1, 1, 0)) qt_quarta,
        sum(iif(EXTRACT(WEEKDAY FROM N.DTEMISSAO) = 1, 1, 0)) qt_quinta,
        sum(iif(EXTRACT(WEEKDAY FROM N.DTEMISSAO) = 1, 1, 0)) qt_sexta,
        sum(iif(EXTRACT(WEEKDAY FROM N.DTEMISSAO) = 1, 1, 0)) qt_sabado,
        sum(iif(EXTRACT(WEEKDAY FROM N.DTEMISSAO) = 1, 1, 0)) qt_domingo
FROM NOTA N
WHERE N.DTEMISSAO BETWEEN '01.06.2021' AND '30.06.2021'
  • make a group by by date (per day in your case)

  • Did not work, gave error: Cannot use an Aggregate Function in a GROUP BY clause.

  • @Ricardopunctual put your code as answer, if it doesn’t work just delete

  • what is the difference of the column dtemitted to outdate?

  • 1

    No, I wrote it wrong, I’ll fix it

1 answer

0


That’s not very simple because of some things:

  • It is necessary to group by dates, but there may be more than one record on the same date, so it is necessary to make a DISTINCT;
  • To make a DISTINCT, it is necessary to do in a subquery, ie first return all unique dates;
  • That one SUM doesn’t make sense, could be a COUNT, anyway it is necessary to separate the days, then a better opição would be to use a CASE WHEN.

That said, let’s go to the query:

select case 
         when extract(dow from dados.data)=0 THEN 'DOM'
         when extract(dow from dados.data)=1 THEN 'SEG'
         when extract(dow from dados.data)=2 THEN 'TER'
         when extract(dow from dados.data)=3 THEN 'QUA'
         when extract(dow from dados.data)=4 THEN 'QUI'
         when extract(dow from dados.data)=5 THEN 'SEX'
         when extract(dow from dados.data)=6 THEN 'SAB'
       end dia,
       count(extract(dow from dados.data)) tot
 from (select distinct dtemissao as data
        from nota) dados
group by extract(dow from dados.data)
order by extract(dow from dados.data);

Remarks:

  • To subquery select distinct dtemissao as data, called "date", will return the unique dates. Note that if your dates have time will need to remove the date to group correctly;
  • To identify the day of the week was used extract(dow from dados.data)=0, to learn more, see the documentation: https://www.postgresql.org/docs/8.1/functions-datetime.html
  • In the end, grouped by the same criterion extract(dow from dados.data) who made the COUNT.

You can see it working here: http://sqlfiddle.com/, insert some date for example

  • Thanks, it worked. I made some adjustments to work in SQL Firebird. Query below 
SELECT CASE EXTRACT(WEEKDAY FROM N.DIAS)
 WHEN 1 THEN 'SEGUNDA'
 WHEN 2 THEN 'TERCA'
 WHEN 3 THEN 'QUARTA'
 WHEN 4 THEN 'QUINTA'
 WHEN 5 THEN 'SEXTA'
 WHEN 6 THEN 'SABADO'
 WHEN 0 THEN 'DOMINGO'
 END SEMANA,
 COUNT(EXTRACT(WEEKDAY FROM N.DIAS)) QTDE
 FROM( SELECT DISTINCT DTEMISSAO AS DIAS
 FROM NOTA )N
WHERE N.DIAS BETWEEN '01.06.2021' AND '30.06.2021'
GROUP BY EXTRACT(WEEKDAY FROM N.DIAS)


  • good, do not forget to vote or mark the answer as accepted if solved your question :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.