How to make a select from a Night time period (6:00 pm to 6:00 p.m.)

4

Description: Count how many times the action occurred in the night time considering the time from 6:00 p.m. to 6:00 p.m.

There is a better way to do such a select without having to use or to compare the periods?

select count(*) as contador,log_desc,'Noite' from log
where extract(hour from log_data_hora)>=0 and extract(hour fromlog_data_hora)<6  
or  extract(hour from log_data_hora)>18 and extract(hour from log_data_hora)<=23  
group by log_desc,cor_cod

I'm using PostgreSQL.

    
asked by anonymous 03.03.2015 / 15:14

2 answers

4

You can use the BETWEEN operator in conjunction with NOT to do this:

select count(*) as contador,log_desc,'Noite' from log
where extract(hour from log_data_hora) NOT BETWEEN 6 AND 17
group by log_desc,cor_cod
    
03.03.2015 / 15:16
0

My friend, I do not know what db is using, but in the SQL standard Ansi the query would look like this.

select count(*) as contador,log_desc,'Noite' from log
where log_data_hora >= '2015-03-02 18:00:00.000' and log_data_hora <= '2015-03-03     06:00:00.000'
group by log_desc,cor_cod
    
03.03.2015 / 19:45