Jeg ved, at du ikke bruger MSSQL, men dette koncept kan måske hjælpe dig.
Jeg er ikke sikker, men i stedet for STRING_AGG
du skal muligvis bruge LISTAGG
. Jeg prøver bare at formidle konceptet her.
CREATE PROCEDURE PivotMyTable
AS
BEGIN
DECLARE @cols NVARCHAR(MAX) = '';
DECLARE @sql NVARCHAR(MAX) = 'select * from (select competency_id from competency_tab t) pivot (count(competency_id) for competency_id in (##COLUMS##))'
WITH T
AS
(SELECT DISTINCT
competency_id
FROM competency_tab)
SELECT
@cols = STRING_AGG(QUOTENAME(T.competency_id, ''''), ',')
FROM T
SET @sql = REPLACE(@sql, '##COLUMNS##', @cols);
EXEC @sql;
END