Solved

Insert an incremented value in SQL?

Posted on 2011-03-16
4
357 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

743 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

13 Experts available now in Live!

Get 1:1 Help Now