MySQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesMySQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 6th, 2012, 01:27 PM
annihilate's Avatar
annihilate annihilate is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: Surrey, UK
Posts: 224 annihilate User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 6 h 12 m 49 sec
Reputation Power: 10
Help with where clause

Hi

I am struggling to work out how to write a specific query. This is what I have so far:

sql Code:
Original - sql Code
  1. SELECT n.show_id, n.show_name, n.STATUS,
  2. MAX(s.season) AS max_season, s.date_added AS max_season_added
  3. FROM tv_show_names AS n
  4. LEFT JOIN tv_seasons AS s ON s.show_id = n.show_id
  5. WHERE n.STATUS != 'END'
  6. GROUP BY n.show_id
  7. ORDER BY n.show_name

This seems to be showing what I expect. i.e. it is showing all TV shows where the status is not END and its showing the highest season number for that show and the date that season was added.

What I want to do is restrict the results to only show TV shows where the date added of the highest season number for that show is more than a year ago. So I tried the following but this is not giving the required results.

sql Code:
Original - sql Code
  1. SELECT n.show_id, n.show_name, n.STATUS,
  2. MAX(s.season) AS max_season, s.date_added AS max_season_added
  3. FROM tv_show_names AS n
  4. LEFT JOIN tv_seasons AS s ON s.show_id = n.show_id
  5. WHERE n.STATUS != 'END'
  6. AND s.date_added <= DATE_SUB(UTC_TIMESTAMP(), INTERVAL 1 YEAR)
  7. GROUP BY n.show_id
  8. ORDER BY n.show_name

The MySQL version is 5.1.65.

Could anyone please give some assistance on this query?

Thanks
Colin
__________________
-annihilate- Personal Site | Java Server Faces Discussion

Reply With Quote
  #2  
Old November 6th, 2012, 03:27 PM
r937's Avatar
r937 r937 is online now
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,374 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 7 h 8 m 42 sec
Reputation Power: 4140
Code:
SELECT n.show_id
     , n.show_name
     , n.STATUS
     , m.max_season
     , s.date_added AS max_season_added 
  FROM tv_show_names AS n 
INNER 
  JOIN ( SELECT show_id
              , MAX(season) AS max_season
           FROM tv_seasons
         GROUP
             BY show_id ) AS m
INNER
  JOIN tv_seasons AS s 
    ON s.show_id = m.show_id
   AND s.season = m.max_season
   AND s.date_added <= CURRENT_DATE - INTERVAL 1 YEAR 
 WHERE n.STATUS != 'END' 
ORDER 
    BY n.show_name 
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book

Reply With Quote
  #3  
Old November 6th, 2012, 04:19 PM
annihilate's Avatar
annihilate annihilate is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: Surrey, UK
Posts: 224 annihilate User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 6 h 12 m 49 sec
Reputation Power: 10
Hi

Thanks for replying.

I tried running that query and got quite lot more rows back than expected. There are only 57 TV shows where the status is not END. The query returned over 6000 rows.

Reply With Quote
  #4  
Old November 6th, 2012, 05:18 PM
r937's Avatar
r937 r937 is online now
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,374 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 7 h 8 m 42 sec
Reputation Power: 4140
run the subquery by itself... how many rows does it produce?

Reply With Quote
  #5  
Old November 6th, 2012, 05:23 PM
annihilate's Avatar
annihilate annihilate is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: Surrey, UK
Posts: 224 annihilate User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 6 h 12 m 49 sec
Reputation Power: 10
Hi

I ran the following:
sql Code:
Original - sql Code
  1. SELECT show_id, MAX(season) AS max_season
  2. FROM tv_seasons
  3. GROUP BY show_id

This produced 162 rows. There are 162 rows in the tv_show_names table, so I believe this is the expected result, since every show does have at least 1 season in the tv_seasons table.

Reply With Quote
  #6  
Old November 6th, 2012, 05:26 PM
r937's Avatar
r937 r937 is online now
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,374 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 7 h 8 m 42 sec
Reputation Power: 4140
so far so good...

now take the query that i gave you, and remove the last INNER JOIN (as well as s.date_added from the SELECT list), like this --
Code:
SELECT n.show_id
     , n.show_name
     , n.STATUS
     , m.max_season
  FROM tv_show_names AS n 
INNER 
  JOIN ( SELECT show_id
              , MAX(season) AS max_season
           FROM tv_seasons
         GROUP
             BY show_id ) AS m
 WHERE n.STATUS != 'END' 
ORDER 
    BY n.show_name 

now how many rows?

Reply With Quote
  #7  
Old November 6th, 2012, 05:38 PM
annihilate's Avatar
annihilate annihilate is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: Surrey, UK
Posts: 224 annihilate User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 6 h 12 m 49 sec
Reputation Power: 10
This is where it goes wrong.
9454 rows

Which looks like its 58 x 163 (there are now 58 shows not with a status of END and there are 163 shows in total)

Last edited by annihilate : November 6th, 2012 at 05:43 PM.

Reply With Quote
  #8  
Old November 6th, 2012, 06:25 PM
r937's Avatar
r937 r937 is online now
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,374 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 7 h 8 m 42 sec
Reputation Power: 4140
Quote:
Originally Posted by annihilate
This is where it goes wrong.
9454 rows
well, well

it seems i am an idiot

try this --
Code:
SELECT n.show_id
     , n.show_name
     , n.STATUS
     , m.max_season
     , s.date_added AS max_season_added 
  FROM tv_show_names AS n 
INNER 
  JOIN ( SELECT show_id
              , MAX(season) AS max_season
           FROM tv_seasons
         GROUP
             BY show_id ) AS m
    ON m.show_id = n.show_id
INNER
  JOIN tv_seasons AS s 
    ON s.show_id = m.show_id
   AND s.season = m.max_season
   AND s.date_added <= CURRENT_DATE - INTERVAL 1 YEAR 
 WHERE n.STATUS != 'END' 
ORDER 
    BY n.show_name 
Comments on this post
annihilate agrees!

Reply With Quote
  #9  
Old November 7th, 2012, 08:00 AM
annihilate's Avatar
annihilate annihilate is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: Surrey, UK
Posts: 224 annihilate User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 6 h 12 m 49 sec
Reputation Power: 10
Brilliant! That seems to be working perfectly.
Thanks a lot for your help.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMySQL Help > Help with where clause

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap