Get rows with max date per product

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!
LVL 1
hpetAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Raja Jegan RSQL Server DBA & Architect, EE Solution GuideCommented:
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
RiteshShahCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Racim BOUDJAKDJIDatabase Architect - Dba - Data ScientistCommented:
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
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

RiteshShahCommented:
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
Racim BOUDJAKDJIDatabase Architect - Dba - Data ScientistCommented:

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
Racim BOUDJAKDJIDatabase Architect - Dba - Data ScientistCommented:
<<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
RiteshShahCommented:
:)
0
hpetAuthor 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!
0
RiteshShahCommented:
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
RiteshShahCommented:
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
hpetAuthor 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.
0
hpetAuthor Commented:
That was fast!
Thank you very much!
0
Racim BOUDJAKDJIDatabase Architect - Dba - Data ScientistCommented:
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
RiteshShahCommented:
Hi Racimo,

I guess your query would work but at the cost of performance. because of subquery and DISTINCT mainly.
0
Racim BOUDJAKDJIDatabase Architect - Dba - Data ScientistCommented:
<<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
RiteshShahCommented:
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
hpetAuthor 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.
0
Racim BOUDJAKDJIDatabase Architect - Dba - Data ScientistCommented:
<<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
Racim BOUDJAKDJIDatabase Architect - Dba - Data ScientistCommented:

<< 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
RiteshShahCommented:
ok, let author decide but I can bet on the performance for sure in this particular case.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.