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 Statement

Author  Topic 

jwallz
Starting Member

14 Posts

Posted - 2009-03-08 : 10:41:27
Hi

I have a link table to store categories assigned to articles. Currently it's in this form
Table: Article_Category
Columns: ArticleID (pk, fk, int, notnull)
CategoryID (pk,fk,smallint,notnull)

An article can have 1+ associated categories. What i need to do is set one primary category for each ArticleID. So I'm adding a bit column to this table called PrimaryCat(bit, notnull, default 0)

Now I'd like to arbitrarily set a primary cat for each of the unique ArticleIDs.

if a particular ArticleID is associated with 4 categories i'd like to randomly set one as the PrimmaryCat, ie:

ArticleID CategoryID PrimaryCat
10 22 0
10 23 1
10 24 0
10 25 0

I'm having trouble creating the update statement to do this because i don't know how to identify the where clause. My issue is the update is dependent on a combo of both ArticleID and CategoryID. There's no primary key since this is a link table. I'm stuck here:
UPDATE Article_Category
SET PrimaryCat = 1
WHERE ResourceID = ?? and CategoryID = ??

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2009-03-08 : 11:45:32
One way to do this is as follows:
with A as
(
select
row_number() over (partition by articleid order by categoryid) as row,
primarycat
from
article_category
)
update A set primary_cat = 1 where row = 1

One thing I want to point out though, is that this scheme for identifying the primary category does not prevent someone from setting more than one category as primary category for a given articleid. So, instead, you may want to add a new table that has ArticleId and PrimaryCatId which has foreign keys into the ArticleCategory table and the ArticleId and PrimaryCatId as primary keys.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-03-08 : 12:48:30
quote:
Originally posted by jwallz

Hi

I have a link table to store categories assigned to articles. Currently it's in this form
Table: Article_Category
Columns: ArticleID (pk, fk, int, notnull)
CategoryID (pk,fk,smallint,notnull)

An article can have 1+ associated categories. What i need to do is set one primary category for each ArticleID. So I'm adding a bit column to this table called PrimaryCat(bit, notnull, default 0)

Now I'd like to arbitrarily set a primary cat for each of the unique ArticleIDs.

if a particular ArticleID is associated with 4 categories i'd like to randomly set one as the PrimmaryCat, ie:

ArticleID CategoryID PrimaryCat
10 22 0
10 23 1
10 24 0
10 25 0

I'm having trouble creating the update statement to do this because i don't know how to identify the where clause. My issue is the update is dependent on a combo of both ArticleID and CategoryID. There's no primary key since this is a link table. I'm stuck here:
UPDATE Article_Category
SET PrimaryCat = 1
WHERE ResourceID = ?? and CategoryID = ??



what you need is just as derived table like below


UPDATE t
SET t.PrimaryCat=1
FROM
(SELECT ROW_NUMBER() OVER (PARTITION BY ArticleID ORDER BY NEWID()) AS Seq,PrimaryCat
FROM Article_Category
)t
WHERE Seq=1
Go to Top of Page

jwallz
Starting Member

14 Posts

Posted - 2009-03-09 : 04:34:53
Thank you both for your responses. They both worked. I also may consider changing schema per sunitabeck's recommendation
Go to Top of Page
   

- Advertisement -