SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to add item No. in SELECT


How to add item No. in SELECT

Author
Message
RZ52
RZ52
Valued Member
Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)

Group: General Forum Members
Points: 52 Visits: 74
Hello,

I have a table like :


DECLARE @TEST1 TABLE (C_NAME varchar(10), REQ_ITEM VARCHAR(5))
INSERT INTO @TEST1(C_NAME,REQ_ITEM) VALUES('John', 'Item1')
INSERT INTO @TEST1(C_NAME,REQ_ITEM) VALUES('John', 'Item2')
INSERT INTO @TEST1(C_NAME,REQ_ITEM) VALUES('Jake', 'Item2')
INSERT INTO @TEST1(C_NAME,REQ_ITEM) VALUES('Jake', 'Item3')
INSERT INTO @TEST1(C_NAME,REQ_ITEM) VALUES('Jake', 'Item4')
INSERT INTO @TEST1(C_NAME,REQ_ITEM) VALUES('Jake', 'Item5')
INSERT INTO @TEST1(C_NAME,REQ_ITEM) VALUES('Joe', 'Item1')
INSERT INTO @TEST1(C_NAME,REQ_ITEM) VALUES('Joe', 'Item5')
INSERT INTO @TEST1(C_NAME,REQ_ITEM) VALUES('Jane', 'Item3')



How can I show something like :


C_NAME ITEM_NO REQ_ITEM
-------------------------------------
John 01 Item1
John 02 Item2
Jake 01 Item2
Jake 02 Item3
Jake 03 Item4
Jake 04 Item5
Joe 01 Item1
Joe 02 Item5
Jane 01 Item3


Thanks in advance.
Deque
Deque
Valued Member
Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)

Group: General Forum Members
Points: 57 Visits: 1744
Does this do what you are looking for?

SELECT C_NAME, ROW_NUMBER() OVER(PARTITION BY C_NAME ORDER BY C_NAME ASC) AS ITEM_NO, REQ_ITEM FROM @Test1


RZ52
RZ52
Valued Member
Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)

Group: General Forum Members
Points: 52 Visits: 74
Deque (3/7/2013)
Does this do what you are looking for?

SELECT C_NAME, ROW_NUMBER() OVER(PARTITION BY C_NAME ORDER BY C_NAME ASC) AS ITEM_NO, REQ_ITEM FROM @Test1



Yep, It worked.

Thanks Deque
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search