SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Select latest description from table


Select latest description from table

Author
Message
seajoker
seajoker
SSC-Enthusiastic
SSC-Enthusiastic (192 reputation)SSC-Enthusiastic (192 reputation)SSC-Enthusiastic (192 reputation)SSC-Enthusiastic (192 reputation)SSC-Enthusiastic (192 reputation)SSC-Enthusiastic (192 reputation)SSC-Enthusiastic (192 reputation)SSC-Enthusiastic (192 reputation)

Group: General Forum Members
Points: 192 Visits: 146
I have a sales table that updated daily with new sales data. When pulling historical data for a particular SKU# and description, I get multiple lines for each description that the SKU# has been. I would like to be able to only pull the latest description from the table and use is for all sales for that SKU#

SKU# Description SalesDate
01110 Red Car with stripe 01/19/2009
01110 Red Car with line 02/01/2009
01110 Red Car with laser 02/25/2009

Thanks,
Seajoker
noeld
noeld
SSCrazy Eights
SSCrazy Eights (10K reputation)SSCrazy Eights (10K reputation)SSCrazy Eights (10K reputation)SSCrazy Eights (10K reputation)SSCrazy Eights (10K reputation)SSCrazy Eights (10K reputation)SSCrazy Eights (10K reputation)SSCrazy Eights (10K reputation)

Group: General Forum Members
Points: 9984 Visits: 2048
if object_id('tempdb..#t1') is not null
drop table #t1

create table #t1 (SKU# char(5), [Description] varchar(200), SalesDate datetime)

Insert into #t1 ([SKU#], [Description], SalesDate) Values('01110', 'Red Car with stripe', '01/19/2009')
Insert into #t1 ([SKU#], [Description], SalesDate) Values('01110', 'Red Car with line', '02/01/2009')
Insert into #t1 ([SKU#], [Description], SalesDate) Values('01110', 'Red Car with laser', '02/25/2009')
Insert into #t1 ([SKU#], [Description], SalesDate) Values('01120', 'Blue Car with stripe', '01/19/2009')
Insert into #t1 ([SKU#], [Description], SalesDate) Values('01120', 'Blue Car with line', '02/05/2009')
Insert into #t1 ([SKU#], [Description], SalesDate) Values('01120', 'Blue Car with laser', '02/20/2009')

;with cte
AS
(SELECT *, row_number() over(partition by [sku#] order by SalesDate desc) rn
from #t1
)
select [SKU#], [Description], SalesDate
from cte
where rn =1




* Noel
Bevan Keighley
Bevan Keighley
Old Hand
Old Hand (360 reputation)Old Hand (360 reputation)Old Hand (360 reputation)Old Hand (360 reputation)Old Hand (360 reputation)Old Hand (360 reputation)Old Hand (360 reputation)Old Hand (360 reputation)

Group: General Forum Members
Points: 360 Visits: 834
Hi,

you need to use the row_number function to identify which specific rows you want returned. Change the order by clause if you want the oldest record.



create table # (SKU# varchar(10), Description varchar(128), SalesDate datetime)

insert #
select '01110', 'Red Car with stripe', '01/19/2009'
union all select '01110', 'Red Car with line', '02/01/2009'
union all select '01110', 'Red Car with laser', '02/25/2009'
union all select '01120', 'Blue Car with stripe', '01/19/2009'
union all select '01120', 'Blue Car with line', '02/01/2009'
union all select '01120', 'Blue Car with laser', '02/25/2009'

select [sku#], [description], [SalesDate] from
(select *, row_number() over (partition by SKU# order by salesDate desc) as rownum from #) x
where rownum=1





Bevan
seajoker
seajoker
SSC-Enthusiastic
SSC-Enthusiastic (192 reputation)SSC-Enthusiastic (192 reputation)SSC-Enthusiastic (192 reputation)SSC-Enthusiastic (192 reputation)SSC-Enthusiastic (192 reputation)SSC-Enthusiastic (192 reputation)SSC-Enthusiastic (192 reputation)SSC-Enthusiastic (192 reputation)

Group: General Forum Members
Points: 192 Visits: 146
If I add Quantity to the equation, the data looks like this :
SKU# Description Date Quantity RN
01110 Red Car with laser 2009-02-25 00:00:00.000 3 1
01110 Red Car with line 2009-02-01 00:00:00.000 3 2
01110 Red Car with stripe 2009-01-19 00:00:00.000 3 3
01120 Blue Car with laser 2009-02-20 00:00:00.000 3 1
01120 Blue Car with line 2009-02-05 00:00:00.000 2 2
01120 Blue Car with stripe 2009-01-19 00:00:00.000 2 3

How can I change all descriptions for a particular sku to the newest description, so the data looks like this: I need to keep the daily sales quantity.

SKU# Description Date Quantity RN
01110 Red Car with laser 2009-02-25 00:00:00.000 3 1
01110 Red Car with laser 2009-02-01 00:00:00.000 3 2
01110 Red Car with laser 2009-01-19 00:00:00.000 3 3
01120 Blue Car with laser 2009-02-20 00:00:00.000 3 1
01120 Blue Car with laser 2009-02-05 00:00:00.000 2 2
01120 Blue Car with laser 2009-01-19 00:00:00.000 2 3
Bevan Keighley
Bevan Keighley
Old Hand
Old Hand (360 reputation)Old Hand (360 reputation)Old Hand (360 reputation)Old Hand (360 reputation)Old Hand (360 reputation)Old Hand (360 reputation)Old Hand (360 reputation)Old Hand (360 reputation)

Group: General Forum Members
Points: 360 Visits: 834
I see Noel and I posted almost identical answers on the first question!

Heres one way to update the values...


--- Have a look at new and old data side by side
select a.[sku#], a.[description], a.[SalesDate], x.[description] as new from
(select *, row_number() over (partition by SKU# order by salesDate desc) as rownum from #) x
inner join # a on a.sku# = x.sku#
where rownum=1


-- Run the update
UPDATE a set a.[description] = x.[description]
from
(select *, row_number() over (partition by SKU# order by salesDate desc) as rownum from #) x
inner join # a on a.sku# = x.sku#
where rownum=1


-- Have a look at new values
select * from #


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search