?
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
Medium Priority
?
59 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 1000 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 1000 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: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

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…
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how the fundamental information of how to create a table.
Suggested Courses

765 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