| Author |
Topic |
|
NguyenL71
Posting Yak Master
228 Posts |
Posted - 2011-01-20 : 11:55:25
|
| [code]I need to update/insert sequence number into one of the column. Please see the output below and how to construct the correct query to produce the desire output. SQL 2008Thank you in advance for your help.IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Test]') AND type in (N'U'))DROP TABLE [dbo].[Test]GOCREATE TABLE [dbo].[Test]( [JobId] [int] IDENTITY(1,1) NOT NULL, [OrderId] [int] NOT NULL, [JobTypeCode] [varchar](10) NOT NULL, [JobSeq] [varchar](4) NOT NULL) ON [PRIMARY]GOBEGIN TRANSACTION;INSERT INTO [dbo].[Test]([JobId], [OrderId], [JobTypeCode], [JobSeq])SELECT 1, 7, N'Review', N'0001' UNION ALLSELECT 2, 7, N'Cure', N'0002' UNION ALLSELECT 3, 7, N'Cure', N'0003' UNION ALLSELECT 5, 8, N'Review', N'0001' UNION ALLSELECT 6, 8, N'Cure', N'0002' UNION ALLSELECT 7, 8, N'Cure', N'0003' UNION ALLSELECT 8, 8, N'Cure', N'0004' UNION ALLSELECT 9, 8, N'Cure', N'0005' UNION ALLSELECT 11, 1, N'Review', N'' UNION ALLSELECT 12, 1, N'Cure', N'' UNION ALLSELECT 13, 1, N'Cure', N'' UNION ALLSELECT 14, 1, N'Cure', N'' UNION ALLSELECT 15, 1, N'Cure', N'' UNION ALLSELECT 16, 1, N'Cure', N'' UNION ALLSELECT 17, 1, N'Cure', N'' UNION ALLSELECT 18, 1, N'Cure', N'' UNION ALLSELECT 19, 1, N'Cure', N'' UNION ALLSELECT 20, 1, N'Cure', N'' UNION ALLSELECT 21, 1, N'Cure', N''COMMIT;RAISERROR (N'[dbo].[Test]: Insert Batch: 1.....Done!', 10, 1) WITH NOWAIT;GO SELECT JobId, OrderId, JobTypeCode, '000' + CAST(ROW_NUMBER() OVER(PARTITION BY OrderId ORDER BY OrderId ) AS VARCHAR(4)) AS 'JobSeq' --,JobSeq FROM Test GOJobId OrderId JobTypeCode JobSeq----------- ----------- ----------- -------11 1 Review 000112 1 Cure 000213 1 Cure 000314 1 Cure 000415 1 Cure 000516 1 Cure 000617 1 Cure 000718 1 Cure 000819 1 Cure 000920 1 Cure 00010 -- Wrong should be 001021 1 Cure 00011 -- same should be 00111 7 Review 00012 7 Cure 00023 7 Cure 00035 8 Review 00016 8 Cure 00027 8 Cure 00038 8 Cure 00049 8 Cure 0005Desired output:JobId OrderId JobTypeCode JobSeq----------- ----------- ----------- -------11 1 Review 000112 1 Cure 000213 1 Cure 000314 1 Cure 000415 1 Cure 000516 1 Cure 000617 1 Cure 000718 1 Cure 000819 1 Cure 000920 1 Cure 0010 <------- Need to replacing only 4 digits.21 1 Cure 0011 <------- same.22 1 Cure 0230 <------- just a sample....1 7 Review 00012 7 Cure 00023 7 Cure 00035 8 Review 00016 8 Cure 00027 8 Cure 00038 8 Cure 00049 8 Cure 0005[/code] |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2011-01-20 : 12:10:05
|
| SELECT JobId, OrderId, JobTypeCode, RIGHT('000' + CAST(ROW_NUMBER() OVER(PARTITION BY OrderId ORDER BY OrderId ) AS VARCHAR(4)) ,4) AS 'JobSeq' --,JobSeq FROM #TestJimEveryday I learn something that somebody else already knew |
 |
|
|
NguyenL71
Posting Yak Master
228 Posts |
Posted - 2011-01-20 : 12:28:04
|
Thanks Jim. Couldn't think that's simple.quote: Originally posted by jimf SELECT JobId, OrderId, JobTypeCode, RIGHT('000' + CAST(ROW_NUMBER() OVER(PARTITION BY OrderId ORDER BY OrderId ) AS VARCHAR(4)) ,4) AS 'JobSeq' --,JobSeq FROM #TestJimEveryday I learn something that somebody else already knew
|
 |
|
|
|
|
|