Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Incrementing Existing Column Values Expand / Collapse
Author
Message
Posted Thursday, February 13, 2014 8:12 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, February 17, 2014 1:03 PM
Points: 14, Visits: 43
I need to insert new rows in a table that has a column for sequence number values (attrib_seq) for each unique id_num. id_nums in the table have differing maximum attrib_seq values depending on the number of records for that id_num. I need the newly inserted columns to have the next highest seq_num value for each id_num. The code I have now works if only one record is inserted for a particular ID. However, if there are multiples records to insert for that ID it maintains the same value and the insert crashes. Below is the select portion of my insert. Any assistance is much appreciated.

SELECT w.id_num,
ISNULL((
SELECT MAX(a.ATTRIB_SEQ)
FROM dbo.ATTRIBUTE_TRANS a
WHERE w.ID_NUM = a.ID_NUM
GROUP BY a.ID_NUM
)+1, 1) AS MAX_SEQ_NUM
,attribute_cde,
'ADMIN',
'attribute_insert',
CURRENT_TIMESTAMP
FROM WS_Table w
Post #1541222
Posted Thursday, February 13, 2014 8:27 AM This worked for the OP Answer marked as solution


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Wednesday, October 15, 2014 8:56 AM
Points: 592, Visits: 925
Please read the first article in my signature about posting these types of questions to the forum. I have added the tables and some sample data for you and using the window function ROW_NUMBER() to solve the issue. Let me know if this works for you:
drop table ATTRIBUTE_TRANS;
drop table WS_Table;
go

create table ATTRIBUTE_TRANS (
ID_NUM int
, Attrib_seq int)

create table WS_Table (
id_num int
, attribute_cde int
);

insert ATTRIBUTE_TRANS
values (1,2)
, (2,1);

insert WS_Table
values (1,1)
, (2,1)
, (1,2)
, (3,1);


SELECT w.id_num,
ISNULL((SELECT MAX(a.ATTRIB_SEQ)
FROM dbo.ATTRIBUTE_TRANS a
WHERE w.ID_NUM = a.ID_NUM
GROUP BY a.ID_NUM
)+ROW_NUMBER() over (partition by w.id_num order by w.id_num), 1) AS MAX_SEQ_NUM
, attribute_cde
, 'ADMIN'
, 'attribute_insert'
, CURRENT_TIMESTAMP
FROM WS_Table w

EDIT: I forgot to add partition by in ROW_NUMBER




Microsoft Certified Master - SQL Server 2008
Follow me on twitter: @keith_tate

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1541236
Posted Monday, February 17, 2014 1:03 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, February 17, 2014 1:03 PM
Points: 14, Visits: 43
Thanks much for your solution, much appreciated.
Post #1542258
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse