| Author |
Topic |
|
NeilC
Yak Posting Veteran
55 Posts |
Posted - 2008-07-12 : 16:36:04
|
| I am trying to update a bit column of the top 2 records from a queryselect col1, top 2 count(col2) as col2 from table1group by col1 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
NeilC
Yak Posting Veteran
55 Posts |
Posted - 2008-07-12 : 17:15:29
|
| [code]col1 col2 This needs updating to true----------- ----------- ---------------------------1 5 3 3 [/code]Does this make it a little clearer? The records that get returned from the query need a column (bit) updating to true |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2008-07-12 : 22:37:22
|
| It makes the output clearer, however how do you determine what two rows to return? Could you show us say 5-10 rows of data and then the expected output of that data?Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Subscribe to my blog |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-07-13 : 02:34:41
|
It seems like this is what you're afterUPDATE tSET t.BitField=1FROM (SELECT ROW_NUMBER() OVER (PARTITION BY col1 ORDER BY Col2 DESC) AS RowNo,Col1,Col2FROM YourTable)tWHERE t.RowNo<=2 If this is not what you require please make your requirement more clear by means of some data. |
 |
|
|
NeilC
Yak Posting Veteran
55 Posts |
Posted - 2008-07-13 : 11:37:53
|
I need to do this, but ALL the records are getting updated, nit the records in the sub-queryupdate table1set bitCol = 1where exists ( SELECT TOP 2 col1 FROM table2 GROUP BY col1 ORDER BY COUNT(table2.col2) DESC ) |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2008-07-13 : 11:54:53
|
[code]update table1set bitCol = 1where somecol in ( SELECT TOP 2 col1 FROM table2 GROUP BY col1 ORDER BY COUNT(table2.col2) DESC )[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
NeilC
Yak Posting Veteran
55 Posts |
Posted - 2008-07-13 : 12:07:05
|
| Thanks! I tried that earlier an it failed, but I have col1, col2 in the select statement - oops |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-07-13 : 13:25:03
|
Try this too...UPDATE tSET t.BitField=1FROM YourTable tINNER JOIN (SELECT ROW_NUMBER() OVER (PARTITION BY col1 ORDER BY COUNT(Col2) DESC) AS RowNo,Col1,COUNT(Col2)FROM YourTable)t1ON t.Col1=t1.Col1AND t1.RowNo<=2 |
 |
|
|
NeilC
Yak Posting Veteran
55 Posts |
Posted - 2008-07-14 : 08:56:03
|
| I have never used over and partition before, is there any benefit than the above query? |
 |
|
|
|