UPDATE:
Poprawa odpowiedź -
DECLARE @temp TABLE
(
SetId VARCHAR(9)
, AppCode CHAR(4)
, AppEventId INT
, EventId INT
, FieldId TINYINT
, ValueData VARCHAR(50)
)
INSERT INTO @temp (SetId, AppCode, AppEventId, EventId, FieldId, ValueData)
VALUES
('2012/2013', '1000', 361616, 16, 1, 'UNI'),
('2012/2013', '1000', 361616, 16, 2, 'Isolation'),
('2012/2013', '1000', 361616, 16, 3, 'DN'),
('2012/2013', '1050', 378194, 16, 1, 'BUL'),
('2012/2013', '1050', 378194, 16, 2, 'Isolation'),
('2012/2013', '1050', 378194, 16, 3, 'RD')
;WITH tbl AS
(
SELECT
PK = t.SetId + t.AppCode + CAST(AppEventId AS VARCHAR(10)) + CAST(EventId AS VARCHAR(5))
, t.SetId
, t.AppCode
, t.AppEventId
, t.EventId
, t.FieldId
, t.ValueData
FROM @temp t
)
SELECT DISTINCT
t.SetId
, t.AppCode
, t.AppEventId
, t.EventId
, t2.ValueData
, t3.ValueData
, t4.ValueData
FROM tbl t
JOIN tbl t2 ON t.PK = t2.PK AND t2.FieldId = 1
JOIN tbl t3 ON t.PK = t3.PK AND t3.FieldId = 2
JOIN tbl t4 ON t.PK = t4.PK AND t4.FieldId = 3
Nie eleganckie, ale działa -
DECLARE @temp TABLE
(
SetId VARCHAR(9)
, AppCode CHAR(4)
, AppEventId INT
, EventId INT
, FieldId TINYINT
, ValueData VARCHAR(50)
)
INSERT INTO @temp (SetId, AppCode, AppEventId, EventId, FieldId, ValueData)
VALUES
('2012/2013', '1000', 361616, 16, 1, 'UNI'),
('2012/2013', '1000', 361616, 16, 2, 'Isolation'),
('2012/2013', '1000', 361616, 16, 3, 'DN'),
('2012/2013', '1050', 378194, 16, 1, 'BUL'),
('2012/2013', '1050', 378194, 16, 2, 'Isolation'),
('2012/2013', '1050', 378194, 16, 3, 'RD')
SELECT t.SetId
, t.AppCode
, t.AppEventId
, t.EventId
, t2.ValueData
, t3.ValueData
, t4.ValueData
FROM (
SELECT
t.SetId
, t.AppCode
, t.AppEventId
, t.EventId
, FieldId1 = MAX(CASE WHEN t.FieldId = 1 THEN t.FieldId END)
, FieldId2 = MAX(CASE WHEN t.FieldId = 2 THEN t.FieldId END)
, FieldId3 = MAX(CASE WHEN t.FieldId = 3 THEN t.FieldId END)
FROM @temp t
GROUP BY
t.SetId
, t.AppCode
, t.AppEventId
, t.EventId
) t
JOIN @temp t2 ON t.SetId = t2.SetId
AND t.AppCode = t2.AppCode
AND t.AppEventId = t2.AppEventId
AND t.EventId = t2.EventId
AND t2.FieldId = 1
JOIN @temp t3 ON t.SetId = t3.SetId
AND t.AppCode = t3.AppCode
AND t.AppEventId = t3.AppEventId
AND t.EventId = t3.EventId
AND t3.FieldId = 2
JOIN @temp t4 ON t.SetId = t4.SetId
AND t.AppCode = t4.AppCode
AND t.AppEventId = t4.AppEventId
AND t.EventId = t4.EventId
AND t4.FieldId = 3
** PIVOT ** Jest odpowiedzią. Pokaż nam, co próbujesz. – Luv
Witam, wierzę, że Twoja tabela nie spełnia reguł normalizacji. powinieneś najpierw przemyśleć swoją strukturę tabeli. –
Dzięki Luv zajrzę do PIVOT. – dawsonz