Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 65
  • Last Modified:

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

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
Auerelio Vasquez
Asked:
Auerelio Vasquez
2 Solutions
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
>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
 
Brian CroweCommented:
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
 
Auerelio VasquezETL DeveloperAuthor Commented:
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
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
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
 
PortletPaulCommented:
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now