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 |
|
jay_sql
Starting Member
3 Posts |
Posted - 2009-01-31 : 17:48:41
|
| I got a table named tblAlerts. It has two columns viz., ALERTS, ROW_TEXT and has many rows in the table. The sample of few rows is shown belowALERTS ROW_TEXT------- --------1010748 Yes1010748 No1010748 N/A1010749 Yes1010749 No1010749 N/A1010751 Yes1010752 Yes1010752 No1010752 N/A1010753 Yes1010754 Yes1010754 No1010754 N/A1010755 Not OK1010755 N/A1010757 111111010757 222221010757 33333Now, I need to do sql manipulation and produce the table as below with three columns viz., ALERTS, ROW_TEXT and ROW_ID. Basically ROW_ID is evolved from ALERTS and it is just a sequence number relevant to ALERTS. As ALERTS 1010748 has three rows, so in the ROW_ID you have numbers 1,2,3.... similarly for ALERTS 1010753 has only one row, thus the ROW_ID has 1. For ALERTS 1010754 has four rows, thus the ROW_ID has 1,2,3,4.ALERTS ROW_TEXT ROW_ID------ -------- ----------1010748 Yes 11010748 No 21010748 N/A 31010749 Yes 11010749 No 21010749 N/A 31010751 Yes 11010752 Yes 11010752 No 21010752 N/A 31010753 Yes 11010754 Yes 11010754 No 21010754 N/A 31010754 None 41010755 Not OK 11010755 N/A 21010757 11111 11010757 22222 21010757 33333 3Thanks,JayThanks,Jay |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2009-01-31 : 18:33:03
|
In SQL Server 2005 or higher:selectALERTS,ROW_TEXT,ROW_NUMBER() OVER (partition by ALERTS order by ALERTS) AS ROW_IDfrom tblAlerts Webfred No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2009-01-31 : 21:11:32
|
In SQL 2000Declare @t table(ALERTS int,ROW_TEXT varchar(10))-- Preparing Sample DataInsert @tSelect 1010748, 'Yes' union allSelect 1010748, 'No' union allSelect 1010748, 'N/A' union allSelect 1010749 ,'Yes' union allSelect 1010749 ,'No' union allSelect 1010749 ,'N/A' union allSelect 1010751 ,'Yes' union allSelect 1010752, 'Yes' union allSelect 1010752, 'No' union allSelect 1010752, 'N/A' union allSelect 1010753, 'Yes' union allSelect 1010754, 'Yes' union allSelect 1010754, 'No' union allSelect 1010754, 'N/A' union allSelect 1010755, 'Not OK' union allSelect 1010755, 'N/A' union allSelect 1010757, '11111' union allSelect 1010757, '22222' union allSelect 1010757, '33333'-- Make UniqueSelect IDENTITY(int,1,1) as ALERTID,* into #K from @TSelect ALERTS,ROW_TEXT,(Select Count(*) from #K Where ALERTS = Z.ALERTSand ALERTID <= Z.ALERTID) as ROW_IDfrom #K Z--output1010748 Yes 11010748 No 21010748 N/A 31010749 Yes 11010749 No 21010749 N/A 31010751 Yes 11010752 Yes 11010752 No 21010752 N/A 31010753 Yes 11010754 Yes 11010754 No 21010754 N/A 31010755 Not OK 11010755 N/A 21010757 11111 11010757 22222 21010757 33333 3 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-02-01 : 01:01:20
|
quote: Originally posted by webfred In SQL Server 2005 or higher:selectALERTS,ROW_TEXT,ROW_NUMBER() OVER (partition by ALERTS order by ALERTS) AS ROW_IDfrom tblAlerts Webfred No, you're never too old to Yak'n'Roll if you're too young to die.
will this make sure Yes will always be given value of 1,No 2,... |
 |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2009-02-01 : 01:25:18
|
Yes it will.quote: Originally posted by visakh16
quote: Originally posted by webfred In SQL Server 2005 or higher:selectALERTS,ROW_TEXT,ROW_NUMBER() OVER (partition by ALERTS order by ALERTS) AS ROW_IDfrom tblAlerts Webfred No, you're never too old to Yak'n'Roll if you're too young to die.
will this make sure Yes will always be given value of 1,No 2,...
|
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-02-01 : 01:32:33
|
| how? the code's ordering by ALERTS field itself. so on what basis does it apply rownumbering over a ALERT value group? how can we always guarantee that yes will get 1 |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2009-02-01 : 11:22:16
|
Hi Visabot The OP did not ask for a special ordering explicit!GreetingsWebfred No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
jay_sql
Starting Member
3 Posts |
Posted - 2009-02-01 : 15:10:05
|
| Thanks Webfred, row_number() with partition just works fine.Thanks again |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2009-02-01 : 15:15:34
|
Welcome  No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
|
|
|
|
|