We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Get rows with max date per product

Medium Priority
468 Views
Last Modified: 2012-06-22
Hello,

I have this situation:

id      |      product      |     date
===============================
1       |      6100          |   01.01.2005
2       |      6600          |   02.02.2005
3       |      660001      |   04.03.2005
12     |      610001      |   10.05.2005
14     |      1100          |   12.05.2006
22     |       6100         |    01.06.2009


ID is auto inc.
PRODUCT is product id where first 4 digits is product code and possible extension 01, 02, 03, etc are updates to the product.
DATE is date published

I would like to get a list of rows grouped by product in this way:


id      |      product      |     date
===============================
3       |      660001      |   04.03.2005           <= because max product within max date
14     |      1100          |   12.05.2006           <= because max product within max date
22     |       6100         |    01.06.2009          <= because max product within max date


What troubles me is this combination of 1st max date and then max product id.

Thank you!
Comment
Watch Question

Raja Jegan RSQL Server DBA & Architect, EE Solution Guide
CERTIFIED EXPERT
Awarded 2009
Distinguished Expert 2019

Commented:
Hope this helps:

P.S: works only for SQL Server 2005
select id, product, date
from (
select id, product, date, row_number() over ( partition by product order by date desc ) rnum
from ur_table ) temp
where rnum = 1

Open in new window

Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Racim BOUDJAKDJIDatabase Architect - Dba - Data Scientist
CERTIFIED EXPERT

Commented:
select distinct A.*
from yourtable A
inner join
           (select product, max(date) as maxdate from yourtable group by yourtable) B
on B.maxdate = A.date and B.product = A.product

HTH
Hi Racimo,

I am bit confuse about your above query, there may be a mistake in clause "group by yourtable" moreover, don't we need to compare and group by based on only first four character of product,

either I misread the question or there is problem in yours and rrgen's solution. let me read question once again.
Racim BOUDJAKDJIDatabase Architect - Dba - Data Scientist
CERTIFIED EXPERT

Commented:

Sorry ignore my previous post..

select distinct A.*
from yourtable A
inner join
           (select left(product, 4) as leftproduct, max(date) as maxdate from yourtable group by left(product, 4)) B
on B.maxdate = A.date and B.leftproduct = left(A.product, 4)
Racim BOUDJAKDJIDatabase Architect - Dba - Data Scientist
CERTIFIED EXPERT

Commented:
<<either I misread the question or there is problem in yours and rrgen's solution. let me read question once again.>>
Don't bother I had read the question too fast and I did not get much sleep last night ;)
:)

Author

Commented:
Thank you guys.

I forgot to mention one extra field, which is customer id. SORRY!
So basicly what you have written works, just how to make it "per customer_id"?

id      |   customer_id  |      product      |     date
===============================
1       |           1            |     6100          |   01.01.2005
2       |           2            |     6600          |   02.02.2005
3       |           2            |     660001      |   04.03.2005
12     |           1            |     610001      |   10.05.2005
14     |           3            |     1100          |   12.05.2006
22     |           1            |      6100         |    01.06.2009


Expected result:

id      |   customer_id  |      product      |     date
===============================
3       |           2            |     660001      |   04.03.2005
14     |           3            |     1100          |   12.05.2006
22     |           1            |      6100         |    01.06.2009

Thanks!
select id, product, date
from (
select id, product, date, customer_id  , row_number() over ( partition by left(product,4), customer_id   order by date desc ) rnum
from ur_table ) temp
where rnum = 1
or may be you want to display customer id also.


select id, product, date,customer_id
from (
select id, product, date, customer_id  , row_number() over ( partition by left(product,4), customer_id   order by date desc ) rnum
from ur_table ) temp
where rnum = 1

Author

Commented:
Ok guys, I think I have it.
I just added customer_id into the partition by clause.

Is this the correct way?

The result seems to be correct.

Author

Commented:
That was fast!
Thank you very much!
Racim BOUDJAKDJIDatabase Architect - Dba - Data Scientist
CERTIFIED EXPERT

Commented:
Have you tried

select distinct A.*
from yourtable A
inner join
           (select left(product, 4) as leftproduct, max(date) as maxdate from yourtable group by left(product, 4)) B
on B.maxdate = A.date and B.leftproduct = left(A.product, 4)


Hi Racimo,

I guess your query would work but at the cost of performance. because of subquery and DISTINCT mainly.
Racim BOUDJAKDJIDatabase Architect - Dba - Data Scientist
CERTIFIED EXPERT

Commented:
<<I guess your query would work but at the cost of performance. because of subquery and DISTINCT mainly.>>
This is just a matter of standards and common sense.

First, the idea that subqueries *always* degrade performance is *wrong*.  
Subqueries *may* degrade performance when there is too much tempdb merging and sorting OR when there cause blocking locks.  Both of the previous can be easily avoided by the grammar and syntax used.  There are plenty of situations where subqueries will get results much faster and more reliably than by using alternatives.  

Second, the cost of performance *must* always be balanced with making the query return accurate dupplicate- free results and the time of execution.  In this case, the questionner may *not* want dupplicates records to be returned. This is why DISTINCT ought to be used.  The fact that you totally decided to ignore this aspect is a matter of choice.  Else youd have added it in your own query.
I am agree with you but in this particular case, if you run yours and my both query with execution plan you will came to know the performance difference

Author

Commented:
Hi Racimo,

I didn't try your query yet as I just had to make it work as soon as possible.
I will try it in a next couple of hours (as I have to leave now) and will compare the two in sense of performance on my real data.

I need distinct results... but in a sense of customer / product, not the whole row.
Results of this query go further to be used by another query for extra grouping and such.

Will post back the results.
Racim BOUDJAKDJIDatabase Architect - Dba - Data Scientist
CERTIFIED EXPERT

Commented:
<<I am agree with you but in this particular case, if you run yours and my both query with execution plan you will came to know the performance difference>>
I am amazed as to how you actually come to this conclusion, since neither you or I have the actual indexing structure and correct data to actually determine the correct execution plan.  Let the user run both queries and see what would work best for him.
Racim BOUDJAKDJIDatabase Architect - Dba - Data Scientist
CERTIFIED EXPERT

Commented:

<< I didn't try your query yet as I just had to make it work as soon as possible.>>
In that case try the below version...

select distinct A.*
from yourtable (nolock) A
inner join
           (select left(product, 4) as leftproduct, max(date) as maxdate from yourtable (nolock) group by left(product, 4)) B
on B.maxdate = A.date and B.leftproduct = left(A.product, 4)
ok, let author decide but I can bet on the performance for sure in this particular case.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.