Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Increment data while select


Increment data while select

Author
Message
tlampe
tlampe
Forum Newbie
Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)

Group: General Forum Members
Points: 1 Visits: 11
Hello,

I am having an issue with a select statement I am trying to run. I need to select some data from a table, which has multiple records for one customer ID. I need to add a static column in my select, and then incrememnt that column for each record per customer ID.

If I create this Table:
USE YourDB
GO
INSERT INTO MyTable (FirstCol, SecondCol)
VALUES ('Waverider',1);
INSERT INTO MyTable (FirstCol, SecondCol)
VALUES ('Psalms',2);
INSERT INTO MyTable (FirstCol, SecondCol)
VALUES ('WRV',3);
INSERT INTO MyTable (FirstCol, SecondCol)
VALUES ('18th Street',3);
INSERT INTO MyTable (FirstCol, SecondCol)
VALUES ('Soundside',3);
GO

Then try this Select:
Select
FirstCol,
SecondCol,
'100' as ThirdCol
From MyTable

I want to increment the ThirdCol by 100 where the SecondCol is the same?
GSquared
GSquared
SSCoach
SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)

Group: General Forum Members
Points: 15819 Visits: 9729
I'm not clear on what you want. Can you show an example?

- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Evil Kraig F
Evil Kraig F
SSCertifiable
SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)

Group: General Forum Members
Points: 6259 Visits: 7660
I think you're looking for this (Please note how I adjusted your build to create the full test script):

DECLARE @MyTable TABLE ( FirstCol VARCHAR(50), SecondCol INT)

INSERT INTO @MyTable (FirstCol, SecondCol)
VALUES ('Waverider',1);
INSERT INTO @MyTable (FirstCol, SecondCol)
VALUES ('Psalms',2);
INSERT INTO @MyTable (FirstCol, SecondCol)
VALUES ('WRV',3);
INSERT INTO @MyTable (FirstCol, SecondCol)
VALUES ('18th Street',3);
INSERT INTO @MyTable (FirstCol, SecondCol)
VALUES ('Soundside',3);


Select
FirstCol,
SecondCol,
ROW_NUMBER() OVER ( PARTITION BY SecondCol ORDER BY FirstCol) * 100 AS Incrementor
From
@MyTable
ORDER BY
SecondCol, FirstCol




- Craig Farrell

Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

For better assistance in answering your questions | Forum Netiquette
For index/tuning help, follow these directions. |Tally Tables

Twitter: @AnyWayDBA
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