Skip to content

SQL-457: How to return pivot table output? #369

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
INSERT INTO Teaching (id, role, semester, year, course_id, faculty_id)
VALUES
(613, 'Instructor', 'SUMMER', 2023, 'CS111', 111),
(614, 'TA', 'SUMMER', 2023, 'CS111', 300),
(615, 'Instructor', 'SUMMER', 2023, 'CS112', 121),
(616, 'TA', 'SUMMER', 2023, 'CS112', 301);

INSERT INTO Teaching (id, role, semester, year, course_id, faculty_id)
VALUES
(617, 'Instructor', 'WINTER', 2023, 'CS113', 221),
(618, 'TA', 'WINTER', 2023, 'CS113', 302),
(619, 'Instructor', 'WINTER', 2023, 'CS121', 131);
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
SET @sql = NULL;
SELECT GROUP_CONCAT(DISTINCT CONCAT(
'COUNT(CASE WHEN semester = ''', semester, ''' THEN 1 END) AS `', semester, '`' ) )
INTO @sql
FROM Teaching;
SET @sql = CONCAT('SELECT role, ', @sql, '
FROM Teaching
GROUP BY role');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
DO $$
DECLARE
cols TEXT;
dyn_sql TEXT;
BEGIN
SELECT string_agg(
format('COALESCE(MAX(CASE WHEN semester = %L THEN count END), 0) AS "%s"', semester, semester),
', ' ) INTO cols
FROM (SELECT DISTINCT semester FROM Teaching) s;
dyn_sql := format(
$f$
SELECT role, %s
FROM ( SELECT role, semester, COUNT(*) AS count FROM Teaching GROUP BY role, semester ) AS sub
GROUP BY role
ORDER BY role;
$f$, cols);
RAISE NOTICE 'Run this SQL: %', dyn_sql;
END $$;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
DECLARE @columns NVARCHAR(MAX), @sql NVARCHAR(MAX);
SELECT @columns = STRING_AGG(QUOTENAME(semester), ', ')
FROM (SELECT DISTINCT semester FROM Teaching) AS semesters;
SET @sql = '
SELECT role, ' + @columns + '
FROM ( SELECT role, semester FROM Teaching ) AS source
PIVOT ( COUNT(semester) FOR semester IN (' + @columns + ') ) AS pivot_table;
';
EXEC sp_executesql @sql;
6 changes: 6 additions & 0 deletions table-operations/pivot-tables/pivoting-in-mysql.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
SELECT
role,
COUNT(CASE WHEN semester = 'SPRING' THEN 1 END) AS spring_count,
COUNT(CASE WHEN semester = 'FALL' THEN 1 END) AS fall_count
FROM teaching
GROUP BY role;
13 changes: 13 additions & 0 deletions table-operations/pivot-tables/pivoting-in-postgresql.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-- Enabling the tablefunc extension so we can use crosstab()
CREATE EXTENSION IF NOT EXISTS tablefunc;

SELECT *
FROM crosstab(
$$
SELECT role, semester, COUNT(*)
FROM Teaching
GROUP BY role, semester
ORDER BY role, semester
$$,
$$ VALUES ('SPRING'), ('FALL') $$
) AS ct(role TEXT, spring_count INT, fall_count INT);
11 changes: 11 additions & 0 deletions table-operations/pivot-tables/pivoting-in-sql-server.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
SELECT [role],
[SPRING] AS spring_count,
[FALL] as fall_count
FROM (
SELECT [role], semester
FROM Teaching
) AS source
PIVOT (
COUNT(semester)
FOR semester IN ([SPRING], [FALL])
) AS pivot_table;