From f6b9cbf8d1cf6643494111d4dee69a5c9192c939 Mon Sep 17 00:00:00 2001 From: jiefei Date: Tue, 17 Dec 2024 21:11:25 +0800 Subject: [PATCH] [Plsql] pivot_clause and unpivot_clause can have own alias --- sql/plsql/PlSqlParser.g4 | 4 ++-- sql/plsql/examples/pivot14.sql | 9 +++++++++ sql/plsql/examples/pivot15.sql | 6 ++++++ 3 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 sql/plsql/examples/pivot14.sql create mode 100644 sql/plsql/examples/pivot15.sql diff --git a/sql/plsql/PlSqlParser.g4 b/sql/plsql/PlSqlParser.g4 index 44785afbff..1b9c64fc6e 100644 --- a/sql/plsql/PlSqlParser.g4 +++ b/sql/plsql/PlSqlParser.g4 @@ -5988,7 +5988,7 @@ flashback_query_clause ; pivot_clause - : PIVOT XML? '(' pivot_element (',' pivot_element)* pivot_for_clause pivot_in_clause ')' + : PIVOT XML? '(' pivot_element (',' pivot_element)* pivot_for_clause pivot_in_clause ')' table_alias? ; pivot_element @@ -6013,7 +6013,7 @@ pivot_in_clause_elements ; unpivot_clause - : UNPIVOT ((INCLUDE | EXCLUDE) NULLS)? '(' (column_name | paren_column_list) pivot_for_clause unpivot_in_clause ')' + : UNPIVOT ((INCLUDE | EXCLUDE) NULLS)? '(' (column_name | paren_column_list) pivot_for_clause unpivot_in_clause ')' table_alias? ; unpivot_in_clause diff --git a/sql/plsql/examples/pivot14.sql b/sql/plsql/examples/pivot14.sql new file mode 100644 index 0000000000..50339de626 --- /dev/null +++ b/sql/plsql/examples/pivot14.sql @@ -0,0 +1,9 @@ +SELECT s2.id, name, subject, score +FROM scores2 s1 + UNPIVOT ( + score FOR subject IN ( + chinese_score AS 'Chinese', + math_score AS 'Math', + english_score AS 'English' + ) +) s2; \ No newline at end of file diff --git a/sql/plsql/examples/pivot15.sql b/sql/plsql/examples/pivot15.sql new file mode 100644 index 0000000000..5bd1394a94 --- /dev/null +++ b/sql/plsql/examples/pivot15.sql @@ -0,0 +1,6 @@ +SELECT * +FROM (SELECT id, name, subject, score FROM scores) s1 + PIVOT ( + MAX(score) + FOR subject IN ('Chinese', 'Math', 'English') +) s2;