Solved

Find the oldest record by date, and get that row, as well as records that aren't dupes

Posted on 2016-07-15
5
56 Views
Last Modified: 2016-07-21
I'm trying to use the query below. What happens is that call_id can get many duplicates. I'm trying to get the latest call_id based in received_date of there are dupes. As well as getting any data that is not a dupe.

 
select received_date, call_id, COUNT(call_id)
from imp_WBDOC_data_myssa_callback
group by received_date, call_id
having COUNT(call_id) > 1
order by COUNT(call_id) des
0
Comment
Question by:Auerelio Vasquez
[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
5 Comments
 
LVL 66

Expert Comment

by:Jim Horn
ID: 41713093
>I'm trying to get the latest call_id based in received_date of (of?) there are dupes.
>As well as getting any data that is not a dupe.
Looks like the above two statements are mutually exclusive.  Please provide us a sample data set and expected result of what you're trying to pull off here.
0
 
LVL 34

Accepted Solution

by:
Brian Crowe earned 250 total points
ID: 41713102
Your question is fairly ambiguous but assuming you are trying to get the latest record for a given call_id...

WITH cteCallback AS
(
	SELECT call_id, received_Date,
		ROW_NUMBER() OVER(PARTITION BY call_id ORDER BY received_date DESC) AS RowNumber
	FROM imp_WBDOC_data_myssa_callback
)
SELECT *
FROM cteCallback
WHERE RowNumber = 1

Open in new window

0
 
LVL 1

Author Comment

by:Auerelio Vasquez
ID: 41713134
Yes  im trying to get the latest call if there are dupes on the call_id based in the received date.

Also, most of the records don't have dupes, so I also want all rows where there are dupes, and only the record with  max received date, so that I'm only getting one row per call_id
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 41713147
Pony us up some sample data please.

Keep in mind that experts here cannot connect to your data source(s) and run queries, and all we have to go on to answer questions is the text provided by the asker, so what may be real obvious to you is not obvious to us.  Unless you give us some data to work with.
0
 
LVL 49

Assisted Solution

by:PortletPaul
PortletPaul earned 250 total points
ID: 41713801
Using ROW_NUMBER() OVER() is very effective for "I want the latest" (or lst/first/earliest).

The row_number) function gives each row a unique integer starting at 1, when combined with the OVER clause you get the row_number value 1 for each "partition" as determined by the order you nominate. Here PARTITION BY call_id ORDER BY received_date DESC you get 1 for the highest (latest) date. After that it is just a matter of filtering for the ones.
SELECT *
FROM (
	SELECT *
		, ROW_NUMBER() OVER(PARTITION BY call_id ORDER BY received_date DESC) AS RowNumber
	FROM imp_WBDOC_data_myssa_callback
      ) Callbacks
WHERE RowNumber = 1

Open in new window

I prefer to use a very easy to read "derived table" for this.
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

724 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