简体   繁体   中英

SQL How to correctly use the where operator in my query?

There is a request for 15 minute intervals in the interval from 09.00 to 18.00 hours.

SELECT t.event_date,             
case                                                                         
  when TIME_TO_SEC(t.event_date) > inter.begin AND TIME_TO_SEC(t.event_date) < inter.end                           
  then floor((TIME_TO_SEC(t.event_date) - inter.begin) / inter.width)           

  when TIME_TO_SEC(t.event_date) <= inter.begin 
  then 0  

  when TIME_TO_SEC(t.event_date) >= inter.end 
  then floor((inter.end - inter.begin) / inter.width)  

  else null 

end as full_interval_number       
FROM table t,                     
    (select TIME_TO_SEC('09:00:00') as begin,  
            TIME_TO_SEC('18:00:00') as end,    
            TIME_TO_SEC('00:15:00') as width
     ) inter 

How to use WHERE to exclude temporary intrevalues №0, 3, 7, 15 or intrevalues from 09.00 to 10.00 hours?

Something like this:

where
       TIME_TO_SEC(event_date) < TIME_TO_SEC('09:00:00') 
and TIME_TO_SEC(event_date) > TIME_TO_SEC('10:00:00') 

or:

where
 full_interval_number not in (0, 3, 7, 15)

Is this what you want? The first part is the general limit for 9-18 and the second part is the specific requirement to exclude values between 9-10

where TIME_TO_SEC(event_date) between TIME_TO_SEC('09:00:00') and TIME_TO_SEC('18:00:00')
and not (TIME_TO_SEC(event_date) between TIME_TO_SEC('09:00:00') and TIME_TO_SEC('10:00:00'))

Full query

SELECT t.event_date,             
case                                                                         
  when TIME_TO_SEC(t.event_date) > inter.begin AND TIME_TO_SEC(t.event_date) < inter.end                           
  then floor((TIME_TO_SEC(t.event_date) - inter.begin) / inter.width)           

  when TIME_TO_SEC(t.event_date) <= inter.begin 
  then 0  

  when TIME_TO_SEC(t.event_date) >= inter.end 
  then floor((inter.end - inter.begin) / inter.width)  

  else null 

end as full_interval_number       
FROM table t,                     
(select TIME_TO_SEC('09:00:00') as begin,  
        TIME_TO_SEC('18:00:00') as end,    
        TIME_TO_SEC('00:15:00') as width
 ) inter 
where TIME_TO_SEC(event_date) between TIME_TO_SEC('09:00:00') and TIME_TO_SEC('18:00:00')
and not (TIME_TO_SEC(event_date) between TIME_TO_SEC('09:00:00') and TIME_TO_SEC('10:00:00'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM