Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select records between values of two columns

I have following table which saves sales data for specific week of a year.

Sales

week | year | value
50   | 2011 | 150
51   | 2011 | 151
52   | 2011 | 152
1    | 2012 | 170
2    | 2012 | 190
3    | 2012 | 180
4    | 2012 | 160
5    | 2012 | 170
6    | 2012 | 290

I want to find records which lies between week1 of year1 and week2 of year2.

for example

  1. between week 50 of 2011 to week 5 of 2012
  2. between week 1 of 2012 to week 6 of 2012

Having little experience in sQL, I tried following query which works for case 2 but not for case 1.

SELECT * from sales WHERE (week >= 1 AND year >= 2012) AND (week <= 6 AND year <= 2012 ) 

Looks like grouping of condition does not work as I understand. It takes as if there are no grouping of conditions.

like image 876
Amit Patel Avatar asked Dec 31 '25 03:12

Amit Patel


1 Answers

select *
from
(
   Select *, year*100 + week as yearweek
   from table
) v
where yearweek between 201150 and 201205
like image 137
podiluska Avatar answered Jan 02 '26 17:01

podiluska



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!