Reputation: 85
I have an interesting problem, I would like to generate an 8 digit unique ID for a table. I have the first 4 digits in a column and the task is to attach another four digits to the existing 4 digits. The following are the conditions and sample data.
The first four digits are found in this column "AFFECTSID_NEW" from the bellow sample data
Sample Data:
AFFECTSID_NEW | activityname | actionname
---------------------------------------------------------------
2301 | Default Proposed Activity | sample 2
2301 | Communicating welcome pack | sample 1
1206 | Execute events (7 Events) | International Trade Seminar
1206 | Execute events (8 Events) | Workshop with one law firm
1206 | Execute events (8 Events) | Workshop with International Speaker
1206 | Execute events (8 Events) | Seminar with Government agency
1206 | Execute events (8 Events) | Execute events (8 Events)
1205 | Resolve commercial disputes | Resolve commercial disputes
Cases:
For generating the 5th and 6th digit
Once 5th and 6th digits are generated these are the conditions for generating the 7th and 8th digit
The Expected result is as follows
AFFECTSID_NEW | activityname | actionname | Expected
---------------------------------------------------------------------------------------------------
2301 | Default Proposed Activity | sample 2 | 23010101
2301 | Communicating welcome pack | sample 1 | 23010201
1206 | Execute events (7 Events) | International Trade Seminar | 12060101
1206 | Execute events (8 Events) | Workshop with one law firm | 12060202
1206 | Execute events (8 Events) | Workshop with International Speaker | 12060203
1206 | Execute events (8 Events) | Seminar with Government agency | 12060204
1206 | Execute events (8 Events) | Execute events (8 Events) | 12060201
1205 | Resolve commercial disputes | Resolve commercial disputes | 12050101
Upvotes: 2
Views: 73
Reputation: 2825
Please try the following uncommented query. I have tried to implement what you need but the result would not be exactly the same as what you have given here as an example. The result depends upon which column you choose to order:
SELECT *,
(AFFECTSID_NEW +
RIGHT('00' + CONVERT(NVARCHAR, RANK() OVER (PARTITION BY AFFECTSID_NEW ORDER BY activityname)), 2) +
RIGHT('00' + CONVERT(NVARCHAR, RANK() OVER (PARTITION BY activityname ORDER BY actionname)), 2)) AS Expected
FROM #TempTable
Following is the table creation scripts (Just in case).
--CREATE TABLE #TempTable (AFFECTSID_NEW NVARCHAR(8), activityname NVARCHAR(256), actionname NVARCHAR(256))
--INSERT INTO #TempTable
--VALUES
--('2301', 'Default Proposed Activity', 'sample 2'),
--('2301', 'Communicating welcome pack', 'sample 1'),
--('1206', 'Execute events (7 Events)', 'International Trade Seminar'),
--('1206', 'Execute events (8 Events)', 'Workshop with one law firm'),
--('1206', 'Execute events (8 Events)', 'Workshop with International Speaker'),
--('1206', 'Execute events (8 Events)', 'Seminar with Government agency'),
--('1206', 'Execute events (8 Events)', 'Execute events (8 Events)'),
--('1205', 'Resolve commercial disputes', 'Resolve commercial disputes')
Hope this helps...
Upvotes: 0
Reputation: 10908
SELECT
*
,AFFECTSID_NEW
+ REPLACE(STR(DENSE_RANK() OVER(PARTITION BY AFFECTSID_NEW ORDER BY activityname),2),' ','0')
+ REPLACE(STR(ROW_NUMBER() OVER(PARTITION BY AFFECTSID_NEW,activityname ORDER BY actionname),2),' ','0')
FROM MyTable
Upvotes: 2