DEMO不要緊:http://rextester.com/IYDJ29385
- CTE給我的樣本數據與
玩
- CTE2簡單地爲每個assignmentId和PersonId分配一個行號(您可以只使用一個內聯視圖)
- 然後我們使用case語句來基於生成的行號來轉發數據。這種方法的缺點是總是返回14列。使用動態SQL,您只能在需要時顯示這些列。
這確實假定assignmentId和personId和projectcode是唯一的。如果同一個作業和人員可能存在多個項目代碼,那麼我們需要做一些不同的事情,而不是最大化。
WITH CTE (assignmentID, personID, projectCode, projectCodePercent) as (
SELECT 642, 13527, 511, 75.00 UNION ALL
SELECT 642, 13527, 621, 25.00 UNION ALL
SELECT 650, 12000, 555, 50.00 UNION ALL
SELECT 650, 12000, 520, 25.00 UNION ALL
SELECT 650, 12000, 621, 25.00 UNION ALL
SELECT 240, 56000, 721, 100.00),
cte2 as (SELECT A.*, row_number() over (partition by AssignmentID, PersonID order by projectCode) RN
FROM cte A)
SELECT AssignmentID
, PersonID
, max(CASE WHEN RN = 1 then projectCode end) as projectCode1
, max(CASE WHEN RN = 1 then ProjectcodePercent end) as ProjectcodePercent1
, max(CASE WHEN RN = 2 then projectCode end) as projectCode2
, max(CASE WHEN RN = 2 then ProjectcodePercent end) as ProjectcodePercent2
, max(CASE WHEN RN = 3 then projectCode end) as projectCode3
, max(CASE WHEN RN = 3 then ProjectcodePercent end) as ProjectcodePercent3
, max(CASE WHEN RN = 4 then projectCode end) as projectCode4
, max(CASE WHEN RN = 4 then ProjectcodePercent end) as ProjectcodePercent4
, max(CASE WHEN RN = 5 then projectCode end) as projectCode5
, max(CASE WHEN RN = 5 then ProjectcodePercent end) as ProjectcodePercent5
, max(CASE WHEN RN = 6 then projectCode end) as projectCode6
, max(CASE WHEN RN = 6 then ProjectcodePercent end) as ProjectcodePercent6
, max(CASE WHEN RN = 7 then projectCode end) as projectCode7
, max(CASE WHEN RN = 7 then ProjectcodePercent end) as ProjectcodePercent7
FROM CTE2
Group by AssignmentID, personId
給予我們:
+----+--------------+----------+--------------+---------------------+--------------+---------------------+--------------+---------------------+--------------+---------------------+--------------+---------------------+--------------+---------------------+--------------+---------------------+
| | AssignmentID | PersonID | projectCode1 | ProjectcodePercent1 | projectCode2 | ProjectcodePercent2 | projectCode3 | ProjectcodePercent3 | projectCode4 | ProjectcodePercent4 | projectCode5 | ProjectcodePercent5 | projectCode6 | ProjectcodePercent6 | projectCode7 | ProjectcodePercent7 |
+----+--------------+----------+--------------+---------------------+--------------+---------------------+--------------+---------------------+--------------+---------------------+--------------+---------------------+--------------+---------------------+--------------+---------------------+
| 1 | 650 | 12000 | 520 | 25,00 | 555 | 50,00 | 621 | 25,00 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
| 2 | 642 | 13527 | 511 | 75,00 | 621 | 25,00 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
| 3 | 240 | 56000 | 721 | 100,00 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
+----+--------------+----------+--------------+---------------------+--------------+---------------------+--------------+---------------------+--------------+---------------------+--------------+---------------------+--------------+---------------------+--------------+---------------------+
什麼定義記錄訂單?爲什麼520 projectcode2而不是555或621?隨機?沒關係? – xQbert
@xQbert謝謝澄清。記錄順序無關緊要。 –