Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Update Table with SEQUENTIAL # based on criteria

Author  Topic 

Christy
Starting Member

7 Posts

Posted - 2007-05-09 : 16:58:03
***** SQL Server 2005 ********

I have a table that needs to be updated with a sequential number based on criteria.

I am trying to update the SeqID and LinkSeqID with the same sequential number if the ProductID and StoreID are in the same group. For instance the 1st three rows below are in the same group 752534 and 4, therefore the SeqID and LinkSeqID should be 1,2,3 and restart at 1 once the grouping of ProductID and StoreID changes. Please look at the examples below.


SALES Table as IS:
======================================
ProductID StoreID DBRowID SeqID LinkSeqID
======================================
752534 4 1
752534 4 2
752534 4 3
896784 2 4
896784 2 5
896784 4 6
898874 2 7
898968 2 8


This is what the table should look like after the update in complete.

SALES after UPDATE:
======================================
ProductID StoreID DBRowID SeqID LinkSeqID
======================================
752534 4 1 1 1
752534 4 2 2 2
752534 4 3 3 3
896784 2 4 1 1
896784 2 5 2 2
896784 4 6 1 1
898874 2 7 1 1
898968 2 8 1 1



Can anyone HELP me please?

dinakar
Master Smack Fu Yak Hacker

2507 Posts

Posted - 2007-05-09 : 17:08:33
[code]
SELECT ...
DENSE_RANK() OVER (PARTITION BY ProductID ORDER BY dbrowid) AS DENSE_RANK
...
FROM ..
[/code]

Dinakar Nethi
SQL Server MVP
************************
Life is short. Enjoy it.
************************
http://weblogs.sqlteam.com/dinakar/
Go to Top of Page

dinakar
Master Smack Fu Yak Hacker

2507 Posts

Posted - 2007-05-09 : 17:12:22
The functions can only be used in SELECT's. If you need to do an UPDATE, get the data into a table variable or a temp table along with the rank and then do an update on the original table by joining with the table variable/temp table.

Dinakar Nethi
SQL Server MVP
************************
Life is short. Enjoy it.
************************
http://weblogs.sqlteam.com/dinakar/
Go to Top of Page

pootle_flump

1064 Posts

Posted - 2007-05-09 : 17:53:17
Duped over at dbforums:
http://www.dbforums.com/showthread.php?t=1618091
quote:
Originally posted by dinakar

The functions can only be used in SELECT's. If you need to do an UPDATE, get the data into a table variable or a temp table along with the rank and then do an update on the original table by joining with the table variable/temp table.

... and I did not know that. What about a CTE?
Go to Top of Page

pootle_flump

1064 Posts

Posted - 2007-05-09 : 18:11:07
quote:
Originally posted by pootle_flump

What about a CTE?
Turns out it is fine.

use tempdb
go

create table #sales
(
productid int
, storeid int
, dbrowid int
, seqid int
, linkseqid int
)

insert into #sales (productid, storeid, dbrowid)
select 752534, 4, 1
union all
select 752534, 4, 2
union all
select 752534, 4, 3
union all
select 896784, 2, 4
union all
select 896784, 2, 5
union all
select 896784, 4, 6
union all
select 898874, 2, 7
union all
select 898968, 2, 8
go

with rns
as
(
select productid
, storeid
, row_number() over (partition by productid
, storeid
order by productid
, storeid) as row_num
from #sales
)
update s
set seqid = row_num
from #sales as s
inner join
rns
on rns.productid = s.productid
and rns.storeid = s.storeid

select *
from #sales

drop table #sales
Go to Top of Page
   

- Advertisement -