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 February 15th, 2013, 02:07 PM
seanjohnstone seanjohnstone is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 2 seanjohnstone User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 20 m 47 sec
Reputation Power: 0
Referencing a derived value in an INNER JOIN?

Hi there,

I wonder if anyone can please advise me on how I might restructure this query so that I can use one of the derived values (average_score, as listed in line 3) further down inside the second INNER_JOIN statement?
I suspect that it doesn't work because the INNER_JOIN parts gets calculated before the derived values get created and therefore they are NULL?

SELECT e.index, e.name, e.occupation, e.location,
COUNT(r.index) AS number_of_reports,
ROUND(AVG(r.score)) AS average_score,
ROUND(SUM(r.cost)/SUM(r.hours_worked)*60) AS hourly_rate,
g.grade as grade
FROM `employees` AS e
INNER JOIN `reports` AS r
ON e.index = r.host_index
INNER JOIN `grades` as g
ON (hourly_rate=g.rate)

GROUP BY e.index, e.name, e.location
HAVING COUNT(r.index) >= 2
ORDER BY AVG(r.score) DESC

Thanks for any help!
Sean.

Reply With Quote
  #2  
Old February 15th, 2013, 03:36 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,362 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 5 h 42 m 2 sec
Reputation Power: 4140
Code:
SELECT data.*
     , g.grade
  FROM ( SELECT e.index
              , e.name
              , e.occupation
              , e.location
              , COUNT(r.index) AS number_of_reports
              , ROUND(AVG(r.score)) AS average_score
              , ROUND(SUM(r.cost)/SUM(r.hours_worked)*60) AS hourly_rate
           FROM `employees` AS e
         INNER 
           JOIN `reports` AS r
             ON r.host_index = e.index
         GROUP 
             BY e.index
              , e.name
              , e.occupation
              , e.location
         HAVING COUNT(r.index) >= 2 ) AS data
INNER 
  JOIN `grades` as g
    ON g.rate = data.hourly_rate
ORDER 
    BY data.average_score DESC
thread moved to mysql forum, because ms sql server doesn't use those horrendous backticks

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book

Reply With Quote
  #3  
Old February 18th, 2013, 09:23 AM
seanjohnstone seanjohnstone is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 2 seanjohnstone User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 20 m 47 sec
Reputation Power: 0
Thank you :-)
I'll give this a try tonight!

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMySQL Help > Referencing a derived value in an INNER JOIN?

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