Solved

Get rows with max date per product

Posted on 2009-07-03
20
402 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!
0
Comment
Question by:hpet
  • 8
  • 7
  • 4
  • +1
20 Comments
 
LVL 57

Expert Comment

by:Raja Jegan R
ID: 24770650
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

0
 
LVL 31

Accepted Solution

by:
RiteshShah earned 250 total points
ID: 24770660
I think above solution will not work as you need to compare only first four digit of product, try using this one:


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

0
 
LVL 23

Expert Comment

by:Racim BOUDJAKDJI
ID: 24770718
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
0
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24770735
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.
0
 
LVL 23

Expert Comment

by:Racim BOUDJAKDJI
ID: 24770746

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)
0
 
LVL 23

Expert Comment

by:Racim BOUDJAKDJI
ID: 24770754
<<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 ;)
0
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24770766
:)
0
 
LVL 1

Author Comment

by:hpet
ID: 24770911
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!
0
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24770929
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
0
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24770931
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

0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 1

Author Comment

by:hpet
ID: 24770936
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.
0
 
LVL 1

Author Closing Comment

by:hpet
ID: 31599483
That was fast!
Thank you very much!
0
 
LVL 23

Expert Comment

by:Racim BOUDJAKDJI
ID: 24770960
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)


0
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24770971
Hi Racimo,

I guess your query would work but at the cost of performance. because of subquery and DISTINCT mainly.
0
 
LVL 23

Expert Comment

by:Racim BOUDJAKDJI
ID: 24771051
<<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.
0
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24771071
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
0
 
LVL 1

Author Comment

by:hpet
ID: 24771102
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.
0
 
LVL 23

Expert Comment

by:Racim BOUDJAKDJI
ID: 24771148
<<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.
0
 
LVL 23

Expert Comment

by:Racim BOUDJAKDJI
ID: 24771158

<< 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)
0
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24771160
ok, let author decide but I can bet on the performance for sure in this particular case.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
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…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how the fundamental information of how to create a table.

744 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

9 Experts available now in Live!

Get 1:1 Help Now