Solved

Max Date and Group By SQL Server

Posted on 2014-10-16
2
82 Views
Last Modified: 2014-10-17
I'm working on a CRM report.  I want to show the last contact history per account.  The history is all housed in one table, account information in the other.

Pertinint fields in History are:

HistoryID, Account, Date
Where there can be many dates and history IDs related to the account

I'm trying to find the latest date by doing a MAX expression on the date field and grouping by the Account field -- that works fine, but I need to return the HistoryID related to that record in order to join it to the account table.

I hope this makes enough sense for you to help.

For now, I've used a query joining the account to history with all history and then filtered within SSRS to get the results for this one report, but I'd like to create a view of just the HistoryID, Account, Date from the History table for future use.
0
Comment
Question by:akscott
2 Comments
 
LVL 24

Accepted Solution

by:
chaau earned 500 total points
ID: 40385950
These type of queries are best performed with a help of a window function:
with a as (
Select Account, HistoryID, Date, ROW_NUMBER() OVER (PARTITION BY Account ORDER BY Date DESC) as rn
From History )
SELECT Account, HistoryID, Date FROM a WHERE rn = 1

Open in new window

0
 

Author Comment

by:akscott
ID: 40386737
I don't understand it, but it worked. There's a section on partitions in my SQL book -- looks like I need to read that.  Thank you!
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now