I only want to count people below a specific number in SQL by month

Asked 2 years ago, Updated 2 years ago, 39 views

I am a beginner in SQL.
I'm sorry for the rudimentary question, but I'd appreciate it if you could let me know.

I'd like to count only people with number 90 or less by month.
Could you tell me the syntax?

For example images, I would like information such as 20180823 is 3, 20180930 is 2, and so on.

sql

2022-09-30 19:08

1 Answers

create table mytable(
    datetimesend date,
    id char(1),
    number integer
);

insert into mytable (datetimesend, id, number) values 
    ('2018-08-23', 'a', 34),
    ('2018-08-23', 'b', 23),
    ('2018-08-23', 'c', 95),
    ('2018-08-23', 'd', 78),
    ('2018-09-30', 'e', 50),
    ('2018-09-30', 'f', 6),
    ('2018-09-30', 'g', 99')
;

I think you can simply specify it with the where clause, but what do you think?

select sum(number) as sum, year(datetimesend) as year, month(datetimesend) as month
from mytable where number <=90 group by year, month;


2022-09-30 19:08

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.