Solved

Insert an incremented value in SQL?

Posted on 2011-03-16
4
360 Views
Last Modified: 2012-08-13
Hi.  I have a stored procedure that returns a recordset of customers from two customer tables.  I gather the records into a single temp table, as shown in the attached code.

First, the final resultset must be ordered by customer name.  Next, I have a column called 'GroupID'.  'GroupID' needs to be set to an incremented integer --  however, it needs increment every 25 rows.  So, 'GroupID' for the first 25 rows returned needs to be set to 1, the next 25 rows need to be set to 2, and so on for the next 25000+ rows.

I hope I am being clear ... I have simplified my existing procedure to a bare minimum in the example attached.

Any suggestions about how to do this in MS SQL?
Thanks.

create table #Temp1
   (TEMP1_Id int IDENTITY (1, 1) NOT NULL,
    Cust_Name  varchar(50),
    GroupID    integer null)
		 
insert into #Temp1 (Cust_Name)
    Select Cust_Name from CustTable1

insert into #Temp1 (Cust_Name)
    Select Cust_Name from CustTable2
	
------------------------------------------
-- need some UPDATE #Temp1 code in here ...
-- probably need to 'order by Cust_Name' here 
------------------------------------------

-- return the final resultset ...
select * from #Temp1
order by Cust_Name

Open in new window

0
Comment
Question by:Rob Rudloff
4 Comments
 
LVL 13

Expert Comment

by:devlab2012
ID: 35149151
you cannot do it using IDENTITY. You can create a stored procedure for inserting records. In the stored procedure, count the number of records and set the value of groupid based upon the total number of rows/25.
0
 
LVL 4

Assisted Solution

by:LeDaouk
LeDaouk earned 25 total points
ID: 35149201
also before the last select you can add the attached code to update the group ID field
DECLARE DECLARE  @Min  numeric,@Max  numeric, @Count  numeric,@Seq  numeric, @TempSeq  numeric
	SET @Count = (Select count(TEMP1_Id) From #Temp1)
	SET @Min = (SELECT MIN TEMP1_Id FROM  #Temp1)
	SET @Max = (SELECT MIN TEMP1_Id FROM  #Temp1)
	SET @Seq = 1
	SET @TempSeq = 1
	WHILE @Min <= @Max
		BEGIN
			UPDATE #TEMP1 SET GroupID = @Seq WHERE Temp1_ID = @Min
			SET @Seq = 1
			SET @TempSeq = @TempSeq +1
			IF @TempSeq  = 26
			BEGIN
				SET @TempSeq = 1
				SET @Seq = @Seq +1 
			END
			Set @Min = @Min+1

		End

Open in new window

0
 
LVL 51

Accepted Solution

by:
HainKurt earned 225 total points
ID: 35149236
no need for identity, temptable etc etc... try this

with Cust as (
Select 1 ord, Cust_Name from CustTable1
union
Select 2 ord, Cust_Name from CustTable2
)
select rn, rn/25 as GroupID, cust_name
from (select *, ROW_NUMBER() over (Order by ord, cust_name) rn from cust) x
0
 

Author Comment

by:Rob Rudloff
ID: 35150147
Thanks -- I started with LaDaouk's suggestions, and ended up using HainKurt's idea.

With HainKurts code, I started with:  
select *, ((ROW_NUMBER() over (Order by cust_name))/25) as GroupID    from #temp1

Open in new window

This presented rows 1-24 in group 0, and 25-49 in group 1.

After that I adjusted it a bit, so that rows 1-25 were in group 1, rows 26-50 in group 2:
select *, (((ROW_NUMBER() over (Order by cust_name))- 1)/25) + 1 as GroupID    from #temp1

Open in new window


Thanks everyone.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now