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 2008 Forums
 Transact-SQL (2008)
 Query Help

Author  Topic 

sqlfresher2k7
Aged Yak Warrior

623 Posts

Posted - 2012-02-22 : 15:51:59
I want to generate the row numbers for the below..


declare @TableVar table (
PID Varchar(5) NOT NULL,
Pval Varchar(10) NULL
)

Insert Into @TableVar (PID,Pval)
(
Select '100C','I'
UNION ALL
Select '100C','S'
UNION ALL
Select '100C','K'
UNION ALL
Select '100C','P'
UNION ALL
Select '100D','P'
)

Select *
from @TableVar

Expected output:

PID Pval Rowno
---- ---- -----
100C I 1
100C S 4
100C K 2
100C P 3
100D P 1

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2012-02-22 : 15:57:18
Use the ROW_NUMBER() function.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

sqlfresher2k7
Aged Yak Warrior

623 Posts

Posted - 2012-02-22 : 16:03:10
Thanks I got it with Row_number function..
Go to Top of Page
   

- Advertisement -