If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Select all years between two years

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-03-10, 21:46
gompuok gompuok is offline
Registered User
 
Join Date: Oct 2008
Posts: 12
Red face Select all years between two years

Hi all,

This might be a piece of cake question to those who expert in mysql but not for me. I've been trying to figure out whole day how to solve this problem but no luck.

How can I select all of the years between two years?

For example, given the start year 2007 and the end year 2010, I want to return:

2007
2008
2009
2010

your help are much appreciated.
Reply With Quote
  #2 (permalink)  
Old 01-03-10, 22:36
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
step 1, create a numbers table --
Code:
CREATE TABLE numbers (n INTEGER NOT NULL PRIMARY KEY);
INSERT INTO numbers (n) VALUES 
 (0),(1),(2),(3),(4),(5),(6),(7),(8),(9),...;
make sure there are as many numbers as you have for the largest gap of years

step 2, your query --
Code:
SELECT 2007 + n AS year
    FROM numbers
 WHERE 2007 + n <= 2010
you would likely parameterize the start and end years
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 01-04-10, 03:26
gompuok gompuok is offline
Registered User
 
Join Date: Oct 2008
Posts: 12
Thank you very much my friend. You solution works very well. God bless you.
Reply With Quote
  #4 (permalink)  
Old 01-06-10, 21:02
gompuok gompuok is offline
Registered User
 
Join Date: Oct 2008
Posts: 12
Hi R937,

Hope you can help me again. Here is my select statement (after modified yours);

SELECT 2007 + n as 'year', sales.total
FROM numbers
LEFT JOIN sales on sales.year = 2007 + n
WHERE 2007 + n <= 2010

so the result looks something like below;
year total
2007 1000
2008 1500
2009 800
2010 2000

My question is, i would like to calculate increase/decrease. What I want is the output should be exactly like below; (current year total - last year total)

year total_increase_decrease
2007 200
2008 500
2009 -700
2010 1200

your help are much appreciated.
Reply With Quote
  #5 (permalink)  
Old 01-07-10, 00:03
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
what application language are you using for this? php?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #6 (permalink)  
Old 01-07-10, 01:22
gompuok gompuok is offline
Registered User
 
Join Date: Oct 2008
Posts: 12
Actually I'm doing reporting using Pentaho. So, I'm directly using mysql syntax to pull out data from database.
Reply With Quote
  #7 (permalink)  
Old 01-07-10, 06:00
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by gompuok View Post
Actually I'm doing reporting using Pentaho. So, I'm directly using mysql syntax to pull out data from database.
the reason i asked is because current/previous row calculations are extremely convoluted if you try to do them with SQL

i've never used pentaho myself but from looking at their web site, i betcha it can do it
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #8 (permalink)  
Old 01-11-10, 22:23
gompuok gompuok is offline
Registered User
 
Join Date: Oct 2008
Posts: 12
I have been spending 2 days looking for a solution for this problem in pentaho but i dont think it's doable. I really appreciate if you can help me doing this in mysql..

Thanks so much again.
Reply With Quote
  #9 (permalink)  
Old 01-11-10, 22:35
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by gompuok View Post
I really appreciate if you can help me doing this in mysql..
i'll give it a shot

show me your exact query please
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #10 (permalink)  
Old 01-11-10, 23:17
gompuok gompuok is offline
Registered User
 
Join Date: Oct 2008
Posts: 12
Basicaly below is my exact query;
Code:
SELECT 2007 + n as 'year', sales.total
FROM numbers
LEFT JOIN sales on sales.year = 2007 + n
WHERE 2007 + n <= 2010
the output looks like below;

year total
2007 1000
2008 1500
2009 800
2010 2000

so basically i want to add one more column to my query called increase_decrease. So the final output later will become like below;

year total increase_decrease
2007 1000 1000
2008 1500 500
2009 800 -700
2010 2000 1200

Hope you can help me
Reply With Quote
  #11 (permalink)  
Old 01-12-10, 08:27
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
try this --
Code:
SET @start_year = 2004;
SET @end_year = 2010;

SELECT @start_year + n as 'year'
     , COALESCE(curr_year.total,0) AS curr_total
     , COALESCE(prev_year.total,0) AS prev_total
     , COALESCE(curr_year.total,0) -
       COALESCE(prev_year.total,0) AS increase_decrease
  FROM numbers
LEFT OUTER
  JOIN sales AS curr_year
    ON curr_year.year = @start_year + n
LEFT OUTER
  JOIN sales AS prev_year
    ON prev_year.year = @start_year + n - 1
 WHERE @start_year + n <= @end_year
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #12 (permalink)  
Old 01-12-10, 20:43
gompuok gompuok is offline
Registered User
 
Join Date: Oct 2008
Posts: 12
thanks a lot my friend..you are the man..it works very well!! thanks again.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On