Demo: http://rextester.com/IYDJ29385
- CTE daje mi przykładowe dane do zabawy
- CTE2 po prostu przypisuje numer wiersza do każdego przydziału i Indywidualnego (można użyć widoku liniowego)
- Następnie używamy instrukcji case, aby przestawić dane na podstawie wygenerowanych numerów wierszy. Upadek tego podejścia polega na tym, że wszystkie 14 kolumn są zawsze zwracane. Używając dynamicznego SQL, możesz wyświetlać te kolumny tylko wtedy, gdy są potrzebne.
Zakłada się, że atrybut assignId oraz personId i projectcode są unikalne. Gdyby istniało wiele kodów projektu dla tego samego zadania i osoby, musielibyśmy zrobić coś innego zamiast max.
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
dając nam:
+----+--------------+----------+--------------+---------------------+--------------+---------------------+--------------+---------------------+--------------+---------------------+--------------+---------------------+--------------+---------------------+--------------+---------------------+
| | 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 |
+----+--------------+----------+--------------+---------------------+--------------+---------------------+--------------+---------------------+--------------+---------------------+--------------+---------------------+--------------+---------------------+--------------+---------------------+
Co określa rozkazy rekordu? Dlaczego 520 projectcode2 zamiast 555 lub 621? Losowy? nie ma znaczenia? – xQbert
@xQbert Dziękujemy za wyjaśnienie. kolejność rekordów nie ma znaczenia. –