开发者

Limiting results in a SQL query based on distinctness in an arbitrary column

I need to write a SQL query that pulls from a table that stores records for each time one of our salespeople speaks to a client. The relevant columns are: (1) the salesperson's employee ID, (2) the client's account number, and (2) the date of the conversation.

It's often the case that salespeople have spoken to clients multiple times within the report period (a calendar month) so there will be several entries that are nearly identical except for the date.

Where I'm getting tripped up is that, for the purpose of this que开发者_JAVA百科ry, I need to return only one record per salesperson/client combination, but I can't use DISTINCT because I need to include the date of the most recent conversation within the reporting period.

So, if salesperson John has spoken to client ABC on 10/10, 10/18, and 10/25 I need to pull the 10/25 record but not the others.

It's a Sybase database.

I have the feeling that I may be missing something simple here but I've tried searching and remain stumped. Any help is greatly appreciated.

Thanks for your time,

John


Guessing at the column names...

  SELECT employee_id, client_acct_no, 
         MAX(conversation_date) AS MOST_RECENT_CONV_DATE
    FROM mytable 
   WHERE conversation_date BETWEEN DATE '2010-10-01' AND DATE '2010-10-31'
GROUP BY employee_id, client_acct_no

Documentation for GROUP BY clause.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜