Solved

Get rows with max date per product

Posted on 2009-07-03
20
418 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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
 
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

Backup Solution for AWS

Read about how CloudBerry Backup fully integrates your backups with Amazon S3 and Amazon Glacier to provide military-grade encryption and dramatically cut storage costs on any platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

726 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