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.
| 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_CategoryColumns: 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 PrimaryCat10 22 010 23 110 24 010 25 0I'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. |
 |
|
|
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_CategoryColumns: 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 PrimaryCat10 22 010 23 110 24 010 25 0I'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 belowUPDATE tSET t.PrimaryCat=1FROM(SELECT ROW_NUMBER() OVER (PARTITION BY ArticleID ORDER BY NEWID()) AS Seq,PrimaryCatFROM Article_Category)tWHERE Seq=1 |
 |
|
|
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 |
 |
|
|
|
|
|
|
|