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
39 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:basile
5 Comments
 
LVL 65

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:basile
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 65

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 48

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
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 UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

746 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

18 Experts available now in Live!

Get 1:1 Help Now