Solved

Max Date and Group By SQL Server

Posted on 2014-10-16
2
85 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 25

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

739 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