1

I have the following table as follows

date          hour     click
2015-02-27    00        7
2015-02-27    01        7
2016-02-27    02        7
2016-02-27    02        8
2016-02-27    03        7
2016-02-27    04        9
.             .         .
.             .         .
2016-02-27    23        9
.             .         .
.             .         .
.             .         .
.             .         .
2016-03-02    00        9
2016-02-02    01        5
2016-02-02    02        18

We store the date in one column in hour in another column.

I need to select the range of rows where i pass the from date and hour to date and hour. I had get it for two different dates as follows.

select b.date, b.hour, sum(b.click)
from clickinfo b
where (b.date ='2016-01-27' and b.hour>='02')
   or (b.date >'2016-01-27' and b.date <'2016-02-02')
   or (b.date='2016-02-02' and b.hour<=05)
group by b.`date`, b.hour;

but i am not able to select the range for same day date and different hours.

That is from date - '2016-01-27' and hour -'05' to date - '2016-01-27'and hour = 23

3
  • Seriously consider storing date and time as a single entity. Commented Mar 4, 2016 at 9:26
  • that is part of other project design. we only get the data what we need. Commented Mar 4, 2016 at 9:28
  • So efficiency isn't part of the equation? Commented Mar 4, 2016 at 10:18

2 Answers 2

1
create temporary table clickinfo (date1 varchar(50), hour1 varchar(50), click int);

insert into clickinfo (date1, hour1, click) values
('2015-02-27'    ,'00',        7)
,('2015-02-27'    ,'01',        7)
,('2015-02-27'    ,'05',        7)
,('2016-02-27'    ,'02',        7)
,('2016-02-27'    ,'02',        8)
,('2016-02-27'    ,'03',        7)
,('2016-02-27'    ,'04',        9)
,('2016-02-27'    ,'05',        9);


select
  date_add(date1 ,  interval hour1 hour), sum(click) from clickinfo
where
  date_add(date1 ,  interval hour1 hour)
      between str_to_date('2015-02-27 5:0', '%Y-%m-%d %H:%i')
              and str_to_date('2016-02-28 23:0', '%Y-%m-%d %H:%i')
group by
  date_add(date1 ,  interval hour1 hour);
Sign up to request clarification or add additional context in comments.

1 Comment

hey can u please check your query . i am still not able to get the rows always showing empty rows.
1

You can just add another or condition on case equal date of start and end:

select b.date, b.hour, sum(b.click)
from clickinfo b
where (b.date ='2016-01-27' and b.hour>='02')
   or (b.date >'2016-01-27' and b.date <'2016-02-02')
   or (b.date='2016-02-02' and b.hour<=05)
   or ('2016-01-27' == '2016-02-02' and b.hour>='02' and b.hour<=05)
group by b.`date`, b.hour;

2 Comments

What about for same date with different hours.
last or in the where section do this for you ('start_date' == 'finish_date' and b.hour>='start_hour' and b.hour<='finish_hour')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.