Thread: query help
View Single Post
  #21 (permalink)  
Old 08-08-10, 14:36
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
good

next stage is to join the original table onto this query

when a query is used in the FROM clause, as a subquery, it is known as an inline view or derived table, because you can think of the results it produces as thought they were a table, which they are

so the rows would be one row per employee and the columns would be emp_id and latest date
Code:
SELECT ...
  FROM ( previous query goes here ) AS q
INNER
  JOIN emp_monthly_sales AS t
    ON t.emp_id = q.emp_id
   AND t.dt = q.latest_date
you are joining each employee's latest date

now, as a result of the join, you can show the value for t.sales_amount, and you are now sure that it is the correct amount for the latest date
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote