2015-05-07 73 views
2

我有一個有趣的問題,我想爲一個表格生成一個8位唯一ID。我有一列中的前四位數字,任務是將另外四位數字附加到現有的四位數字上。以下是條件和樣本數據。根據三列引入4位數字

前4位被在此列 「AFFECTSID_NEW」 從波紋管的樣本數據

發現

示例數據:

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 

例:

爲了產生第5位和第6位

  1. 爲相同的 「Affectsid_new」 和獨特的 「activityname」 它應該是01,02,03 ...
  2. 非獨特的 「activityname」 和「活動名稱=動作名稱「它應該是上述點的延續數字1如果」Affectsid_new「也是非唯一的或i T應當是「」如果「Affectsid_new」是唯一

一旦第5和第6位數字生成這些是產生的第7和第8位的條件

  1. 對於相同的「Affectsid_new」和唯一的「活動名稱」它應該是 是「 「
  2. 非獨特的」 activityname 「和」 activityname=actionname 「應該是」 「
  3. 非獨特的」 activityname 「和」 activitynameactionname「(不等於)它應該是從上述點2(02,03,04 ..)的連續數字。)如果 「Affectsid_new」 也非唯一的或者它應該是 「」 如果 「Affectsid_new」 是獨特

預期的結果是如下

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 
+1

所以,這RDBMS? – Strawberry

+0

它是mysql或sql server嗎? – Deepshikha

+0

那麼,哪個RDBMS?它的MySQL或SQL服務器? –

回答

2
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 
+0

儘管這段代碼可能會回答這個問題,但幾句解釋將有助於當前和未來的讀者更好地理解這個答案。 – Thom

+0

@Anon精彩!謝謝:) –

+0

什麼地獄。你聲明它的mysql,你從標籤列表中刪除sql-server,然後接受一個sql-server的答案? –

0

請嘗試以下未註釋的查詢。我試圖實現你所需要的,但結果與你在這裏給出的例子不完全一樣。結果取決於您選擇訂購的列:

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 

以下是表創建腳本(以防萬一)。

--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') 

希望這有助於...