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 > Sybase > query help

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-07-10, 04:29
sakitram sakitram is offline
Registered User
 
Join Date: Aug 2010
Posts: 13
query help

Hi,

I have a question regarding filtering records. Kindly please help.
I have the following table.

Table: emp_monthly_sales
emp_id|date_time|sales_amount
101|20100105|250000
102|20100118|750000
101|20100409|1800000

Now I just want an SQL query to pick records of each employee's latest sales.
So the query result should be like the following.

102|20100118|750000
101|20100409|1800000

Here emp_id is the foreign key.

Thank you.

Regards,
Sakitram
Reply With Quote
  #2 (permalink)  
Old 08-07-10, 05:47
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,517
Why on earth would we want to do your coursework?
__________________
Mike
Reply With Quote
  #3 (permalink)  
Old 08-07-10, 09:42
sakitram sakitram is offline
Registered User
 
Join Date: Aug 2010
Posts: 13
Hi, it's not my coursewoork, but in just asked a way how to do a task in database in my profession. Please help.
Reply With Quote
  #4 (permalink)  
Old 08-07-10, 10:49
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,084
Quote:
Originally Posted by sakitram View Post
Please help.
sure, be glad to

we'll do it in two stages

first, please write a query that returns the latest date for each employee
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 08-07-10, 12:23
sakitram sakitram is offline
Registered User
 
Join Date: Aug 2010
Posts: 13
SELECT max(date_time) FROM emp_monthly_sales
Reply With Quote
  #6 (permalink)  
Old 08-07-10, 12:50
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,517
that's actually the latest date for all employees.
__________________
Mike
Reply With Quote
  #7 (permalink)  
Old 08-07-10, 13:29
sakitram sakitram is offline
Registered User
 
Join Date: Aug 2010
Posts: 13
SELECT max(date_time) FROM emp_monthly_sales group by emp_id.

I think the answer is the following.

SELECT a.emp_id, max(b.date_time), b.sales_amount
FROM emp_monthly_sales a, emp_monthly_sales b
WHERE a.emp_id=b.emp_id
GROUP BY a.emp_id

I'm not sure.
Reply With Quote
  #8 (permalink)  
Old 08-07-10, 13:45
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,517
Quote:
Originally Posted by sakitram View Post
I'm not sure.
The simple way to see if you're correct is to run the query.
Do you get the latest date for each employee?
__________________
Mike
Reply With Quote
  #9 (permalink)  
Old 08-07-10, 14:53
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,084
Quote:
Originally Posted by sakitram View Post
SELECT max(date_time) FROM emp_monthly_sales group by emp_id.
this is syntactically correct, but not semantically

it does give you a result set of dates, and they do happen to be the last date for each employee, however, all you get is the dates... so this query has a very serious logical shortcoming -- you can't tell whose dates they are!!
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #10 (permalink)  
Old 08-07-10, 15:28
sakitram sakitram is offline
Registered User
 
Join Date: Aug 2010
Posts: 13
sorry... that did not give result what I wanted..
please correct the query.. thanks.
Reply With Quote
  #11 (permalink)  
Old 08-07-10, 15:47
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,084
Quote:
Originally Posted by sakitram View Post
please correct the query.. thanks.
how about if i teach you how to fix it

let's do this in two stages

first, please write a query that returns the latest date for each employee
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #12 (permalink)  
Old 08-07-10, 16:52
sakitram sakitram is offline
Registered User
 
Join Date: Aug 2010
Posts: 13
Quote:
Originally Posted by r937 View Post
first, please write a query that returns the latest date for each employee
here is the query

SELECT a.emp_id, max(b.dt)
FROM tmp a, tmp b
WHERE a.emp_id=b.emp_id
GROUP BY a.emp_id
Reply With Quote
  #13 (permalink)  
Old 08-07-10, 17:01
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,084
Quote:
Originally Posted by sakitram View Post
here is the query
now we're making progress

before going to stage 2, i wonder if i could ask you to please rewrite this query using JOIN syntax instead of the implicit comma join you have now

also, would you please assign a column alias to the MAX expression
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #14 (permalink)  
Old 08-07-10, 17:18
sakitram sakitram is offline
Registered User
 
Join Date: Aug 2010
Posts: 13
Quote:
Originally Posted by r937 View Post
before going to stage 2, i wonder if i could ask you to please rewrite this query using JOIN syntax instead of the implicit comma join you have now

also, would you please assign a column alias to the MAX expression
SELECT a.emp_id, max(b.dt) sales_date
FROM tmp a left join tmp b
on a.emp_id=b.emp_id
GROUP BY a.emp_id
Reply With Quote
  #15 (permalink)  
Old 08-07-10, 17:39
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,084
my sincere apologies, i was so anxious to move on with this that i only briefly examined your last query, and sort of automatically asked you to rewrite the join (because i won't work with anything except explicit JOIN syntax)

but now i need for you to go back to square one, as it were, and write a brand new query that gets the latest sales date for each employee...

... except that there will be only one table involved in the query, namely, the single table that you talked about in your first post
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools
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