79668745

Date: 2025-06-17 08:59:13
Score: 0.5
Natty:
Report link

Using PIVOT

SELECT * 
FROM(
SELECT id_request, Alert_Outcome, ROW_NUMBER() OVER(PARTITION BY id_request ORDER BY Alert_Outcome) AS rn
FROM Test
)
PIVOT (MAX(Alert_Outcome) FOR rn IN (1 AS Alert1, 2 AS Alert2, 3 AS Alert3))
ORDER BY id_request;

Using Conditional Aggregation

SELECT
 id_request,
 MAX(CASE WHEN rn = 1 THEN Alert_Outcome END) AS Alert1,
 MAX(CASE WHEN rn = 2 THEN Alert_Outcome END) AS Alert2,
 MAX(CASE WHEN rn = 3 THEN Alert_Outcome END) AS Alert3
FROM(
SELECT id_request, Alert_Outcome, ROW_NUMBER() OVER(PARTITION BY id_request ORDER BY Alert_Outcome) AS rn
FROM Test
) AS D
GROUP BY id_request
ORDER BY id_request

The result is below Result

Reasons:
  • Probably link only (1):
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Low reputation (0.5):
Posted by: SQL006