From 1f59e370d964f54aaac52405a2f20c67a4bb945d Mon Sep 17 00:00:00 2001 From: Yanjie Xu Date: Fri, 26 Jul 2024 01:03:33 +0000 Subject: [PATCH] Support View usage for PIVOT operator Support create/select/drop view on stmt with pivot operator Task: BABEL-4673 Signed-off-by: Yanjie Xu --- contrib/babelfishpg_tsql/runtime/functions.c | 161 +- .../sql/object_definition_tsql.sql | 13 +- .../babelfishpg_tsql/sql/sys_functions.sql | 2 +- .../babelfishpg_tsql--3.6.0--3.7.0.sql | 17 + contrib/babelfishpg_tsql/src/catalog.c | 357 ++- contrib/babelfishpg_tsql/src/catalog.h | 33 + contrib/babelfishpg_tsql/src/dbcmds.c | 2 + contrib/babelfishpg_tsql/src/hooks.c | 131 +- contrib/babelfishpg_tsql/src/pl_handler.c | 1 + .../src/tsqlUnsupportedFeatureHandler.cpp | 15 +- .../JDBC/expected/AUTO_ANALYZE-vu-prepare.out | 1 + test/JDBC/expected/TEST_PUBLICATION.out | 1 + .../babelfish_integrity_checker-vu-verify.out | 1 + .../pivot-before-15_8-or-16_4-vu-cleanup.out | 54 + .../pivot-before-15_8-or-16_4-vu-prepare.out | 862 ++++++++ .../pivot-before-15_8-or-16_4-vu-verify.out | 1938 +++++++++++++++++ test/JDBC/expected/pivot-vu-cleanup.out | 103 +- test/JDBC/expected/pivot-vu-prepare.out | 520 ++++- test/JDBC/expected/pivot-vu-verify.out | 564 ++++- .../pivot-before-15_8-or-16_4-vu-cleanup.sql | 54 + .../pivot-before-15_8-or-16_4-vu-prepare.sql | 404 ++++ .../pivot-before-15_8-or-16_4-vu-verify.sql | 904 ++++++++ test/JDBC/input/pivot-vu-cleanup.sql | 98 +- test/JDBC/input/pivot-vu-prepare.sql | 495 ++++- test/JDBC/input/pivot-vu-verify.sql | 89 +- test/JDBC/upgrade/15_5/schedule | 2 +- test/JDBC/upgrade/15_6/schedule | 2 +- test/JDBC/upgrade/15_7/schedule | 2 +- .../expected_create.out | 2 + .../expected_dependency.out | 1 - 30 files changed, 6656 insertions(+), 173 deletions(-) create mode 100644 test/JDBC/expected/pivot-before-15_8-or-16_4-vu-cleanup.out create mode 100644 test/JDBC/expected/pivot-before-15_8-or-16_4-vu-prepare.out create mode 100644 test/JDBC/expected/pivot-before-15_8-or-16_4-vu-verify.out create mode 100644 test/JDBC/input/pivot-before-15_8-or-16_4-vu-cleanup.sql create mode 100644 test/JDBC/input/pivot-before-15_8-or-16_4-vu-prepare.sql create mode 100644 test/JDBC/input/pivot-before-15_8-or-16_4-vu-verify.sql diff --git a/contrib/babelfishpg_tsql/runtime/functions.c b/contrib/babelfishpg_tsql/runtime/functions.c index 37f96c4ca65..9e58c30b2b7 100644 --- a/contrib/babelfishpg_tsql/runtime/functions.c +++ b/contrib/babelfishpg_tsql/runtime/functions.c @@ -75,6 +75,7 @@ #define DATEPART_MIN_VALUE -53690 /* minimun value for datepart general_integer_datatype */ #define DATEPART_SMALLMONEY_MAX_VALUE 214748.3647 /* maximum value for datepart smallmoney */ #define DATEPART_SMALLMONEY_MIN_VALUE -53690 /* minimum value for datepart smallmoney */ +#define PIVOT_METADATA_COUNT 3 /* total of 3 metadata is needed by bbf_pivot function */ typedef enum { @@ -198,9 +199,10 @@ void *get_host_id(void); Datum datepart_internal(char *field , Timestamp timestamp , float8 df_tz, bool general_integer_datatype); int SPI_execute_raw_parsetree(RawStmt *parsetree, const char *sourcetext, bool read_only, long tcount); -static HTAB *load_categories_hash(RawStmt *cats_sql, const char *sourcetext, MemoryContext per_query_ctx); +static HTAB *load_categories_hash(RawStmt *cats_sql, const char *sourcetext, const char *view_uuid, MemoryContext per_query_ctx); static Tuplestorestate *get_bbf_pivot_tuplestore(RawStmt *sql, const char *sourcetext, + const char *view_uuid, const char *funcName, HTAB *bbf_pivot_hash, TupleDesc tupdesc, @@ -4407,6 +4409,7 @@ Datum bbf_pivot(PG_FUNCTION_ARGS) { ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo; + char *view_uuid = text_to_cstring(PG_GETARG_TEXT_PP(0)); TupleDesc tupdesc; MemoryContext per_query_ctx; MemoryContext oldcontext; @@ -4430,32 +4433,82 @@ bbf_pivot(PG_FUNCTION_ARGS) (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("materialize mode required, but it is not allowed in this context"))); - if (fcinfo->context == NULL || !IsA(fcinfo->context, List) || list_length((List *) fcinfo->context) != 3) - ereport(ERROR, - (errcode(ERRCODE_CHECK_VIOLATION), - errmsg("Babelfish PIVOT is not properly initialized."))); + bbf_pivot_src_sql = NULL; + bbf_pivot_cat_sql = NULL; + query_string = NULL; + funcName = NULL; + + /* + * if view_uuid is not defined (view_uuid is empty string), then we will get the pivot metadata from + * FunctionCallInfo(fcinfo). + */ + if (!view_uuid || strlen(view_uuid) == 0) + { + if (fcinfo->context == NULL || !IsA(fcinfo->context, List) || list_length((List *) fcinfo->context) != PIVOT_METADATA_COUNT) + { + ereport(ERROR, + (errcode(ERRCODE_CHECK_VIOLATION), + errmsg("Babelfish PIVOT is not properly initialized."))); + } + + node = list_nth((List *)fcinfo->context, 0); + if (!IsA(node, List) + || !IsA(list_nth((List *)node, 0), String) + || strcmp(((String *)list_nth((List *)node, 0))->sval, "bbf_pivot_func") != 0) + { + ereport(ERROR, + (errcode(ERRCODE_CHECK_VIOLATION), + errmsg("Babelfish PIVOT is not properly initialized."))); + } - node = list_nth((List *)fcinfo->context, 0); - if (!IsA(node, List) - || !IsA(list_nth((List *)node, 0), String) - || strcmp(((String *)list_nth((List *)node, 0))->sval, "bbf_pivot_func") != 0) + pivot_parsetree = (List *) list_nth((List *) fcinfo->context, 1); + pivot_extrainfo = (List *) list_nth((List *) fcinfo->context, 2); + + if (!IsA(pivot_parsetree, List) || !IsA(pivot_extrainfo, List)) + ereport(ERROR, + (errcode(ERRCODE_CHECK_VIOLATION), + errmsg("Babelfish PIVOT is not properly initialized."))); + + bbf_pivot_src_sql = (RawStmt *) list_nth(pivot_parsetree, 0); + bbf_pivot_cat_sql = (RawStmt *) list_nth(pivot_parsetree, 1); + query_string = ((String *) list_nth(pivot_extrainfo, 0))->sval; + funcName = ((String *) list_nth(pivot_extrainfo, 1))->sval; + } + else { - ereport(ERROR, - (errcode(ERRCODE_CHECK_VIOLATION), - errmsg("Babelfish PIVOT is not properly initialized."))); + /* + * If view_uuid is defined (not empty string), then we know current bbf_pivot function is within + * a view stmt. + * We will not fetch metadata from FunctionCallInfo (fcinfo) but rather get the + * view_uuid from the function argument and get aggregate function argument from babel- + * fish_view_catalog. + */ + Relation rel; + HeapTuple tuple; + SysScanDesc scan; + ScanKeyData scanKey[1]; + + rel = table_open(get_bbf_pivot_view_oid(), RowExclusiveLock); + + ScanKeyInit(&scanKey[0], + Anum_bbf_pivot_view_pivot_view_uuid, + BTEqualStrategyNumber, F_TEXTEQ, + CStringGetTextDatum(view_uuid)); + + scan = systable_beginscan(rel, get_bbf_pivot_view_idx_oid(), + false, NULL, 1, scanKey); + + tuple = systable_getnext(scan); + if (HeapTupleIsValid(tuple)) + { + bool isnull; + funcName = TextDatumGetCString(heap_getattr(tuple, Anum_bbf_pivot_view_agg_func_name, RelationGetDescr(rel), &isnull)); + } + + systable_endscan(scan); + table_close(rel, RowExclusiveLock); } - pivot_parsetree = (List *) list_nth((List *) fcinfo->context, 1); - pivot_extrainfo = (List *) list_nth((List *) fcinfo->context, 2); - - if (!IsA(pivot_parsetree, List) || !IsA(pivot_extrainfo, List)) - ereport(ERROR, - (errcode(ERRCODE_CHECK_VIOLATION), - errmsg("Babelfish PIVOT is not properly initialized."))); - bbf_pivot_src_sql = (RawStmt *) list_nth(pivot_parsetree, 0); - bbf_pivot_cat_sql = (RawStmt *) list_nth(pivot_parsetree, 1); - query_string = ((String *) list_nth(pivot_extrainfo, 0))->sval; - funcName = ((String *) list_nth(pivot_extrainfo, 1))->sval; per_query_ctx = rsinfo->econtext->ecxt_per_query_memory; oldcontext = MemoryContextSwitchTo(per_query_ctx); @@ -4477,7 +4530,7 @@ bbf_pivot(PG_FUNCTION_ARGS) "bbf_pivot function are not compatible"))); /* load up the categories hash table */ - bbf_pivot_hash = load_categories_hash(bbf_pivot_cat_sql, query_string, per_query_ctx); + bbf_pivot_hash = load_categories_hash(bbf_pivot_cat_sql, query_string, view_uuid, per_query_ctx); /* let the caller know we're sending back a tuplestore */ rsinfo->returnMode = SFRM_Materialize; @@ -4485,6 +4538,7 @@ bbf_pivot(PG_FUNCTION_ARGS) /* now go build it */ rsinfo->setResult = get_bbf_pivot_tuplestore(bbf_pivot_src_sql, query_string, + view_uuid, funcName, bbf_pivot_hash, tupdesc, @@ -4507,7 +4561,11 @@ bbf_pivot(PG_FUNCTION_ARGS) * load up the categories hash table */ static HTAB * -load_categories_hash(RawStmt *cats_sql, const char * sourcetext, MemoryContext per_query_ctx) +load_categories_hash(RawStmt *cats_sql, + const char *sourcetext, + const char *view_uuid, + MemoryContext per_query_ctx + ) { HTAB *bbf_pivot_hash; HASHCTL ctl; @@ -4534,8 +4592,30 @@ load_categories_hash(RawStmt *cats_sql, const char * sourcetext, MemoryContext p /* internal error */ elog(ERROR, "load_categories_hash: SPI_connect returned %d", ret); - /* Retrieve the category name rows */ - ret = SPI_execute_raw_parsetree(cats_sql, sourcetext, true, 0); + /* + * Retrieve the category name rows + * + * If view_uuid is defined, then we will use the provided uuid to form the catefory_sql and + * get result. category_sql name format is pvt_cv_[uuid] + * + * if view_uuid is not defined, then we will use the rawparsetree of catefory_sql to get the + * result + */ + if (!view_uuid || strlen(view_uuid) != 0) + { + StringInfoData buf; + + initStringInfo(&buf); + appendStringInfoString(&buf, "select * from "); + appendStringInfoString(&buf, BBF_PIVOT_VIEW_CAT_PREFIX); + appendStringInfoString(&buf, view_uuid); + + ret = SPI_execute(buf.data, true, 0); + } + else + { + ret = SPI_execute_raw_parsetree(cats_sql, sourcetext, true, 0); + } tuple_processed = SPI_processed; /* Check for qualifying tuples */ @@ -4602,6 +4682,7 @@ load_categories_hash(RawStmt *cats_sql, const char * sourcetext, MemoryContext p static Tuplestorestate * get_bbf_pivot_tuplestore(RawStmt *sql, const char *sourcetext, + const char *view_uuid, const char *funcName, HTAB *bbf_pivot_hash, TupleDesc tupdesc, @@ -4623,8 +4704,30 @@ get_bbf_pivot_tuplestore(RawStmt *sql, /* internal error */ elog(ERROR, "get_bbf_pivot_tuplestore: SPI_connect returned %d", ret); - /* Now retrieve the bbf_pivot source rows */ - ret = SPI_execute_raw_parsetree(sql, sourcetext, true, 0); + /* + * Now retrieve the bbf_pivot source rows + * + * If view_uuid is defined, then we will use the provided uuid to form the source_sql and + * get result. source_sql name format is pvt_sv_[uuid] + * + * if view_uuid is not defined, then we will use the rawparsetree of source_sql to get the + * result + */ + if (!view_uuid || strlen(view_uuid) != 0) + { + StringInfoData buf; + + initStringInfo(&buf); + appendStringInfoString(&buf, "select * from "); + appendStringInfoString(&buf, BBF_PIVOT_VIEW_SRC_PREFIX); + appendStringInfoString(&buf, view_uuid); + + ret = SPI_execute(buf.data, true, 0); + } + else + { + ret = SPI_execute_raw_parsetree(sql, sourcetext, true, 0); + } tuple_processed = SPI_processed; /* Check for qualifying tuples */ diff --git a/contrib/babelfishpg_tsql/sql/object_definition_tsql.sql b/contrib/babelfishpg_tsql/sql/object_definition_tsql.sql index da5b0f152b8..41548f82c1c 100644 --- a/contrib/babelfishpg_tsql/sql/object_definition_tsql.sql +++ b/contrib/babelfishpg_tsql/sql/object_definition_tsql.sql @@ -11,4 +11,15 @@ CREATE TABLE sys.babelfish_view_def ( ); GRANT SELECT ON sys.babelfish_view_def TO PUBLIC; -SELECT pg_catalog.pg_extension_config_dump('sys.babelfish_view_def', ''); \ No newline at end of file +CREATE TABLE sys.babelfish_pivot_view +( + dbid SMALLINT NOT NULL, + pivot_view_uuid sys.NVARCHAR(128) NOT NULL, + schema_name sys.NVARCHAR(128) NOT NULL COLLATE sys.database_default, + pivot_view_name sys.NVARCHAR(128) NOT NULL COLLATE sys.database_default, + agg_func_name sys.NVARCHAR(128) NOT NULL, + PRIMARY KEY(pivot_view_uuid) +); + +SELECT pg_catalog.pg_extension_config_dump('sys.babelfish_view_def', ''); +SELECT pg_catalog.pg_extension_config_dump('sys.babelfish_pivot_view', ''); \ No newline at end of file diff --git a/contrib/babelfishpg_tsql/sql/sys_functions.sql b/contrib/babelfishpg_tsql/sql/sys_functions.sql index acd3ea0ce36..e2bfa21980f 100644 --- a/contrib/babelfishpg_tsql/sql/sys_functions.sql +++ b/contrib/babelfishpg_tsql/sql/sys_functions.sql @@ -5091,7 +5091,7 @@ END; $body$ LANGUAGE plpgsql STABLE; -CREATE OR REPLACE FUNCTION sys.bbf_pivot() +CREATE OR REPLACE FUNCTION sys.bbf_pivot(IN arg TEXT) RETURNS setof record AS 'babelfishpg_tsql', 'bbf_pivot' LANGUAGE C STABLE; diff --git a/contrib/babelfishpg_tsql/sql/upgrades/babelfishpg_tsql--3.6.0--3.7.0.sql b/contrib/babelfishpg_tsql/sql/upgrades/babelfishpg_tsql--3.6.0--3.7.0.sql index d4c32b24fe7..dd095a920ea 100644 --- a/contrib/babelfishpg_tsql/sql/upgrades/babelfishpg_tsql--3.6.0--3.7.0.sql +++ b/contrib/babelfishpg_tsql/sql/upgrades/babelfishpg_tsql--3.6.0--3.7.0.sql @@ -135,6 +135,23 @@ $$ LANGUAGE 'pltsql'; GRANT EXECUTE ON PROCEDURE sys.sp_tables TO PUBLIC; +CREATE TABLE sys.babelfish_pivot_view +( + dbid SMALLINT NOT NULL, + pivot_view_uuid sys.NVARCHAR(128) NOT NULL, + schema_name sys.NVARCHAR(128) NOT NULL COLLATE sys.database_default, + pivot_view_name sys.NVARCHAR(128) NOT NULL COLLATE sys.database_default, + agg_func_name sys.NVARCHAR(128) NOT NULL, + PRIMARY KEY(pivot_view_uuid) +); + +CREATE OR REPLACE FUNCTION sys.bbf_pivot(IN arg TEXT) +RETURNS setof record +AS 'babelfishpg_tsql', 'bbf_pivot' +LANGUAGE C STABLE; + +SELECT pg_catalog.pg_extension_config_dump('sys.babelfish_pivot_view', ''); + -- Update deprecated object_id function(s) since left function now restricts TEXT datatype DO $$ BEGIN diff --git a/contrib/babelfishpg_tsql/src/catalog.c b/contrib/babelfishpg_tsql/src/catalog.c index a5bfe7922ef..7d817017ed2 100644 --- a/contrib/babelfishpg_tsql/src/catalog.c +++ b/contrib/babelfishpg_tsql/src/catalog.c @@ -15,14 +15,18 @@ #include "catalog/pg_foreign_server.h" #include "catalog/namespace.h" #include "commands/extension.h" +#include "commands/view.h" +#include "commands/tablecmds.h" #include "parser/parse_relation.h" #include "parser/scansup.h" +#include "nodes/makefuncs.h" #include "tcop/utility.h" #include "utils/builtins.h" #include "utils/fmgroids.h" #include "utils/formatting.h" #include "utils/lsyscache.h" #include "utils/syscache.h" +#include "utils/uuid.h" #include "utils/tuplestore.h" #include "utils/rel.h" #include "utils/timestamp.h" @@ -35,6 +39,8 @@ #include "rolecmds.h" #include "session.h" #include "pltsql.h" +#include "port.h" +#include "fmgr.h" /***************************************** * SYS schema @@ -104,6 +110,11 @@ Oid bbf_domain_mapping_idx_oid = InvalidOid; *****************************************/ Oid bbf_extended_properties_oid = InvalidOid; Oid bbf_extended_properties_idx_oid = InvalidOid; +/***************************************** + * PIVOT_VIEW + *****************************************/ +Oid bbf_pivot_view_oid = InvalidOid; +Oid bbf_pivot_view_idx_oid = InvalidOid; /***************************************** * Catalog General @@ -228,6 +239,9 @@ init_catalog(PG_FUNCTION_ARGS) spt_datatype_info_table_oid = get_relname_relid(SPT_DATATYPE_INFO_TABLE_NAME, sys_schema_oid); bbf_versions_oid = get_relname_relid(BBF_VERSIONS_TABLE_NAME, sys_schema_oid); + bbf_pivot_view_oid = get_bbf_pivot_view_oid(); + bbf_pivot_view_idx_oid = get_bbf_pivot_view_idx_oid(); + if (sysdatabases_oid != InvalidOid) initTsqlSyscache(); @@ -265,7 +279,8 @@ IsPLtsqlExtendedCatalog(Oid relationId) relationId == bbf_extended_properties_oid || relationId == bbf_assemblies_oid || relationId == bbf_configurations_oid || relationId == bbf_helpcollation_oid || relationId == bbf_syslanguages_oid || relationId == bbf_service_settings_oid || - relationId == spt_datatype_info_table_oid || relationId == bbf_versions_oid)) + relationId == spt_datatype_info_table_oid || relationId == bbf_versions_oid || + relationId == bbf_pivot_view_oid)) return true; if (PrevIsExtendedCatalogHook) return (*PrevIsExtendedCatalogHook) (relationId); @@ -1550,6 +1565,29 @@ get_bbf_extended_properties_idx_oid() return bbf_extended_properties_idx_oid; } +/***************************************** + * PIVOT_VIEW + *****************************************/ +Oid +get_bbf_pivot_view_oid() +{ + if (!OidIsValid(bbf_pivot_view_oid)) + bbf_pivot_view_oid = get_relname_relid(BBF_PIVOT_VIEW_TABLE_NAME, + get_namespace_oid("sys", false)); + + return bbf_pivot_view_oid; +} + +Oid +get_bbf_pivot_view_idx_oid() +{ + if (!OidIsValid(bbf_pivot_view_idx_oid)) + bbf_pivot_view_idx_oid = get_relname_relid(BBF_PIVOT_VIEW_IDX_NAME, + get_namespace_oid("sys", false)); + + return bbf_pivot_view_idx_oid; +} + /***************************************** * Metadata Check * --------------------------------------- @@ -1600,6 +1638,8 @@ static Datum get_function_name(HeapTuple tuple, TupleDesc dsc); static Datum get_perms_schema_name(HeapTuple tuple, TupleDesc dsc); static Datum get_perms_grantee_name(HeapTuple tuple, TupleDesc dsc); static Datum get_server_name(HeapTuple tuple, TupleDesc dsc); +static Datum get_pivot_view_dbname(HeapTuple tuple, TupleDesc dsc); +static Datum get_pivot_view_schema_name(HeapTuple tuple, TupleDesc dsc); /* Condition function declaration */ static bool is_multidb(void); @@ -1760,6 +1800,15 @@ Rule must_match_rules_srv_options[] = "pg_foreign_server", "srvname", NULL, get_server_name, NULL, check_exist, NULL} }; +/* babelfish_pivot_view */ +Rule must_match_rules_pivot_view[] = +{ + {" in babelfish_pivot_view must also exist in babelfish_sysdatabases", + "babelfish_sysdatabases", "name", NULL, get_pivot_view_dbname, NULL, check_exist, NULL}, + {" in babelfish_pivot_view must also exist in babelfish_namespace_ext", + "babelfish_namespace_ext", "nspname", NULL, get_pivot_view_schema_name, NULL, check_exist, NULL} +}; + /***************************************** * Core function *****************************************/ @@ -1851,6 +1900,7 @@ metadata_inconsistency_check(Tuplestorestate *res_tupstore, TupleDesc res_tupdes size_t num_must_match_rules_function = sizeof(must_match_rules_function) / sizeof(must_match_rules_function[0]); size_t num_must_match_rules_schema_permission = sizeof(must_match_rules_schema_permission) / sizeof(must_match_rules_schema_permission[0]); size_t num_must_match_rules_srv_options = sizeof(must_match_rules_srv_options) / sizeof(must_match_rules_srv_options[0]); + size_t num_must_match_rules_pivot_view = sizeof(must_match_rules_pivot_view) / sizeof(must_match_rules_pivot_view[0]); /* Initialize the catalog_data array to fetch catalog info */ init_catalog_data(); @@ -1885,6 +1935,9 @@ metadata_inconsistency_check(Tuplestorestate *res_tupstore, TupleDesc res_tupdes || !(check_must_match_rules(must_match_rules_srv_options, num_must_match_rules_srv_options, bbf_servers_def_oid, res_tupstore, res_tupdesc)) + || + !(check_must_match_rules(must_match_rules_pivot_view, num_must_match_rules_pivot_view, + bbf_pivot_view_oid, res_tupstore, res_tupdesc)) ) return; } @@ -2214,6 +2267,76 @@ get_server_name(HeapTuple tuple, TupleDesc dsc) return CStringGetDatum(servername); } +static Datum +get_pivot_view_dbname(HeapTuple tuple, TupleDesc dsc) +{ + bool is_null; + char *dbname; + Datum dbid; + + is_null = NULL; + dbname = NULL; + + dbid = heap_getattr(tuple, Anum_bbf_pivot_view_dbid, dsc, &is_null); + + if (is_null) /* Sanity check. */ + ereport(ERROR, + (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED), + errmsg("dbid should not be null in babelfish_pivot_view catalog"))); + + /* Another way to check for existence of dbid in babelfish_sysdatabases catalog. */ + dbname = get_db_name(DatumGetInt16(dbid)); + + if (!dbname) /* Sanity check. */ + ereport(ERROR, + (errcode(ERRCODE_INTERNAL_ERROR), + errmsg("dbid in babelfish_pivot_view catalog doesn't exists in babelfish_sysdatabases catalog"))); + + return CStringGetTextDatum(dbname); +} + +static Datum +get_pivot_view_schema_name(HeapTuple tuple, TupleDesc dsc) +{ + bool schema_is_null, dbid_is_null; + char *physical_schema_name, + *schema_name, + *org_schema_name; + Datum dbid; + Datum schema_name_datum; + + schema_is_null = NULL; + dbid_is_null = NULL; + physical_schema_name = NULL; + schema_name = NULL; + org_schema_name = NULL; + + dbid = heap_getattr(tuple, Anum_bbf_pivot_view_dbid, dsc, &dbid_is_null); + schema_name_datum = heap_getattr(tuple, Anum_bbf_pivot_view_schema_name, dsc, &schema_is_null); + + if (dbid_is_null) /* Sanity check. */ + ereport(ERROR, + (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED), + errmsg("dbid should not be null in babelfish_pivot_view catalog"))); + + if (schema_is_null) /* Sanity check. */ + ereport(ERROR, + (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED), + errmsg("schema_name should not be null in babelfish_pivot_view catalog"))); + + org_schema_name = TextDatumGetCString(schema_name_datum); + /* + * Downcase the orginal schema name and don't truncate it since + * truncation will be handled inside get_physical_schema_name(). + */ + schema_name = downcase_identifier(org_schema_name, strlen(org_schema_name), false, false); + physical_schema_name = get_physical_schema_name(get_db_name(DatumGetInt16(dbid)), schema_name); + + pfree(schema_name); + pfree(org_schema_name); + return CStringGetDatum(physical_schema_name); +} + /***************************************** * Condition check funcs *****************************************/ @@ -3957,3 +4080,235 @@ update_db_owner(const char *new_owner_name, const char *db_name) table_endscan(tblscan); table_close(sysdatabases_rel, RowExclusiveLock); } + + + +static ViewStmt * +make_pivot_view_helper_view(Node* node, const char *viewName, const char *schemaname) +{ + ViewStmt *n; + + n = makeNode(ViewStmt); + n->view = makeRangeVar(pstrdup(schemaname), pstrdup(viewName), -1); + n->view->relpersistence = RELPERSISTENCE_PERMANENT; + n->aliases = NIL; + n->query = node; + n->replace = false; + n->options = NIL; + + return n; +} + +/* + * get_uuid_no_dash + * generate a uuid string with no dash(hyphen) + */ +static char * +get_uuid_str_no_dash(pg_uuid_t *uuid) +{ + static const char hex_chars[] = "0123456789abcdef"; + StringInfoData buf; + int i; + + initStringInfo(&buf); + for (i = 0; i < UUID_LEN; i++) + { + int hi; + int lo; + + hi = uuid->data[i] >> 4; + lo = uuid->data[i] & 0x0F; + + appendStringInfoChar(&buf, hex_chars[hi]); + appendStringInfoChar(&buf, hex_chars[lo]); + } + return buf.data; +} + +/* + * add_entry_to_bbf_pivot_view + * Add a new entry to the sys.bbf_pivot_view catalog table. + */ +char * +add_entry_to_bbf_pivot_view(const char *pivot_view_name, Node *src_parsetree, Node *cat_parsetree, const char * source_text, const char *agg_func_name) +{ + Relation rel; + TupleDesc dsc; + HeapTuple tuple; + ViewStmt *src_view; + ViewStmt *cat_view; + pg_uuid_t *uuid; + char *uuid_str; + char *logical_schema_name; + char *physical_schema_name; + char *src_view_name; + char *cat_view_name; + Datum new_record[BBF_PIVOT_VIEW_NUM_COLS]; + bool new_record_nulls[BBF_PIVOT_VIEW_NUM_COLS]; + StringInfoData buf; + + logical_schema_name = get_authid_user_ext_schema_name(get_cur_db_name(), get_user_for_database(get_cur_db_name())); + physical_schema_name = get_physical_schema_name(get_cur_db_name(), logical_schema_name); + + rel = table_open(get_bbf_pivot_view_oid(), RowExclusiveLock); + dsc = RelationGetDescr(rel); + + uuid = DatumGetUUIDP(DirectFunctionCall1(gen_random_uuid, PointerGetDatum(NULL))); + uuid_str = get_uuid_str_no_dash(uuid); + + /* Build the view names */ + initStringInfo(&buf); + appendStringInfoString(&buf, BBF_PIVOT_VIEW_SRC_PREFIX); + appendStringInfoString(&buf, uuid_str); + src_view_name = pstrdup(buf.data); + + resetStringInfo(&buf); + appendStringInfoString(&buf, BBF_PIVOT_VIEW_CAT_PREFIX); + appendStringInfoString(&buf, uuid_str); + cat_view_name = pstrdup(buf.data); + + /* create view for src_view and cat_view */ + src_view = (ViewStmt *) make_pivot_view_helper_view(src_parsetree, src_view_name, physical_schema_name); + cat_view = (ViewStmt *) make_pivot_view_helper_view(cat_parsetree, cat_view_name, physical_schema_name); + DefineView(src_view, pstrdup(source_text), -1, strlen(source_text)); + DefineView(cat_view, pstrdup(source_text), -1, strlen(source_text)); + + /* Build a tuple to insert */ + MemSet(new_record, 0, sizeof(new_record)); + MemSet(new_record_nulls, false, sizeof(new_record_nulls)); + + new_record[Anum_bbf_pivot_view_pivot_view_uuid - 1] = CStringGetTextDatum(uuid_str); + new_record[Anum_bbf_pivot_view_dbid - 1] = Int16GetDatum(get_cur_db_id()); + new_record[Anum_bbf_pivot_view_schema_name - 1] = CStringGetTextDatum(logical_schema_name); + new_record[Anum_bbf_pivot_view_pivot_view_name - 1] = CStringGetTextDatum(pivot_view_name); + new_record[Anum_bbf_pivot_view_agg_func_name - 1] = CStringGetTextDatum(agg_func_name); + + tuple = heap_form_tuple(dsc, new_record, new_record_nulls); + + CatalogTupleInsert(rel, tuple); + + heap_freetuple(tuple); + table_close(rel, RowExclusiveLock); + + return uuid_str; +} + +static void drop_pivot_view_helper_view(char *view_name) +{ + DropStmt *n; + + n = makeNode(DropStmt); + n->objects = list_make1(list_make1(makeString(view_name))); + n->removeType = OBJECT_VIEW; + n->behavior = DROP_RESTRICT; + n->missing_ok = false; + n->concurrent = false; + + RemoveRelations(n); +} + +/* + * remove_entrys_from_bbf_pivot_view + * Remove all entries from the sys.bbf_pivot_view catalog table. + */ +void +remove_entrys_from_bbf_pivot_view(int16 dbid, const char *logical_schema_name, const char *pivot_view_name) +{ + Relation rel; + HeapTuple tuple; + SysScanDesc scan; + ScanKeyData scanKey[3]; + + rel = table_open(get_bbf_pivot_view_oid(), RowExclusiveLock); + + ScanKeyInit(&scanKey[0], + Anum_bbf_pivot_view_dbid, + BTEqualStrategyNumber, F_INT2EQ, + Int16GetDatum(dbid)); + + ScanKeyEntryInitialize(&scanKey[1], 0, Anum_bbf_pivot_view_schema_name, + BTEqualStrategyNumber, InvalidOid, + tsql_get_server_collation_oid_internal(false), F_TEXTEQ, + CStringGetTextDatum(logical_schema_name)); + + ScanKeyEntryInitialize(&scanKey[2], 0, Anum_bbf_pivot_view_pivot_view_name, + BTEqualStrategyNumber, InvalidOid, + tsql_get_server_collation_oid_internal(false), F_TEXTEQ, + CStringGetTextDatum(pivot_view_name)); + + scan = systable_beginscan(rel, get_bbf_pivot_view_idx_oid(), + false, NULL, 3, scanKey); + + /* Remove all pivot view related entries from bbf_pivot_view table */ + tuple = systable_getnext(scan); + while (HeapTupleIsValid(tuple)) + { + char *uuid; + char *src_view_name; + char *cat_view_name; + bool uuid_is_null; + StringInfoData buf; + + uuid = TextDatumGetCString(heap_getattr(tuple, Anum_bbf_pivot_view_pivot_view_uuid, RelationGetDescr(rel), &uuid_is_null)); + + initStringInfo(&buf); + appendStringInfoString(&buf, BBF_PIVOT_VIEW_SRC_PREFIX); + appendStringInfoString(&buf, uuid); + src_view_name = pstrdup(buf.data); + + resetStringInfo(&buf); + appendStringInfoString(&buf, BBF_PIVOT_VIEW_CAT_PREFIX); + appendStringInfoString(&buf, uuid); + cat_view_name = pstrdup(buf.data); + + drop_pivot_view_helper_view(src_view_name); + drop_pivot_view_helper_view(cat_view_name); + + if (HeapTupleIsValid(tuple)) + CatalogTupleDelete(rel, &tuple->t_self); + + tuple = systable_getnext(scan); + + pfree(src_view_name); + pfree(cat_view_name); + } + + systable_endscan(scan); + table_close(rel, RowExclusiveLock); +} + +/* + * clean_up_bbf_pivot_view + * clean up all the maintained metadata related to pivot view for + * provided database + */ +void +clean_up_bbf_pivot_view(int16 dbid) +{ + Relation rel; + HeapTuple tuple; + SysScanDesc scan; + ScanKeyData scanKey[1]; + + rel = table_open(get_bbf_pivot_view_oid(), RowExclusiveLock); + + ScanKeyInit(&scanKey[0], + Anum_bbf_pivot_view_dbid, + BTEqualStrategyNumber, F_INT2EQ, + Int16GetDatum(dbid)); + + scan = systable_beginscan(rel, get_bbf_pivot_view_idx_oid(), + false, NULL, 1, scanKey); + + tuple = systable_getnext(scan); + while (HeapTupleIsValid(tuple)) + { + if (HeapTupleIsValid(tuple)) + CatalogTupleDelete(rel, &tuple->t_self); + + tuple = systable_getnext(scan); + } + + systable_endscan(scan); + table_close(rel, RowExclusiveLock); +} \ No newline at end of file diff --git a/contrib/babelfishpg_tsql/src/catalog.h b/contrib/babelfishpg_tsql/src/catalog.h index 4b7bbf4edc7..0a5376046d5 100644 --- a/contrib/babelfishpg_tsql/src/catalog.h +++ b/contrib/babelfishpg_tsql/src/catalog.h @@ -394,6 +394,39 @@ typedef struct FormData_bbf_extended_properties typedef FormData_bbf_extended_properties *Form_bbf_extended_properties; +/***************************************** + * PIVOT_VIEW + *****************************************/ +#define BBF_PIVOT_VIEW_TABLE_NAME "babelfish_pivot_view" +#define BBF_PIVOT_VIEW_IDX_NAME "babelfish_pivot_view_pkey" +#define Anum_bbf_pivot_view_dbid 1 +#define Anum_bbf_pivot_view_pivot_view_uuid 2 +#define Anum_bbf_pivot_view_schema_name 3 +#define Anum_bbf_pivot_view_pivot_view_name 4 +#define Anum_bbf_pivot_view_agg_func_name 5 +#define BBF_PIVOT_VIEW_NUM_COLS 5 +#define BBF_PIVOT_VIEW_SRC_PREFIX "pvt_sv_" +#define BBF_PIVOT_VIEW_CAT_PREFIX "pvt_cv_" +extern Oid bbf_pivot_view_oid; +extern Oid bbf_pivot_view_idx_oid; + +extern Oid get_bbf_pivot_view_oid(void); +extern Oid get_bbf_pivot_view_idx_oid(void); +extern char *add_entry_to_bbf_pivot_view(const char *pivot_view_name, Node *src_parsetree, Node *cat_parsetree, const char *source_text, const char *agg_func_name); +extern void remove_entrys_from_bbf_pivot_view(int16 dbid, const char *logical_schema_name, const char *pivot_view_name); +extern void clean_up_bbf_pivot_view(int16 dbid); + +typedef struct FormData_bbf_pivot_view +{ + int16 dbid; + VarChar pivot_view_uuid; + VarChar schema_name; + VarChar pivot_view_name; + VarChar agg_func_name; +} FormData_bbf_pivot_view; + +typedef FormData_bbf_pivot_view *Form_bbf_pivot_view; + /***************************************** * Metadata Check Rule *****************************************/ diff --git a/contrib/babelfishpg_tsql/src/dbcmds.c b/contrib/babelfishpg_tsql/src/dbcmds.c index 4cadd91adc3..48a158386dc 100644 --- a/contrib/babelfishpg_tsql/src/dbcmds.c +++ b/contrib/babelfishpg_tsql/src/dbcmds.c @@ -758,6 +758,8 @@ drop_bbf_db(const char *dbname, bool missing_ok, bool force_drop) } /* clean up bbf view def catalog */ clean_up_bbf_view_def(dbid); + /* clean up bbf_pivot_view catalog */ + clean_up_bbf_pivot_view(dbid); /* clean up bbf function catalog */ clean_up_bbf_function_ext(dbid); /* clean up bbf namespace catalog accordingly */ diff --git a/contrib/babelfishpg_tsql/src/hooks.c b/contrib/babelfishpg_tsql/src/hooks.c index 3d441a19d54..be2fbad608c 100644 --- a/contrib/babelfishpg_tsql/src/hooks.c +++ b/contrib/babelfishpg_tsql/src/hooks.c @@ -139,6 +139,7 @@ static void fill_missing_values_in_copyfrom(Relation rel, Datum *values, bool *n static void pltsql_report_proc_not_found_error(List *names, List *argnames, Oid *input_typeids, int nargs, ParseState *pstate, int location, bool proc_call); extern PLtsql_execstate *get_outermost_tsql_estate(int *nestlevel); extern PLtsql_execstate *get_current_tsql_estate(); +static Query *parse_analyze_babelfish_view(ViewStmt *stmt, RawStmt *rawstmt, const char *queryString); static void pltsql_store_view_definition(const char *queryString, ObjectAddress address); static void pltsql_drop_view_definition(Oid objectId); static void preserve_view_constraints_from_base_table(ColumnDef *col, Oid tableOid, AttrNumber colId); @@ -242,6 +243,7 @@ static drop_relation_refcnt_hook_type prev_drop_relation_refcnt_hook = NULL; static set_local_schema_for_func_hook_type prev_set_local_schema_for_func_hook = NULL; static bbf_get_sysadmin_oid_hook_type prev_bbf_get_sysadmin_oid_hook = NULL; static transform_pivot_clause_hook_type pre_transform_pivot_clause_hook = NULL; +static parse_analyze_babelfish_view_hook_type pre_parse_analyze_babelfish_view_hook = NULL; static pass_pivot_data_to_fcinfo_hook_type pre_pass_pivot_data_to_fcinfo_hook = NULL; static called_from_tsql_insert_exec_hook_type pre_called_from_tsql_insert_exec_hook = NULL; static called_for_tsql_itvf_func_hook_type prev_called_for_tsql_itvf_func_hook = NULL; @@ -434,6 +436,8 @@ InstallExtendedHooks(void) prev_pltsql_unique_constraint_nulls_ordering_hook = pltsql_unique_constraint_nulls_ordering_hook; pltsql_unique_constraint_nulls_ordering_hook = unique_constraint_nulls_ordering; + pre_parse_analyze_babelfish_view_hook = parse_analyze_babelfish_view_hook; + parse_analyze_babelfish_view_hook = parse_analyze_babelfish_view; } void @@ -501,6 +505,7 @@ UninstallExtendedHooks(void) called_for_tsql_itvf_func_hook = prev_called_for_tsql_itvf_func_hook; pltsql_pgstat_end_function_usage_hook = prev_pltsql_pgstat_end_function_usage_hook; pltsql_unique_constraint_nulls_ordering_hook = prev_pltsql_unique_constraint_nulls_ordering_hook; + parse_analyze_babelfish_view_hook = pre_parse_analyze_babelfish_view_hook; bbf_InitializeParallelDSM_hook = NULL; bbf_ParallelWorkerMain_hook = NULL; @@ -2993,6 +2998,9 @@ pltsql_drop_view_definition(Oid objectId) return; } + /* check if the view is a pivot view, if so, we drop all its related views */ + remove_entrys_from_bbf_pivot_view(dbid, logical_schemaname, objectname); + /* Fetch the relation */ bbf_view_def_rel = table_open(get_bbf_view_def_oid(), RowExclusiveLock); @@ -4512,6 +4520,7 @@ fill_missing_values_in_copyfrom(Relation rel, Datum *values, bool *nulls) if (relid == sysdatabases_oid || relid == namespace_ext_oid || relid == bbf_view_def_oid || + relid == bbf_pivot_view_oid || relid == bbf_extended_properties_oid || relid == bbf_schema_perms_oid) { @@ -4625,6 +4634,7 @@ transform_pivot_clause(ParseState *pstate, SelectStmt *stmt) List *src_sql_sortbylist; List *src_sql_fromClause_copy; List *pivot_context_list; + A_Const *bbf_pivot_arg; char *pivot_colstr; char *value_colstr; String *funcName; @@ -4636,14 +4646,19 @@ transform_pivot_clause(ParseState *pstate, SelectStmt *stmt) RawStmt *c_sql; FuncCall *pivot_func; WithClause *with_clause; + bool is_view_transform; if (sql_dialect != SQL_DIALECT_TSQL) return; + /* initialize all lists */ + temp_src_targetlist = NIL; new_src_sql_targetist = NIL; new_pivot_aliaslist = NIL; src_sql_groupbylist = NIL; src_sql_sortbylist = NIL; + pivot_context_list = NIL; + is_view_transform = false; with_clause = copyObject(stmt->withClause); pivot_src_sql = makeNode(SelectStmt); @@ -4773,28 +4788,66 @@ transform_pivot_clause(ParseState *pstate, SelectStmt *stmt) wrapperSelect_RangeFunction->coldeflist = new_pivot_aliaslist; + /* make bbf_pivot function argument, default is NULL */ + bbf_pivot_arg = makeNode(A_Const); + bbf_pivot_arg->val.sval.type = T_String; + bbf_pivot_arg->val.sval.sval = NULL; + bbf_pivot_arg->location = -1; + + /* + * There are two ways to handle stmt with pivot operator: + * 1. If the query is wrapped in a view, then we will first genearte a uuid for this pivot call + * then two helper views with name like pvt_sv_[uuid], pvt_cv_[uuid]. The uuid will be saved as + * bbf_pivot function argument for later in function view name retrival. + * The uuid, dbid, logical name, view name, and aggreate function name will also be saved in + * babelfish_pivot_view catalog. + * 2. If the query is not wrapped in a view, then we will save the pivot operator metadatas + * (rawparsetrees, funcName, sql text)to bbf_pivot->context. The metadata will later passed + * to FunctionCallInfo->context and will be consumed within bbf_pivot function. + */ + is_view_transform = checkQueryWithinView(pstate->p_queryEnv); + if (is_view_transform) + { /* case 1: current query is wrapped in a view */ + char *view_name; + char *view_uuid; + view_name = getQueryEnvViewName(pstate->p_queryEnv); + view_uuid = add_entry_to_bbf_pivot_view(view_name, + (Node *) copyObject(pivot_src_sql), + (Node *) copyObject(stmt->catSql), + pstate->p_sourcetext, + funcName->sval); + bbf_pivot_arg->val.sval.sval = view_uuid; + } + else + { + /* case 2: current query is not wrapped in a view */ + s_sql = makeNode(RawStmt); + c_sql = makeNode(RawStmt); + s_sql->stmt = (Node *) pivot_src_sql; + s_sql->stmt_location = 0; + s_sql->stmt_len = 0; + + c_sql->stmt = (Node *) stmt->catSql; + c_sql->stmt_location = 0; + c_sql->stmt_len = 0; + + pivot_context_list = list_make3(list_make1(makeString("bbf_pivot_func")), + list_make2((Node *) copyObject(s_sql), + (Node *) copyObject(c_sql) + ), + list_make2(makeString(pstrdup(pstate->p_sourcetext)), + makeString(pstrdup(funcName->sval)) + ) + ); - s_sql = makeNode(RawStmt); - c_sql = makeNode(RawStmt); - s_sql->stmt = (Node *) pivot_src_sql; - s_sql->stmt_location = 0; - s_sql->stmt_len = 0; - - c_sql->stmt = (Node *) stmt->catSql; - c_sql->stmt_location = 0; - c_sql->stmt_len = 0; - - pivot_context_list = list_make3(list_make1(makeString("bbf_pivot_func")), - list_make2((Node *) copyObject(s_sql), - (Node *) copyObject(c_sql) - ), - list_make2(makeString(pstrdup(pstate->p_sourcetext)), - makeString(pstrdup(funcName->sval)) - ) - ); - + bbf_pivot_arg->val.sval.sval = ""; + } /* Store pivot information in FuncCall to live through parser analyzer */ - pivot_func = makeFuncCall(list_make2(makeString("sys"), makeString("bbf_pivot")), NIL, COERCE_EXPLICIT_CALL, -1); + pivot_func = makeFuncCall(list_make2(makeString("sys"), + makeString("bbf_pivot")), + list_make1(bbf_pivot_arg), + COERCE_EXPLICIT_CALL, + -1); pivot_func->context = (Node *) pivot_context_list; wrapperSelect_RangeFunction->functions = list_make1(list_make2((Node *) pivot_func, NIL)); } @@ -4929,3 +4982,41 @@ unique_constraint_nulls_ordering(ConstrType constraint_type, SortByDir ordering) return SORTBY_NULLS_DEFAULT; } + +/* + * parse_analyze_babelfish_view + * This function is used to parse the view query and set the QueryEnvironment + * to indicate we are currently analyze/transform a query that inside a view. + */ +static Query * +parse_analyze_babelfish_view(ViewStmt *stmt, RawStmt *rawstmt, const char *queryString) +{ + Query *viewParse; + char *viewName; + + if (sql_dialect != SQL_DIALECT_TSQL) + return parse_analyze_fixedparams(rawstmt, queryString, NULL, 0, NULL); + + viewParse = NULL; + viewName = ((RangeVar *)(stmt->view))->relname; + + create_queryEnv2(AllocSetContextCreate(CurrentMemoryContext, + "BabelViewMemoryContext", + ALLOCSET_DEFAULT_SIZES), + false); + setQueryEnvInView(currentQueryEnv, true); + setQueryEnvViewName(currentQueryEnv, viewName); + PG_TRY(); + { + viewParse = parse_analyze_fixedparams(rawstmt, queryString, NULL, 0, currentQueryEnv); + } + PG_FINALLY(); + { + setQueryEnvInView(currentQueryEnv, false); + setQueryEnvViewName(currentQueryEnv, ""); + remove_queryEnv(); + } + PG_END_TRY(); + + return viewParse; +} \ No newline at end of file diff --git a/contrib/babelfishpg_tsql/src/pl_handler.c b/contrib/babelfishpg_tsql/src/pl_handler.c index 1a1945f75f8..682fbe51dde 100644 --- a/contrib/babelfishpg_tsql/src/pl_handler.c +++ b/contrib/babelfishpg_tsql/src/pl_handler.c @@ -527,6 +527,7 @@ pltsql_pre_parse_analyze(ParseState *pstate, RawStmt *parseTree) if (relid == sysdatabases_oid || relid == namespace_ext_oid || relid == bbf_view_def_oid || + relid == bbf_pivot_view_oid || relid == bbf_extended_properties_oid || relid == bbf_schema_perms_oid) { diff --git a/contrib/babelfishpg_tsql/src/tsqlUnsupportedFeatureHandler.cpp b/contrib/babelfishpg_tsql/src/tsqlUnsupportedFeatureHandler.cpp index 7359194e177..511f74c83f6 100644 --- a/contrib/babelfishpg_tsql/src/tsqlUnsupportedFeatureHandler.cpp +++ b/contrib/babelfishpg_tsql/src/tsqlUnsupportedFeatureHandler.cpp @@ -89,7 +89,6 @@ class TsqlUnsupportedFeatureHandlerImpl : public TsqlUnsupportedFeatureHandler bool throw_error = false; int count = 0; /* record count to skip unnecessary visiting */ bool is_inside_trigger = false; - bool is_inside_view = false; /* handler */ void handle(PgTsqlInstrMetricType tm_type, antlr4::tree::TerminalNode *node, escape_hatch_t* eh); @@ -525,11 +524,7 @@ antlrcpp::Any TsqlUnsupportedFeatureHandlerImpl::visitCreate_or_alter_view(TSqlP handle(INSTR_UNSUPPORTED_TSQL_ALTER_VIEW_VIEW_METADATA_OPTION, option->VIEW_METADATA()); } - is_inside_view = true; - auto ret = visitChildren(ctx); - is_inside_view = false; - - return ret; + return visitChildren(ctx);; } antlrcpp::Any TsqlUnsupportedFeatureHandlerImpl::visitProcedure_param(TSqlParser::Procedure_paramContext *ctx) @@ -1368,14 +1363,6 @@ antlrcpp::Any TsqlUnsupportedFeatureHandlerImpl::visitCheckpoint_statement(TSqlP antlrcpp::Any TsqlUnsupportedFeatureHandlerImpl::visitTable_source_item(TSqlParser::Table_source_itemContext *ctx) { - if (ctx->PIVOT()) - { - if (is_inside_view) - { - is_inside_view = false; - throw PGErrorWrapperException(ERROR, ERRCODE_FEATURE_NOT_SUPPORTED, "Create view on stmt with PIVOT operator is not currently supported.", 0, 0); - } - } if (ctx->UNPIVOT()) handle(INSTR_UNSUPPORTED_TSQL_UNPIVOT, ctx->UNPIVOT()); diff --git a/test/JDBC/expected/AUTO_ANALYZE-vu-prepare.out b/test/JDBC/expected/AUTO_ANALYZE-vu-prepare.out index d569a9922d3..98297c8b92e 100644 --- a/test/JDBC/expected/AUTO_ANALYZE-vu-prepare.out +++ b/test/JDBC/expected/AUTO_ANALYZE-vu-prepare.out @@ -22,6 +22,7 @@ babelfish_extended_properties babelfish_function_ext babelfish_helpcollation babelfish_namespace_ext +babelfish_pivot_view babelfish_schema_permissions babelfish_server_options babelfish_sysdatabases diff --git a/test/JDBC/expected/TEST_PUBLICATION.out b/test/JDBC/expected/TEST_PUBLICATION.out index f222750eff7..2e6180c3753 100644 --- a/test/JDBC/expected/TEST_PUBLICATION.out +++ b/test/JDBC/expected/TEST_PUBLICATION.out @@ -35,6 +35,7 @@ sys.babelfish_extended_properties sys.babelfish_function_ext sys.babelfish_helpcollation sys.babelfish_namespace_ext +sys.babelfish_pivot_view sys.babelfish_schema_permissions sys.babelfish_server_options sys.babelfish_sysdatabases diff --git a/test/JDBC/expected/babelfish_integrity_checker-vu-verify.out b/test/JDBC/expected/babelfish_integrity_checker-vu-verify.out index 8f1881c2d91..24ea0f63848 100644 --- a/test/JDBC/expected/babelfish_integrity_checker-vu-verify.out +++ b/test/JDBC/expected/babelfish_integrity_checker-vu-verify.out @@ -36,6 +36,7 @@ babelfish_domain_mapping babelfish_extended_properties babelfish_function_ext babelfish_namespace_ext +babelfish_pivot_view babelfish_schema_permissions babelfish_server_options babelfish_sysdatabases diff --git a/test/JDBC/expected/pivot-before-15_8-or-16_4-vu-cleanup.out b/test/JDBC/expected/pivot-before-15_8-or-16_4-vu-cleanup.out new file mode 100644 index 00000000000..6a397073ffd --- /dev/null +++ b/test/JDBC/expected/pivot-before-15_8-or-16_4-vu-cleanup.out @@ -0,0 +1,54 @@ +use pivot_test; +GO + +drop trigger pivot_trigger +GO + +drop table trigger_testing +GO + +drop table OSTable; +GO + +drop table STable; +GO + +drop table seating_tbl; +GO + +drop view StoreReceipt_view; +GO + +drop table pivot_insert_into; +GO + +drop table pivot_select_into; +GO + +drop procedure top_n_pivot; +GO + +drop function test_table_valued_function; +GO + +drop table StoreReceipt; +GO + +drop table orders; +GO + +drop table products; +GO + +drop table pivot_schema.products_sch; +GO + +drop schema pivot_schema; +GO + +use master; +GO + +drop database pivot_test; +GO + diff --git a/test/JDBC/expected/pivot-before-15_8-or-16_4-vu-prepare.out b/test/JDBC/expected/pivot-before-15_8-or-16_4-vu-prepare.out new file mode 100644 index 00000000000..48a8f4f60b0 --- /dev/null +++ b/test/JDBC/expected/pivot-before-15_8-or-16_4-vu-prepare.out @@ -0,0 +1,862 @@ +create database pivot_test +GO + +use pivot_test +GO + +create table StoreReceipt ( + OrderID INT, + ItemID INT, + Price DECIMAL(6,2), + EmployeeID INT, + StoreID INT, + ManufactureID INT, + PurchaseDate DATE +); +GO + + +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (1, 2006, 485.14, 252, 7, 1209, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (2, 2146, 681.23, 296, 9, 1234, '2023-10-27'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (3, 2074, 960.42, 251, 4, 1245, '2023-10-27'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (4, 2005, 830.57, 220, 9, 1203, '2023-10-26'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (5, 2050, 649.41, 203, 5, 1200, '2023-10-30'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (6, 2082, 695.76, 269, 2, 1200, '2023-10-25'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (7, 2145, 766.23, 256, 9, 1249, '2023-10-28'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (8, 2085, 146.58, 201, 8, 1240, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (9, 2127, 819.74, 288, 5, 1202, '2023-10-25'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (10, 2036, 803.59, 270, 9, 1208, '2023-10-28'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (11, 2138, 704.37, 223, 5, 1208, '2023-10-26'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (12, 2016, 949.56, 287, 5, 1250, '2023-10-25'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (13, 2114, 187.16, 222, 5, 1200, '2023-10-27'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (14, 2081, 545.96, 269, 3, 1217, '2023-10-25'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (15, 2084, 843.16, 247, 9, 1218, '2023-10-28'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (16, 2004, 152.79, 251, 1, 1240, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (17, 2100, 313.51, 232, 8, 1201, '2023-10-27'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (18, 2001, 34.63, 211, 10, 1232, '2023-10-27'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (19, 2072, 76.61, 247, 9, 1228, '2023-10-27'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (20, 2069, 878.9, 209, 7, 1227, '2023-10-27'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (21, 2074, 124.01, 200, 4, 1226, '2023-10-30'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (22, 2061, 429.58, 204, 3, 1212, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (23, 2027, 709.99, 300, 6, 1238, '2023-10-30'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (24, 2056, 267.88, 202, 2, 1226, '2023-10-25'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (25, 2031, 271.77, 248, 4, 1228, '2023-10-28'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (26, 2080, 397.51, 220, 10, 1200, '2023-10-27'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (27, 2006, 525.4, 207, 8, 1247, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (28, 2010, 343.29, 276, 7, 1229, '2023-10-25'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (29, 2044, 808.24, 227, 1, 1216, '2023-10-27'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (30, 2073, 451.15, 228, 3, 1231, '2023-10-26'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (31, 2074, 808.82, 296, 9, 1214, '2023-10-28'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (32, 2018, 985.56, 221, 9, 1219, '2023-10-26'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (33, 2120, 18.1, 227, 10, 1243, '2023-10-26'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (34, 2094, 532.7, 234, 1, 1238, '2023-10-30'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (35, 2018, 675.61, 212, 4, 1211, '2023-10-28'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (36, 2052, 286.88, 201, 1, 1205, '2023-10-25'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (37, 2079, 351.51, 264, 1, 1217, '2023-10-25'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (38, 2089, 834.46, 264, 3, 1200, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (39, 2111, 564.39, 288, 9, 1213, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (40, 2045, 332.85, 278, 8, 1214, '2023-10-25'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (41, 2139, 814.19, 288, 5, 1220, '2023-10-30'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (42, 2106, 645.39, 218, 4, 1207, '2023-10-25'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (43, 2082, 185.88, 230, 9, 1234, '2023-10-28'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (44, 2078, 235.07, 232, 6, 1250, '2023-10-28'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (45, 2077, 307.92, 297, 5, 1248, '2023-10-30'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (46, 2021, 606.12, 262, 1, 1203, '2023-10-26'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (47, 2028, 622.14, 296, 7, 1246, '2023-10-25'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (48, 2092, 2.41, 224, 10, 1225, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (49, 2142, 447.79, 260, 7, 1245, '2023-10-26'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (50, 2006, 970.28, 272, 8, 1202, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (51, 2078, 459.75, 274, 9, 1221, '2023-10-27'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (52, 2128, 376.82, 294, 8, 1215, '2023-10-30'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (53, 2059, 357.59, 219, 2, 1211, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (54, 2058, 535.53, 271, 8, 1246, '2023-10-25'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (55, 2127, 661.96, 227, 1, 1219, '2023-10-27'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (56, 2053, 885.07, 275, 7, 1233, '2023-10-25'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (57, 2094, 55.32, 238, 4, 1208, '2023-10-25'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (58, 2055, 420.27, 264, 2, 1238, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (59, 2117, 306.36, 222, 4, 1234, '2023-10-27'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (60, 2077, 504.6, 266, 4, 1200, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (61, 2120, 279.1, 292, 2, 1226, '2023-10-26'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (62, 2113, 904.88, 299, 1, 1241, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (63, 2051, 496.42, 249, 7, 1203, '2023-10-25'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (64, 2136, 508.71, 262, 3, 1236, '2023-10-26'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (65, 2144, 421.24, 286, 9, 1236, '2023-10-25'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (66, 2119, 236.49, 277, 5, 1241, '2023-10-27'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (67, 2030, 215.66, 216, 3, 1246, '2023-10-26'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (68, 2024, 243.15, 245, 9, 1243, '2023-10-27'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (69, 2073, 397.63, 255, 8, 1235, '2023-10-30'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (70, 2079, 163.06, 229, 4, 1201, '2023-10-25'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (71, 2070, 550.83, 289, 7, 1214, '2023-10-25'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (72, 2069, 676.38, 278, 7, 1225, '2023-10-27'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (73, 2135, 778.12, 211, 10, 1214, '2023-10-27'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (74, 2127, 563.12, 258, 9, 1223, '2023-10-25'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (75, 2010, 502.25, 214, 7, 1218, '2023-10-30'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (76, 2050, 171.66, 271, 3, 1239, '2023-10-27'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (77, 2112, 364.88, 249, 2, 1215, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (78, 2090, 821.38, 269, 1, 1239, '2023-10-30'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (79, 2079, 19.88, 228, 1, 1202, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (80, 2047, 730.79, 255, 8, 1239, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (81, 2080, 664.81, 283, 10, 1215, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (82, 2137, 340.03, 236, 4, 1214, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (83, 2092, 4.28, 203, 10, 1218, '2023-10-26'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (84, 2003, 100.14, 253, 7, 1224, '2023-10-28'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (85, 2001, 952.61, 247, 2, 1212, '2023-10-28'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (86, 2054, 773.2, 210, 8, 1224, '2023-10-25'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (87, 2037, 65.9, 291, 6, 1214, '2023-10-26'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (88, 2092, 904.74, 224, 6, 1204, '2023-10-28'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (89, 2036, 485.19, 214, 10, 1203, '2023-10-26'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (90, 2148, 946.4, 211, 2, 1236, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (91, 2045, 703.15, 232, 7, 1204, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (92, 2093, 711.61, 200, 4, 1229, '2023-10-27'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (93, 2084, 103.15, 267, 2, 1209, '2023-10-25'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (94, 2049, 202.91, 289, 1, 1245, '2023-10-26'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (95, 2038, 760.1, 243, 8, 1241, '2023-10-28'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (96, 2026, 759.33, 253, 2, 1212, '2023-10-26'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (97, 2105, 125.73, 226, 10, 1218, '2023-10-30'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (98, 2011, 176.87, 294, 10, 1213, '2023-10-25'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (99, 2120, 501.65, 204, 9, 1240, '2023-10-28'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (100, 2138, 490.44, 232, 7, 1243, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (101, 2014, 346.61, 265, 9, 1215, '2023-10-27'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (102, 2062, 176.8, 285, 5, 1235, '2023-10-26'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (103, 2112, 113.92, 224, 8, 1229, '2023-10-28'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (104, 2073, 160.8, 267, 2, 1210, '2023-10-26'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (105, 2082, 588.15, 225, 3, 1229, '2023-10-26'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (106, 2138, 571.21, 213, 1, 1242, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (107, 2092, 814.36, 213, 9, 1243, '2023-10-30'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (108, 2089, 221.8, 220, 5, 1203, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (109, 2040, 501.46, 248, 10, 1244, '2023-10-25'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (110, 2096, 974.47, 204, 6, 1221, '2023-10-25'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (111, 2078, 914.56, 208, 3, 1239, '2023-10-25'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (112, 2118, 287.53, 215, 10, 1221, '2023-10-26'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (113, 2106, 415.27, 249, 8, 1242, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (114, 2145, 283.31, 227, 6, 1231, '2023-10-26'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (115, 2148, 950.09, 243, 10, 1211, '2023-10-27'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (116, 2137, 132.57, 269, 3, 1227, '2023-10-25'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (117, 2082, 440.25, 267, 9, 1204, '2023-10-30'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (118, 2015, 749.85, 229, 8, 1232, '2023-10-28'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (119, 2021, 209.93, 229, 9, 1250, '2023-10-28'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (120, 2006, 540.63, 283, 8, 1242, '2023-10-27'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (121, 2030, 197.56, 278, 9, 1215, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (122, 2123, 153.87, 259, 5, 1239, '2023-10-30'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (123, 2079, 444.55, 259, 1, 1200, '2023-10-30'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (124, 2146, 437.87, 231, 10, 1247, '2023-10-30'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (125, 2094, 74.57, 241, 8, 1237, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (126, 2084, 660.65, 251, 3, 1237, '2023-10-27'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (127, 2085, 366.69, 209, 3, 1238, '2023-10-26'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (128, 2031, 560.65, 254, 1, 1233, '2023-10-30'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (129, 2064, 410.85, 217, 5, 1208, '2023-10-25'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (130, 2095, 241.41, 289, 10, 1243, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (131, 2106, 163.57, 235, 9, 1218, '2023-10-27'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (132, 2128, 764.88, 291, 3, 1237, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (133, 2014, 936.97, 201, 10, 1218, '2023-10-26'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (134, 2141, 351.46, 287, 1, 1202, '2023-10-30'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (135, 2094, 277.08, 218, 1, 1211, '2023-10-27'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (136, 2064, 489.19, 251, 2, 1226, '2023-10-28'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (137, 2001, 190.54, 231, 7, 1222, '2023-10-26'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (138, 2007, 252.7, 290, 8, 1242, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (139, 2058, 413.1, 214, 3, 1226, '2023-10-28'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (140, 2140, 230.58, 227, 8, 1206, '2023-10-25'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (141, 2074, 940.96, 200, 8, 1200, '2023-10-28'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (142, 2071, 618.94, 203, 9, 1250, '2023-10-30'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (143, 2002, 115.65, 213, 4, 1201, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (144, 2010, 22.85, 254, 3, 1218, '2023-10-25'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (145, 2023, 901.21, 230, 2, 1245, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (146, 2139, 173.7, 246, 8, 1202, '2023-10-27'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (147, 2047, 848.18, 225, 5, 1221, '2023-10-28'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (148, 2084, 254.96, 250, 10, 1244, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (149, 2004, 298.15, 296, 10, 1231, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (150, 2009, 413.91, 292, 9, 1245, '2023-10-27'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (151, 2009, 664.17, 277, 4, 1240, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (152, 2049, 748.86, 205, 6, 1250, '2023-10-26'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (153, 2064, 935.97, 253, 9, 1218, '2023-10-25'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (154, 2129, 577.5, 290, 9, 1237, '2023-10-25'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (155, 2052, 496.99, 211, 2, 1215, '2023-10-28'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (156, 2144, 753.54, 270, 6, 1229, '2023-10-30'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (157, 2143, 644.8, 267, 7, 1201, '2023-10-27'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (158, 2131, 710.66, 292, 8, 1217, '2023-10-26'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (159, 2051, 336.83, 229, 9, 1229, '2023-10-26'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (160, 2031, 592.09, 248, 4, 1206, '2023-10-30'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (161, 2046, 129.18, 279, 10, 1207, '2023-10-27'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (162, 2101, 536.8, 282, 7, 1204, '2023-10-27'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (163, 2112, 960.31, 296, 2, 1240, '2023-10-28'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (164, 2100, 127.35, 235, 8, 1236, '2023-10-25'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (165, 2031, 352.12, 203, 9, 1208, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (166, 2035, 110.15, 243, 10, 1229, '2023-10-30'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (167, 2105, 531.13, 234, 7, 1220, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (168, 2046, 483.93, 279, 8, 1238, '2023-10-28'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (169, 2083, 669.86, 226, 2, 1243, '2023-10-28'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (170, 2040, 373.61, 208, 10, 1223, '2023-10-28'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (171, 2060, 355.5, 220, 10, 1200, '2023-10-28'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (172, 2120, 28.3, 284, 9, 1247, '2023-10-28'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (173, 2040, 357.99, 250, 6, 1212, '2023-10-26'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (174, 2103, 980.82, 288, 2, 1202, '2023-10-28'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (175, 2035, 813.47, 217, 1, 1235, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (176, 2110, 399.64, 285, 9, 1220, '2023-10-25'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (177, 2016, 44.06, 250, 6, 1207, '2023-10-30'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (178, 2096, 66.57, 292, 4, 1214, '2023-10-28'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (179, 2030, 33.38, 239, 10, 1215, '2023-10-30'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (180, 2073, 459.77, 240, 8, 1218, '2023-10-30'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (181, 2071, 875.42, 230, 3, 1217, '2023-10-26'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (182, 2041, 380.94, 255, 3, 1247, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (183, 2097, 914.44, 298, 3, 1210, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (184, 2105, 329.25, 210, 1, 1242, '2023-10-30'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (185, 2000, 457.91, 256, 2, 1231, '2023-10-28'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (186, 2098, 901.2, 261, 10, 1249, '2023-10-30'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (187, 2146, 236.33, 293, 10, 1223, '2023-10-28'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (188, 2117, 405.01, 279, 8, 1246, '2023-10-30'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (189, 2099, 272.14, 234, 6, 1205, '2023-10-25'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (190, 2145, 42.04, 299, 8, 1204, '2023-10-26'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (191, 2017, 399.9, 280, 4, 1242, '2023-10-28'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (192, 2058, 733.45, 277, 9, 1239, '2023-10-28'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (193, 2124, 809.67, 259, 3, 1246, '2023-10-25'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (194, 2059, 167.54, 221, 10, 1233, '2023-10-30'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (195, 2032, 441.79, 219, 6, 1238, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (196, 2101, 720.37, 286, 1, 1246, '2023-10-27'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (197, 2103, 820.5, 289, 6, 1206, '2023-10-28'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (198, 2010, 433.08, 276, 9, 1213, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (199, 2147, 779.36, 237, 2, 1245, '2023-10-25'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (200, 2084, 735.91, 223, 5, 1221, '2023-10-30'); +GO +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + + +CREATE TABLE orders ( + orderId INT PRIMARY KEY, + productId INT, + employeeName VARCHAR(4), + employeeCode VARBINARY(30), + date DATE); +GO + + +CREATE TABLE products ( + productId int PRIMARY KEY, + productName VARCHAR(30), + productPrice INT +) +INSERT INTO products VALUES + (1, 'mac', 250000), + (2, 'iphone', 80000), + (3, 'airpods', 20000), + (4, 'charger', 2900), + (5, 'ipad', 50000) +GO +~~ROW COUNT: 5~~ + + +INSERT INTO orders VALUES + (101, 5,'empA', 0x656D7041, '2024-05-01'), + (102, 3,'empA', 0x656D7041, '2024-05-01'), + (103, 1,'empA', 0x656D7041, '2024-05-01'), + (104, 2,'empA', 0x656D7041, '2024-05-01'), + (105, 1,'empB', 0x656D7042, '2024-05-01'), + (106, 2,'empB', 0x656D7042, '2024-05-01'), + (110, 3,'empB', 0x656D7042, '2024-05-01'), + (109, 4,'empB', 0x656D7042, '2024-05-01'), + (108, 5,'empB', 0x656D7042, '2024-05-01'), + (107, 5,'empB', 0x656D7042, '2024-05-01'), + (111, 1,'empC', 0x656D7043, '2024-05-01'), + (113, 1,'empC', 0x656D7043, '2024-05-01'), + (115, 1,'empC', 0x656D7043, '2024-05-01'), + (119, 1,'empC', 0x656D7043, '2024-05-01'), + (201, 2,'empC', 0x656D7043, '2024-05-01'), + (223, 2,'empC', 0x656D7043, '2024-05-01'), + (224, 5,'empD', 0x656D7044, '2024-05-01'), + (202, 3,'empD', 0x656D7044, '2024-05-01'), + (190, 1,'empD', 0x656D7044, '2024-05-01'); +GO +~~ROW COUNT: 19~~ + + +create schema pivot_schema; +GO + +CREATE TABLE pivot_schema.products_sch ( + productId int PRIMARY KEY, + productName VARCHAR(30), + productPrice INT +) +GO + +INSERT INTO pivot_schema.products_sch VALUES + (1, 'mac', 250000), + (2, 'iphone', 80000), + (3, 'airpods', 20000), + (4, 'charger', 2900), + (5, 'ipad', 50000) +GO +~~ROW COUNT: 5~~ + + +create table pivot_insert_into(ManufactureID int, EmployeeID int, p1 int, p2 int, p3 int, p4 int, p5 int); +GO + +CREATE PROCEDURE top_n_pivot + ( + @Number int = 5 + ) +AS +BEGIN + SELECT TOP(@Number) ManufactureID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 + FROM + ( + SELECT ManufactureID, ItemID, StoreID + FROM StoreReceipt + )as srctable + PIVOT ( + COUNT (ItemID) + FOR StoreID in ([2], [3], [4], [5], [6]) + ) AS pvt2 + ORDER BY 1 +END; +GO + +CREATE FUNCTION test_table_valued_function(@Number int) +RETURNS TABLE +AS +RETURN + SELECT TOP(@Number) ManufactureID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 + FROM + ( + SELECT ManufactureID, ItemID, StoreID + FROM StoreReceipt + )as srctable + PIVOT ( + COUNT (ItemID) + FOR StoreID in ([2], [3], [4], [5], [6]) + ) AS pvt2 + ORDER BY 1 +GO + +CREATE VIEW StoreReceipt_view +AS +SELECT * FROM StoreReceipt; +GO + +-- BABEL-4558 +CREATE TABLE OSTable( + [Oid] [int] NOT NULL, + [Sid] [int] NOT NULL +) +GO + + +CREATE TABLE STable( + [Id] [int] IDENTITY(1,1) NOT NULL, + [Scode] [varchar](10) NOT NULL, + [Type] [smallint] NOT NULL +) +GO + +insert into OSTable (Oid, Sid) values (1, 2); +insert into OSTable (Oid, Sid) values (2, 8); +insert into OSTable (Oid, Sid) values (3, 5); +insert into OSTable (Oid, Sid) values (4, 11); +insert into OSTable (Oid, Sid) values (5, 12); +insert into OSTable (Oid, Sid) values (6, 8); +insert into OSTable (Oid, Sid) values (7, 5); +insert into OSTable (Oid, Sid) values (8, 2); +insert into OSTable (Oid, Sid) values (9, 15); +insert into OSTable (Oid, Sid) values (10, 1); +GO +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + + +insert into STable (Scode, Type) values ('vestibulum', 11); +insert into STable (Scode, Type) values ('eget', 15); +insert into STable (Scode, Type) values ('pharetra', 13); +insert into STable (Scode, Type) values ('nam', 15); +insert into STable (Scode, Type) values ('fermentum', 13); +insert into STable (Scode, Type) values ('hac', 12); +insert into STable (Scode, Type) values ('molestie', 10); +insert into STable (Scode, Type) values ('justo', 11); +insert into STable (Scode, Type) values ('lobortis', 7); +insert into STable (Scode, Type) values ('at', 3); +insert into STable (Scode, Type) values ('augue', 9); +insert into STable (Scode, Type) values ('luctus', 2); +insert into STable (Scode, Type) values ('nisi', 9); +insert into STable (Scode, Type) values ('sociis', 1); +insert into STable (Scode, Type) values ('ultrices', 14); +GO +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + + +-- table for aggregate with string value +CREATE TABLE seating_tbl ( + seatings VARCHAR(20) NOT NULL, + left_right VARCHAR(20) NOT NULL +); +GO + +INSERT INTO seating_tbl (seatings, left_right) +VALUES ('SEAT1', 'LEFT'), + ('SEAT1', 'RIGHT'), + ('SEAT2', 'LEFT'), + ('SEAT3', 'LEFT'), + ('SEAT3', 'RIGHT'); +GO +~~ROW COUNT: 5~~ + + +create table trigger_testing(col nvarchar(60)) +GO + +create trigger pivot_trigger on trigger_testing after insert +as +begin + SELECT 'OrderNumbers' AS OrderCountbyStore, [1] AS STORE1, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5 + FROM + ( + SELECT StoreID, OrderID + FROM StoreReceipt + )AS SrcTable + PIVOT ( + COUNT (OrderID) + FOR StoreID IN ([1], [2], [3],[4], [5]) + ) AS pvt +end +GO diff --git a/test/JDBC/expected/pivot-before-15_8-or-16_4-vu-verify.out b/test/JDBC/expected/pivot-before-15_8-or-16_4-vu-verify.out new file mode 100644 index 00000000000..0a31371a94b --- /dev/null +++ b/test/JDBC/expected/pivot-before-15_8-or-16_4-vu-verify.out @@ -0,0 +1,1938 @@ +use pivot_test +GO + +-- 2 column in src table pivot +SELECT 'OrderNumbers' AS OrderCountbyStore, [1] AS STORE1, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5 +FROM +( + SELECT StoreID, OrderID + FROM StoreReceipt +)AS SrcTable +PIVOT ( + COUNT (OrderID) + FOR StoreID IN ([1], [2], [3],[4], [5]) +) AS pvt +ORDER BY 1 +GO +~~START~~ +text#!#int#!#int#!#int#!#int#!#int +OrderNumbers#!#19#!#19#!#19#!#16#!#14 +~~END~~ + + +-- testing trigger with pivot +insert into trigger_testing (col) select N'Muffler' +GO +~~START~~ +varchar#!#int#!#int#!#int#!#int#!#int +OrderNumbers#!#19#!#19#!#19#!#16#!#14 +~~END~~ + +~~ROW COUNT: 1~~ + + +-- 3 column in src table pivot +SELECT EmployeeID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 +FROM +( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt +)AS srctable +PIVOT ( + COUNT (ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) +) AS pvt +ORDER BY 1 +GO +~~START~~ +int#!#int#!#int#!#int#!#int#!#int +200#!#0#!#0#!#2#!#0#!#0 +201#!#0#!#0#!#0#!#0#!#0 +202#!#1#!#0#!#0#!#0#!#0 +203#!#0#!#0#!#0#!#1#!#0 +204#!#0#!#1#!#0#!#0#!#1 +205#!#0#!#0#!#0#!#0#!#1 +207#!#0#!#0#!#0#!#0#!#0 +208#!#0#!#1#!#0#!#0#!#0 +209#!#0#!#1#!#0#!#0#!#0 +210#!#0#!#0#!#0#!#0#!#0 +211#!#2#!#0#!#0#!#0#!#0 +212#!#0#!#0#!#1#!#0#!#0 +213#!#0#!#0#!#1#!#0#!#0 +214#!#0#!#1#!#0#!#0#!#0 +215#!#0#!#0#!#0#!#0#!#0 +216#!#0#!#1#!#0#!#0#!#0 +217#!#0#!#0#!#0#!#1#!#0 +218#!#0#!#0#!#1#!#0#!#0 +219#!#1#!#0#!#0#!#0#!#1 +220#!#0#!#0#!#0#!#1#!#0 +221#!#0#!#0#!#0#!#0#!#0 +222#!#0#!#0#!#1#!#1#!#0 +223#!#0#!#0#!#0#!#2#!#0 +224#!#0#!#0#!#0#!#0#!#1 +225#!#0#!#1#!#0#!#1#!#0 +226#!#1#!#0#!#0#!#0#!#0 +227#!#0#!#0#!#0#!#0#!#1 +228#!#0#!#1#!#0#!#0#!#0 +229#!#0#!#0#!#1#!#0#!#0 +230#!#1#!#1#!#0#!#0#!#0 +231#!#0#!#0#!#0#!#0#!#0 +232#!#0#!#0#!#0#!#0#!#1 +234#!#0#!#0#!#0#!#0#!#1 +235#!#0#!#0#!#0#!#0#!#0 +236#!#0#!#0#!#1#!#0#!#0 +237#!#1#!#0#!#0#!#0#!#0 +238#!#0#!#0#!#1#!#0#!#0 +239#!#0#!#0#!#0#!#0#!#0 +240#!#0#!#0#!#0#!#0#!#0 +241#!#0#!#0#!#0#!#0#!#0 +243#!#0#!#0#!#0#!#0#!#0 +245#!#0#!#0#!#0#!#0#!#0 +246#!#0#!#0#!#0#!#0#!#0 +247#!#1#!#0#!#0#!#0#!#0 +248#!#0#!#0#!#2#!#0#!#0 +249#!#1#!#0#!#0#!#0#!#0 +250#!#0#!#0#!#0#!#0#!#2 +251#!#1#!#1#!#1#!#0#!#0 +252#!#0#!#0#!#0#!#0#!#0 +253#!#1#!#0#!#0#!#0#!#0 +254#!#0#!#1#!#0#!#0#!#0 +255#!#0#!#1#!#0#!#0#!#0 +256#!#1#!#0#!#0#!#0#!#0 +258#!#0#!#0#!#0#!#0#!#0 +259#!#0#!#1#!#0#!#1#!#0 +260#!#0#!#0#!#0#!#0#!#0 +261#!#0#!#0#!#0#!#0#!#0 +262#!#0#!#1#!#0#!#0#!#0 +264#!#1#!#1#!#0#!#0#!#0 +265#!#0#!#0#!#0#!#0#!#0 +266#!#0#!#0#!#1#!#0#!#0 +267#!#2#!#0#!#0#!#0#!#0 +269#!#1#!#2#!#0#!#0#!#0 +270#!#0#!#0#!#0#!#0#!#1 +271#!#0#!#1#!#0#!#0#!#0 +272#!#0#!#0#!#0#!#0#!#0 +274#!#0#!#0#!#0#!#0#!#0 +275#!#0#!#0#!#0#!#0#!#0 +276#!#0#!#0#!#0#!#0#!#0 +277#!#0#!#0#!#1#!#1#!#0 +278#!#0#!#0#!#0#!#0#!#0 +279#!#0#!#0#!#0#!#0#!#0 +280#!#0#!#0#!#1#!#0#!#0 +282#!#0#!#0#!#0#!#0#!#0 +283#!#0#!#0#!#0#!#0#!#0 +284#!#0#!#0#!#0#!#0#!#0 +285#!#0#!#0#!#0#!#1#!#0 +286#!#0#!#0#!#0#!#0#!#0 +287#!#0#!#0#!#0#!#1#!#0 +288#!#1#!#0#!#0#!#2#!#0 +289#!#0#!#0#!#0#!#0#!#1 +290#!#0#!#0#!#0#!#0#!#0 +291#!#0#!#1#!#0#!#0#!#1 +292#!#1#!#0#!#1#!#0#!#0 +293#!#0#!#0#!#0#!#0#!#0 +294#!#0#!#0#!#0#!#0#!#0 +296#!#1#!#0#!#0#!#0#!#0 +297#!#0#!#0#!#0#!#1#!#0 +298#!#0#!#1#!#0#!#0#!#0 +299#!#0#!#0#!#0#!#0#!#0 +300#!#0#!#0#!#0#!#0#!#1 +~~END~~ + + +-- 3+ column IN src table pivot +SELECT ManufactureID, EmployeeID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 +FROM +( + SELECT ManufactureID, EmployeeID, ItemID, StoreID + FROM StoreReceipt +)AS srctable +PIVOT ( + COUNT (ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) +) AS pvt +ORDER BY 1 +GO +~~START~~ +int#!#int#!#int#!#int#!#int#!#int#!#int +1200#!#200#!#0#!#0#!#0#!#0#!#0 +1200#!#203#!#0#!#0#!#0#!#1#!#0 +1200#!#220#!#0#!#0#!#0#!#0#!#0 +1200#!#222#!#0#!#0#!#0#!#1#!#0 +1200#!#259#!#0#!#0#!#0#!#0#!#0 +1200#!#264#!#0#!#1#!#0#!#0#!#0 +1200#!#266#!#0#!#0#!#1#!#0#!#0 +1200#!#269#!#1#!#0#!#0#!#0#!#0 +1201#!#213#!#0#!#0#!#1#!#0#!#0 +1201#!#229#!#0#!#0#!#1#!#0#!#0 +1201#!#232#!#0#!#0#!#0#!#0#!#0 +1201#!#267#!#0#!#0#!#0#!#0#!#0 +1202#!#228#!#0#!#0#!#0#!#0#!#0 +1202#!#246#!#0#!#0#!#0#!#0#!#0 +1202#!#272#!#0#!#0#!#0#!#0#!#0 +1202#!#287#!#0#!#0#!#0#!#0#!#0 +1202#!#288#!#1#!#0#!#0#!#1#!#0 +1203#!#214#!#0#!#0#!#0#!#0#!#0 +1203#!#220#!#0#!#0#!#0#!#1#!#0 +1203#!#249#!#0#!#0#!#0#!#0#!#0 +1203#!#262#!#0#!#0#!#0#!#0#!#0 +1204#!#224#!#0#!#0#!#0#!#0#!#1 +1204#!#232#!#0#!#0#!#0#!#0#!#0 +1204#!#267#!#0#!#0#!#0#!#0#!#0 +1204#!#282#!#0#!#0#!#0#!#0#!#0 +1204#!#299#!#0#!#0#!#0#!#0#!#0 +1205#!#201#!#0#!#0#!#0#!#0#!#0 +1205#!#234#!#0#!#0#!#0#!#0#!#1 +1206#!#227#!#0#!#0#!#0#!#0#!#0 +1206#!#248#!#0#!#0#!#1#!#0#!#0 +1206#!#289#!#0#!#0#!#0#!#0#!#1 +1207#!#218#!#0#!#0#!#1#!#0#!#0 +1207#!#250#!#0#!#0#!#0#!#0#!#1 +1207#!#279#!#0#!#0#!#0#!#0#!#0 +1208#!#203#!#0#!#0#!#0#!#0#!#0 +1208#!#217#!#0#!#0#!#0#!#1#!#0 +1208#!#223#!#0#!#0#!#0#!#1#!#0 +1208#!#238#!#0#!#0#!#1#!#0#!#0 +1208#!#270#!#0#!#0#!#0#!#0#!#0 +1209#!#252#!#0#!#0#!#0#!#0#!#0 +1209#!#267#!#1#!#0#!#0#!#0#!#0 +1210#!#267#!#1#!#0#!#0#!#0#!#0 +1210#!#298#!#0#!#1#!#0#!#0#!#0 +1211#!#212#!#0#!#0#!#1#!#0#!#0 +1211#!#218#!#0#!#0#!#0#!#0#!#0 +1211#!#219#!#1#!#0#!#0#!#0#!#0 +1211#!#243#!#0#!#0#!#0#!#0#!#0 +1212#!#204#!#0#!#1#!#0#!#0#!#0 +1212#!#247#!#1#!#0#!#0#!#0#!#0 +1212#!#250#!#0#!#0#!#0#!#0#!#1 +1212#!#253#!#1#!#0#!#0#!#0#!#0 +1213#!#276#!#0#!#0#!#0#!#0#!#0 +1213#!#288#!#0#!#0#!#0#!#0#!#0 +1213#!#294#!#0#!#0#!#0#!#0#!#0 +1214#!#211#!#0#!#0#!#0#!#0#!#0 +1214#!#236#!#0#!#0#!#1#!#0#!#0 +1214#!#278#!#0#!#0#!#0#!#0#!#0 +1214#!#289#!#0#!#0#!#0#!#0#!#0 +1214#!#291#!#0#!#0#!#0#!#0#!#1 +1214#!#292#!#0#!#0#!#1#!#0#!#0 +1214#!#296#!#0#!#0#!#0#!#0#!#0 +1215#!#211#!#1#!#0#!#0#!#0#!#0 +1215#!#239#!#0#!#0#!#0#!#0#!#0 +1215#!#249#!#1#!#0#!#0#!#0#!#0 +1215#!#265#!#0#!#0#!#0#!#0#!#0 +1215#!#278#!#0#!#0#!#0#!#0#!#0 +1215#!#283#!#0#!#0#!#0#!#0#!#0 +1215#!#294#!#0#!#0#!#0#!#0#!#0 +1216#!#227#!#0#!#0#!#0#!#0#!#0 +1217#!#230#!#0#!#1#!#0#!#0#!#0 +1217#!#264#!#0#!#0#!#0#!#0#!#0 +1217#!#269#!#0#!#1#!#0#!#0#!#0 +1217#!#292#!#0#!#0#!#0#!#0#!#0 +1218#!#201#!#0#!#0#!#0#!#0#!#0 +1218#!#203#!#0#!#0#!#0#!#0#!#0 +1218#!#214#!#0#!#0#!#0#!#0#!#0 +1218#!#226#!#0#!#0#!#0#!#0#!#0 +1218#!#235#!#0#!#0#!#0#!#0#!#0 +1218#!#240#!#0#!#0#!#0#!#0#!#0 +1218#!#247#!#0#!#0#!#0#!#0#!#0 +1218#!#253#!#0#!#0#!#0#!#0#!#0 +1218#!#254#!#0#!#1#!#0#!#0#!#0 +1219#!#221#!#0#!#0#!#0#!#0#!#0 +1219#!#227#!#0#!#0#!#0#!#0#!#0 +1220#!#234#!#0#!#0#!#0#!#0#!#0 +1220#!#285#!#0#!#0#!#0#!#0#!#0 +1220#!#288#!#0#!#0#!#0#!#1#!#0 +1221#!#204#!#0#!#0#!#0#!#0#!#1 +1221#!#215#!#0#!#0#!#0#!#0#!#0 +1221#!#223#!#0#!#0#!#0#!#1#!#0 +1221#!#225#!#0#!#0#!#0#!#1#!#0 +1221#!#274#!#0#!#0#!#0#!#0#!#0 +1222#!#231#!#0#!#0#!#0#!#0#!#0 +1223#!#208#!#0#!#0#!#0#!#0#!#0 +1223#!#258#!#0#!#0#!#0#!#0#!#0 +1223#!#293#!#0#!#0#!#0#!#0#!#0 +1224#!#210#!#0#!#0#!#0#!#0#!#0 +1224#!#253#!#0#!#0#!#0#!#0#!#0 +1225#!#224#!#0#!#0#!#0#!#0#!#0 +1225#!#278#!#0#!#0#!#0#!#0#!#0 +1226#!#200#!#0#!#0#!#1#!#0#!#0 +1226#!#202#!#1#!#0#!#0#!#0#!#0 +1226#!#214#!#0#!#1#!#0#!#0#!#0 +1226#!#251#!#1#!#0#!#0#!#0#!#0 +1226#!#292#!#1#!#0#!#0#!#0#!#0 +1227#!#209#!#0#!#0#!#0#!#0#!#0 +1227#!#269#!#0#!#1#!#0#!#0#!#0 +1228#!#247#!#0#!#0#!#0#!#0#!#0 +1228#!#248#!#0#!#0#!#1#!#0#!#0 +1229#!#200#!#0#!#0#!#1#!#0#!#0 +1229#!#224#!#0#!#0#!#0#!#0#!#0 +1229#!#225#!#0#!#1#!#0#!#0#!#0 +1229#!#229#!#0#!#0#!#0#!#0#!#0 +1229#!#243#!#0#!#0#!#0#!#0#!#0 +1229#!#270#!#0#!#0#!#0#!#0#!#1 +1229#!#276#!#0#!#0#!#0#!#0#!#0 +1231#!#227#!#0#!#0#!#0#!#0#!#1 +1231#!#228#!#0#!#1#!#0#!#0#!#0 +1231#!#256#!#1#!#0#!#0#!#0#!#0 +1231#!#296#!#0#!#0#!#0#!#0#!#0 +1232#!#211#!#0#!#0#!#0#!#0#!#0 +1232#!#229#!#0#!#0#!#0#!#0#!#0 +1233#!#221#!#0#!#0#!#0#!#0#!#0 +1233#!#254#!#0#!#0#!#0#!#0#!#0 +1233#!#275#!#0#!#0#!#0#!#0#!#0 +1234#!#222#!#0#!#0#!#1#!#0#!#0 +1234#!#230#!#0#!#0#!#0#!#0#!#0 +1234#!#296#!#0#!#0#!#0#!#0#!#0 +1235#!#217#!#0#!#0#!#0#!#0#!#0 +1235#!#255#!#0#!#0#!#0#!#0#!#0 +1235#!#285#!#0#!#0#!#0#!#1#!#0 +1236#!#211#!#1#!#0#!#0#!#0#!#0 +1236#!#235#!#0#!#0#!#0#!#0#!#0 +1236#!#262#!#0#!#1#!#0#!#0#!#0 +1236#!#286#!#0#!#0#!#0#!#0#!#0 +1237#!#241#!#0#!#0#!#0#!#0#!#0 +1237#!#251#!#0#!#1#!#0#!#0#!#0 +1237#!#290#!#0#!#0#!#0#!#0#!#0 +1237#!#291#!#0#!#1#!#0#!#0#!#0 +1238#!#209#!#0#!#1#!#0#!#0#!#0 +1238#!#219#!#0#!#0#!#0#!#0#!#1 +1238#!#234#!#0#!#0#!#0#!#0#!#0 +1238#!#264#!#1#!#0#!#0#!#0#!#0 +1238#!#279#!#0#!#0#!#0#!#0#!#0 +1238#!#300#!#0#!#0#!#0#!#0#!#1 +1239#!#208#!#0#!#1#!#0#!#0#!#0 +1239#!#255#!#0#!#0#!#0#!#0#!#0 +1239#!#259#!#0#!#0#!#0#!#1#!#0 +1239#!#269#!#0#!#0#!#0#!#0#!#0 +1239#!#271#!#0#!#1#!#0#!#0#!#0 +1239#!#277#!#0#!#0#!#0#!#0#!#0 +1240#!#201#!#0#!#0#!#0#!#0#!#0 +1240#!#204#!#0#!#0#!#0#!#0#!#0 +1240#!#251#!#0#!#0#!#0#!#0#!#0 +1240#!#277#!#0#!#0#!#1#!#0#!#0 +1240#!#296#!#1#!#0#!#0#!#0#!#0 +1241#!#243#!#0#!#0#!#0#!#0#!#0 +1241#!#277#!#0#!#0#!#0#!#1#!#0 +1241#!#299#!#0#!#0#!#0#!#0#!#0 +1242#!#210#!#0#!#0#!#0#!#0#!#0 +1242#!#213#!#0#!#0#!#0#!#0#!#0 +1242#!#249#!#0#!#0#!#0#!#0#!#0 +1242#!#280#!#0#!#0#!#1#!#0#!#0 +1242#!#283#!#0#!#0#!#0#!#0#!#0 +1242#!#290#!#0#!#0#!#0#!#0#!#0 +1243#!#213#!#0#!#0#!#0#!#0#!#0 +1243#!#226#!#1#!#0#!#0#!#0#!#0 +1243#!#227#!#0#!#0#!#0#!#0#!#0 +1243#!#232#!#0#!#0#!#0#!#0#!#0 +1243#!#245#!#0#!#0#!#0#!#0#!#0 +1243#!#289#!#0#!#0#!#0#!#0#!#0 +1244#!#248#!#0#!#0#!#0#!#0#!#0 +1244#!#250#!#0#!#0#!#0#!#0#!#0 +1245#!#230#!#1#!#0#!#0#!#0#!#0 +1245#!#237#!#1#!#0#!#0#!#0#!#0 +1245#!#251#!#0#!#0#!#1#!#0#!#0 +1245#!#260#!#0#!#0#!#0#!#0#!#0 +1245#!#289#!#0#!#0#!#0#!#0#!#0 +1245#!#292#!#0#!#0#!#0#!#0#!#0 +1246#!#216#!#0#!#1#!#0#!#0#!#0 +1246#!#259#!#0#!#1#!#0#!#0#!#0 +1246#!#271#!#0#!#0#!#0#!#0#!#0 +1246#!#279#!#0#!#0#!#0#!#0#!#0 +1246#!#286#!#0#!#0#!#0#!#0#!#0 +1246#!#296#!#0#!#0#!#0#!#0#!#0 +1247#!#207#!#0#!#0#!#0#!#0#!#0 +1247#!#231#!#0#!#0#!#0#!#0#!#0 +1247#!#255#!#0#!#1#!#0#!#0#!#0 +1247#!#284#!#0#!#0#!#0#!#0#!#0 +1248#!#297#!#0#!#0#!#0#!#1#!#0 +1249#!#256#!#0#!#0#!#0#!#0#!#0 +1249#!#261#!#0#!#0#!#0#!#0#!#0 +1250#!#203#!#0#!#0#!#0#!#0#!#0 +1250#!#205#!#0#!#0#!#0#!#0#!#1 +1250#!#229#!#0#!#0#!#0#!#0#!#0 +1250#!#232#!#0#!#0#!#0#!#0#!#1 +1250#!#287#!#0#!#0#!#0#!#1#!#0 +~~END~~ + + +-- ORDER by test +SELECT ManufactureID, EmployeeID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 +FROM +( + SELECT ManufactureID, EmployeeID, ItemID, StoreID + FROM StoreReceipt +)AS srctable +PIVOT ( + COUNT (ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) +) AS pvt +ORDER by EmployeeID +GO +~~START~~ +int#!#int#!#int#!#int#!#int#!#int#!#int +1226#!#200#!#0#!#0#!#1#!#0#!#0 +1229#!#200#!#0#!#0#!#1#!#0#!#0 +1200#!#200#!#0#!#0#!#0#!#0#!#0 +1218#!#201#!#0#!#0#!#0#!#0#!#0 +1240#!#201#!#0#!#0#!#0#!#0#!#0 +1205#!#201#!#0#!#0#!#0#!#0#!#0 +1226#!#202#!#1#!#0#!#0#!#0#!#0 +1200#!#203#!#0#!#0#!#0#!#1#!#0 +1250#!#203#!#0#!#0#!#0#!#0#!#0 +1208#!#203#!#0#!#0#!#0#!#0#!#0 +1218#!#203#!#0#!#0#!#0#!#0#!#0 +1212#!#204#!#0#!#1#!#0#!#0#!#0 +1221#!#204#!#0#!#0#!#0#!#0#!#1 +1240#!#204#!#0#!#0#!#0#!#0#!#0 +1250#!#205#!#0#!#0#!#0#!#0#!#1 +1247#!#207#!#0#!#0#!#0#!#0#!#0 +1239#!#208#!#0#!#1#!#0#!#0#!#0 +1223#!#208#!#0#!#0#!#0#!#0#!#0 +1227#!#209#!#0#!#0#!#0#!#0#!#0 +1238#!#209#!#0#!#1#!#0#!#0#!#0 +1224#!#210#!#0#!#0#!#0#!#0#!#0 +1242#!#210#!#0#!#0#!#0#!#0#!#0 +1214#!#211#!#0#!#0#!#0#!#0#!#0 +1236#!#211#!#1#!#0#!#0#!#0#!#0 +1215#!#211#!#1#!#0#!#0#!#0#!#0 +1232#!#211#!#0#!#0#!#0#!#0#!#0 +1211#!#212#!#0#!#0#!#1#!#0#!#0 +1243#!#213#!#0#!#0#!#0#!#0#!#0 +1201#!#213#!#0#!#0#!#1#!#0#!#0 +1242#!#213#!#0#!#0#!#0#!#0#!#0 +1203#!#214#!#0#!#0#!#0#!#0#!#0 +1226#!#214#!#0#!#1#!#0#!#0#!#0 +1218#!#214#!#0#!#0#!#0#!#0#!#0 +1221#!#215#!#0#!#0#!#0#!#0#!#0 +1246#!#216#!#0#!#1#!#0#!#0#!#0 +1235#!#217#!#0#!#0#!#0#!#0#!#0 +1208#!#217#!#0#!#0#!#0#!#1#!#0 +1211#!#218#!#0#!#0#!#0#!#0#!#0 +1207#!#218#!#0#!#0#!#1#!#0#!#0 +1238#!#219#!#0#!#0#!#0#!#0#!#1 +1211#!#219#!#1#!#0#!#0#!#0#!#0 +1203#!#220#!#0#!#0#!#0#!#1#!#0 +1200#!#220#!#0#!#0#!#0#!#0#!#0 +1233#!#221#!#0#!#0#!#0#!#0#!#0 +1219#!#221#!#0#!#0#!#0#!#0#!#0 +1234#!#222#!#0#!#0#!#1#!#0#!#0 +1200#!#222#!#0#!#0#!#0#!#1#!#0 +1208#!#223#!#0#!#0#!#0#!#1#!#0 +1221#!#223#!#0#!#0#!#0#!#1#!#0 +1225#!#224#!#0#!#0#!#0#!#0#!#0 +1204#!#224#!#0#!#0#!#0#!#0#!#1 +1229#!#224#!#0#!#0#!#0#!#0#!#0 +1221#!#225#!#0#!#0#!#0#!#1#!#0 +1229#!#225#!#0#!#1#!#0#!#0#!#0 +1243#!#226#!#1#!#0#!#0#!#0#!#0 +1218#!#226#!#0#!#0#!#0#!#0#!#0 +1206#!#227#!#0#!#0#!#0#!#0#!#0 +1216#!#227#!#0#!#0#!#0#!#0#!#0 +1231#!#227#!#0#!#0#!#0#!#0#!#1 +1243#!#227#!#0#!#0#!#0#!#0#!#0 +1219#!#227#!#0#!#0#!#0#!#0#!#0 +1202#!#228#!#0#!#0#!#0#!#0#!#0 +1231#!#228#!#0#!#1#!#0#!#0#!#0 +1229#!#229#!#0#!#0#!#0#!#0#!#0 +1201#!#229#!#0#!#0#!#1#!#0#!#0 +1250#!#229#!#0#!#0#!#0#!#0#!#0 +1232#!#229#!#0#!#0#!#0#!#0#!#0 +1217#!#230#!#0#!#1#!#0#!#0#!#0 +1234#!#230#!#0#!#0#!#0#!#0#!#0 +1245#!#230#!#1#!#0#!#0#!#0#!#0 +1247#!#231#!#0#!#0#!#0#!#0#!#0 +1222#!#231#!#0#!#0#!#0#!#0#!#0 +1204#!#232#!#0#!#0#!#0#!#0#!#0 +1243#!#232#!#0#!#0#!#0#!#0#!#0 +1201#!#232#!#0#!#0#!#0#!#0#!#0 +1250#!#232#!#0#!#0#!#0#!#0#!#1 +1220#!#234#!#0#!#0#!#0#!#0#!#0 +1238#!#234#!#0#!#0#!#0#!#0#!#0 +1205#!#234#!#0#!#0#!#0#!#0#!#1 +1236#!#235#!#0#!#0#!#0#!#0#!#0 +1218#!#235#!#0#!#0#!#0#!#0#!#0 +1214#!#236#!#0#!#0#!#1#!#0#!#0 +1245#!#237#!#1#!#0#!#0#!#0#!#0 +1208#!#238#!#0#!#0#!#1#!#0#!#0 +1215#!#239#!#0#!#0#!#0#!#0#!#0 +1218#!#240#!#0#!#0#!#0#!#0#!#0 +1237#!#241#!#0#!#0#!#0#!#0#!#0 +1211#!#243#!#0#!#0#!#0#!#0#!#0 +1241#!#243#!#0#!#0#!#0#!#0#!#0 +1229#!#243#!#0#!#0#!#0#!#0#!#0 +1243#!#245#!#0#!#0#!#0#!#0#!#0 +1202#!#246#!#0#!#0#!#0#!#0#!#0 +1212#!#247#!#1#!#0#!#0#!#0#!#0 +1218#!#247#!#0#!#0#!#0#!#0#!#0 +1228#!#247#!#0#!#0#!#0#!#0#!#0 +1228#!#248#!#0#!#0#!#1#!#0#!#0 +1244#!#248#!#0#!#0#!#0#!#0#!#0 +1206#!#248#!#0#!#0#!#1#!#0#!#0 +1215#!#249#!#1#!#0#!#0#!#0#!#0 +1242#!#249#!#0#!#0#!#0#!#0#!#0 +1203#!#249#!#0#!#0#!#0#!#0#!#0 +1212#!#250#!#0#!#0#!#0#!#0#!#1 +1244#!#250#!#0#!#0#!#0#!#0#!#0 +1207#!#250#!#0#!#0#!#0#!#0#!#1 +1240#!#251#!#0#!#0#!#0#!#0#!#0 +1226#!#251#!#1#!#0#!#0#!#0#!#0 +1245#!#251#!#0#!#0#!#1#!#0#!#0 +1237#!#251#!#0#!#1#!#0#!#0#!#0 +1209#!#252#!#0#!#0#!#0#!#0#!#0 +1218#!#253#!#0#!#0#!#0#!#0#!#0 +1212#!#253#!#1#!#0#!#0#!#0#!#0 +1224#!#253#!#0#!#0#!#0#!#0#!#0 +1233#!#254#!#0#!#0#!#0#!#0#!#0 +1218#!#254#!#0#!#1#!#0#!#0#!#0 +1247#!#255#!#0#!#1#!#0#!#0#!#0 +1235#!#255#!#0#!#0#!#0#!#0#!#0 +1239#!#255#!#0#!#0#!#0#!#0#!#0 +1249#!#256#!#0#!#0#!#0#!#0#!#0 +1231#!#256#!#1#!#0#!#0#!#0#!#0 +1223#!#258#!#0#!#0#!#0#!#0#!#0 +1246#!#259#!#0#!#1#!#0#!#0#!#0 +1200#!#259#!#0#!#0#!#0#!#0#!#0 +1239#!#259#!#0#!#0#!#0#!#1#!#0 +1245#!#260#!#0#!#0#!#0#!#0#!#0 +1249#!#261#!#0#!#0#!#0#!#0#!#0 +1203#!#262#!#0#!#0#!#0#!#0#!#0 +1236#!#262#!#0#!#1#!#0#!#0#!#0 +1200#!#264#!#0#!#1#!#0#!#0#!#0 +1238#!#264#!#1#!#0#!#0#!#0#!#0 +1217#!#264#!#0#!#0#!#0#!#0#!#0 +1215#!#265#!#0#!#0#!#0#!#0#!#0 +1200#!#266#!#0#!#0#!#1#!#0#!#0 +1210#!#267#!#1#!#0#!#0#!#0#!#0 +1201#!#267#!#0#!#0#!#0#!#0#!#0 +1209#!#267#!#1#!#0#!#0#!#0#!#0 +1204#!#267#!#0#!#0#!#0#!#0#!#0 +1239#!#269#!#0#!#0#!#0#!#0#!#0 +1200#!#269#!#1#!#0#!#0#!#0#!#0 +1217#!#269#!#0#!#1#!#0#!#0#!#0 +1227#!#269#!#0#!#1#!#0#!#0#!#0 +1208#!#270#!#0#!#0#!#0#!#0#!#0 +1229#!#270#!#0#!#0#!#0#!#0#!#1 +1246#!#271#!#0#!#0#!#0#!#0#!#0 +1239#!#271#!#0#!#1#!#0#!#0#!#0 +1202#!#272#!#0#!#0#!#0#!#0#!#0 +1221#!#274#!#0#!#0#!#0#!#0#!#0 +1233#!#275#!#0#!#0#!#0#!#0#!#0 +1229#!#276#!#0#!#0#!#0#!#0#!#0 +1213#!#276#!#0#!#0#!#0#!#0#!#0 +1240#!#277#!#0#!#0#!#1#!#0#!#0 +1239#!#277#!#0#!#0#!#0#!#0#!#0 +1241#!#277#!#0#!#0#!#0#!#1#!#0 +1225#!#278#!#0#!#0#!#0#!#0#!#0 +1215#!#278#!#0#!#0#!#0#!#0#!#0 +1214#!#278#!#0#!#0#!#0#!#0#!#0 +1207#!#279#!#0#!#0#!#0#!#0#!#0 +1246#!#279#!#0#!#0#!#0#!#0#!#0 +1238#!#279#!#0#!#0#!#0#!#0#!#0 +1242#!#280#!#0#!#0#!#1#!#0#!#0 +1204#!#282#!#0#!#0#!#0#!#0#!#0 +1242#!#283#!#0#!#0#!#0#!#0#!#0 +1215#!#283#!#0#!#0#!#0#!#0#!#0 +1247#!#284#!#0#!#0#!#0#!#0#!#0 +1220#!#285#!#0#!#0#!#0#!#0#!#0 +1235#!#285#!#0#!#0#!#0#!#1#!#0 +1236#!#286#!#0#!#0#!#0#!#0#!#0 +1246#!#286#!#0#!#0#!#0#!#0#!#0 +1250#!#287#!#0#!#0#!#0#!#1#!#0 +1202#!#287#!#0#!#0#!#0#!#0#!#0 +1202#!#288#!#1#!#0#!#0#!#1#!#0 +1220#!#288#!#0#!#0#!#0#!#1#!#0 +1213#!#288#!#0#!#0#!#0#!#0#!#0 +1245#!#289#!#0#!#0#!#0#!#0#!#0 +1206#!#289#!#0#!#0#!#0#!#0#!#1 +1243#!#289#!#0#!#0#!#0#!#0#!#0 +1214#!#289#!#0#!#0#!#0#!#0#!#0 +1237#!#290#!#0#!#0#!#0#!#0#!#0 +1242#!#290#!#0#!#0#!#0#!#0#!#0 +1214#!#291#!#0#!#0#!#0#!#0#!#1 +1237#!#291#!#0#!#1#!#0#!#0#!#0 +1245#!#292#!#0#!#0#!#0#!#0#!#0 +1226#!#292#!#1#!#0#!#0#!#0#!#0 +1217#!#292#!#0#!#0#!#0#!#0#!#0 +1214#!#292#!#0#!#0#!#1#!#0#!#0 +1223#!#293#!#0#!#0#!#0#!#0#!#0 +1213#!#294#!#0#!#0#!#0#!#0#!#0 +1215#!#294#!#0#!#0#!#0#!#0#!#0 +1246#!#296#!#0#!#0#!#0#!#0#!#0 +1214#!#296#!#0#!#0#!#0#!#0#!#0 +1231#!#296#!#0#!#0#!#0#!#0#!#0 +1240#!#296#!#1#!#0#!#0#!#0#!#0 +1234#!#296#!#0#!#0#!#0#!#0#!#0 +1248#!#297#!#0#!#0#!#0#!#1#!#0 +1210#!#298#!#0#!#1#!#0#!#0#!#0 +1204#!#299#!#0#!#0#!#0#!#0#!#0 +1241#!#299#!#0#!#0#!#0#!#0#!#0 +1238#!#300#!#0#!#0#!#0#!#0#!#1 +~~END~~ + + +-- whereclause test +SELECT ManufactureID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 +FROM +( + SELECT ManufactureID, ItemID, StoreID + FROM StoreReceipt +)AS srctable +PIVOT ( + COUNT (ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) +) AS pvt +WHERE ManufactureID < 1220 +ORDER BY 1 +GO +~~START~~ +int#!#int#!#int#!#int#!#int#!#int +1200#!#1#!#1#!#1#!#2#!#0 +1201#!#0#!#0#!#2#!#0#!#0 +1202#!#1#!#0#!#0#!#1#!#0 +1203#!#0#!#0#!#0#!#1#!#0 +1204#!#0#!#0#!#0#!#0#!#1 +1205#!#0#!#0#!#0#!#0#!#1 +1206#!#0#!#0#!#1#!#0#!#1 +1207#!#0#!#0#!#1#!#0#!#1 +1208#!#0#!#0#!#1#!#2#!#0 +1209#!#1#!#0#!#0#!#0#!#0 +1210#!#1#!#1#!#0#!#0#!#0 +1211#!#1#!#0#!#1#!#0#!#0 +1212#!#2#!#1#!#0#!#0#!#1 +1213#!#0#!#0#!#0#!#0#!#0 +1214#!#0#!#0#!#2#!#0#!#1 +1215#!#2#!#0#!#0#!#0#!#0 +1216#!#0#!#0#!#0#!#0#!#0 +1217#!#0#!#2#!#0#!#0#!#0 +1218#!#0#!#1#!#0#!#0#!#0 +1219#!#0#!#0#!#0#!#0#!#0 +~~END~~ + + +-- groupby, having clause test +SELECT EmployeeID, ManufactureID, [2] AS STORE2 +FROM +( + SELECT EmployeeID, ManufactureID, ItemID, StoreID + FROM StoreReceipt +)AS srctable +PIVOT ( + COUNT (ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) +) AS pvt +WHERE EmployeeID < 210 +group by EmployeeID, ManufactureID, [2] +having ManufactureID < 1250 +ORDER by 1,2 +GO +~~START~~ +int#!#int#!#int +200#!#1200#!#0 +200#!#1226#!#0 +200#!#1229#!#0 +201#!#1205#!#0 +201#!#1218#!#0 +201#!#1240#!#0 +202#!#1226#!#1 +203#!#1200#!#0 +203#!#1208#!#0 +203#!#1218#!#0 +204#!#1212#!#0 +204#!#1221#!#0 +204#!#1240#!#0 +207#!#1247#!#0 +208#!#1223#!#0 +208#!#1239#!#0 +209#!#1227#!#0 +209#!#1238#!#0 +~~END~~ + + + +-- TOP test +SELECT TOP 5 ManufactureID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 +FROM +( + SELECT ManufactureID, ItemID, StoreID + FROM StoreReceipt +)AS srctable +PIVOT ( + COUNT (ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) +) AS pvt +ORDER BY 1 +GO +~~START~~ +int#!#int#!#int#!#int#!#int#!#int +1200#!#1#!#1#!#1#!#2#!#0 +1201#!#0#!#0#!#2#!#0#!#0 +1202#!#1#!#0#!#0#!#1#!#0 +1203#!#0#!#0#!#0#!#1#!#0 +1204#!#0#!#0#!#0#!#0#!#1 +~~END~~ + + +-- distinct test +SELECT distinct ManufactureID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 +FROM +( + SELECT ManufactureID, ItemID, StoreID + FROM StoreReceipt +)AS srctable +PIVOT ( + COUNT (ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) +) AS pvt +ORDER BY 1 +GO +~~START~~ +int#!#int#!#int#!#int#!#int#!#int +1200#!#1#!#1#!#1#!#2#!#0 +1201#!#0#!#0#!#2#!#0#!#0 +1202#!#1#!#0#!#0#!#1#!#0 +1203#!#0#!#0#!#0#!#1#!#0 +1204#!#0#!#0#!#0#!#0#!#1 +1205#!#0#!#0#!#0#!#0#!#1 +1206#!#0#!#0#!#1#!#0#!#1 +1207#!#0#!#0#!#1#!#0#!#1 +1208#!#0#!#0#!#1#!#2#!#0 +1209#!#1#!#0#!#0#!#0#!#0 +1210#!#1#!#1#!#0#!#0#!#0 +1211#!#1#!#0#!#1#!#0#!#0 +1212#!#2#!#1#!#0#!#0#!#1 +1213#!#0#!#0#!#0#!#0#!#0 +1214#!#0#!#0#!#2#!#0#!#1 +1215#!#2#!#0#!#0#!#0#!#0 +1216#!#0#!#0#!#0#!#0#!#0 +1217#!#0#!#2#!#0#!#0#!#0 +1218#!#0#!#1#!#0#!#0#!#0 +1219#!#0#!#0#!#0#!#0#!#0 +1220#!#0#!#0#!#0#!#1#!#0 +1221#!#0#!#0#!#0#!#2#!#1 +1222#!#0#!#0#!#0#!#0#!#0 +1223#!#0#!#0#!#0#!#0#!#0 +1224#!#0#!#0#!#0#!#0#!#0 +1225#!#0#!#0#!#0#!#0#!#0 +1226#!#3#!#1#!#1#!#0#!#0 +1227#!#0#!#1#!#0#!#0#!#0 +1228#!#0#!#0#!#1#!#0#!#0 +1229#!#0#!#1#!#1#!#0#!#1 +1231#!#1#!#1#!#0#!#0#!#1 +1232#!#0#!#0#!#0#!#0#!#0 +1233#!#0#!#0#!#0#!#0#!#0 +1234#!#0#!#0#!#1#!#0#!#0 +1235#!#0#!#0#!#0#!#1#!#0 +1236#!#1#!#1#!#0#!#0#!#0 +1237#!#0#!#2#!#0#!#0#!#0 +1238#!#1#!#1#!#0#!#0#!#2 +1239#!#0#!#2#!#0#!#1#!#0 +1240#!#1#!#0#!#1#!#0#!#0 +1241#!#0#!#0#!#0#!#1#!#0 +1242#!#0#!#0#!#1#!#0#!#0 +1243#!#1#!#0#!#0#!#0#!#0 +1244#!#0#!#0#!#0#!#0#!#0 +1245#!#2#!#0#!#1#!#0#!#0 +1246#!#0#!#2#!#0#!#0#!#0 +1247#!#0#!#1#!#0#!#0#!#0 +1248#!#0#!#0#!#0#!#1#!#0 +1249#!#0#!#0#!#0#!#0#!#0 +1250#!#0#!#0#!#0#!#1#!#2 +~~END~~ + + + +-- INSERT INTO test +INSERT INTO pivot_insert_into +SELECT ManufactureID, EmployeeID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 +FROM +( + SELECT ManufactureID, EmployeeID, ItemID, StoreID + FROM StoreReceipt +)AS srctable +PIVOT ( + COUNT (ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) +) AS pvt +ORDER BY 1 +SELECT TOP 10 * FROM pivot_insert_into ORDER by 1, 2; +GO +~~ROW COUNT: 197~~ + +~~START~~ +int#!#int#!#int#!#int#!#int#!#int#!#int +1200#!#200#!#0#!#0#!#0#!#0#!#0 +1200#!#203#!#0#!#0#!#0#!#1#!#0 +1200#!#220#!#0#!#0#!#0#!#0#!#0 +1200#!#222#!#0#!#0#!#0#!#1#!#0 +1200#!#259#!#0#!#0#!#0#!#0#!#0 +1200#!#264#!#0#!#1#!#0#!#0#!#0 +1200#!#266#!#0#!#0#!#1#!#0#!#0 +1200#!#269#!#1#!#0#!#0#!#0#!#0 +1201#!#213#!#0#!#0#!#1#!#0#!#0 +1201#!#229#!#0#!#0#!#1#!#0#!#0 +~~END~~ + + + +-- SELECT INTO test +SELECT ManufactureID, EmployeeID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 +INTO pivot_SELECT_into +FROM +( + SELECT ManufactureID, EmployeeID, ItemID, StoreID + FROM StoreReceipt +)AS srctable +PIVOT ( + COUNT (ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) +) AS pvt +ORDER BY 1 +SELECT TOP 10 * FROM pivot_SELECT_into ORDER by 1, 2; +GO +~~START~~ +int#!#int#!#int#!#int#!#int#!#int#!#int +1200#!#200#!#0#!#0#!#0#!#0#!#0 +1200#!#203#!#0#!#0#!#0#!#1#!#0 +1200#!#220#!#0#!#0#!#0#!#0#!#0 +1200#!#222#!#0#!#0#!#0#!#1#!#0 +1200#!#259#!#0#!#0#!#0#!#0#!#0 +1200#!#264#!#0#!#1#!#0#!#0#!#0 +1200#!#266#!#0#!#0#!#1#!#0#!#0 +1200#!#269#!#1#!#0#!#0#!#0#!#0 +1201#!#213#!#0#!#0#!#1#!#0#!#0 +1201#!#229#!#0#!#0#!#1#!#0#!#0 +~~END~~ + + +-- union test +SELECT TOP 5 EmployeeID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 +FROM +( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt +)AS srctable +PIVOT ( + COUNT (ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) +) AS pvt +UNION +SELECT TOP 5 ManufactureID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 +FROM +( + SELECT ManufactureID, ItemID, StoreID + FROM StoreReceipt +)AS srctable +PIVOT ( + COUNT (ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) +) AS pvt2 +ORDER by 1 +GO +~~START~~ +int#!#int#!#int#!#int#!#int#!#int +200#!#0#!#0#!#2#!#0#!#0 +201#!#0#!#0#!#0#!#0#!#0 +202#!#1#!#0#!#0#!#0#!#0 +203#!#0#!#0#!#0#!#1#!#0 +204#!#0#!#1#!#0#!#0#!#1 +1200#!#1#!#1#!#1#!#2#!#0 +1201#!#0#!#0#!#2#!#0#!#0 +1202#!#1#!#0#!#0#!#1#!#0 +1203#!#0#!#0#!#0#!#1#!#0 +1204#!#0#!#0#!#0#!#0#!#1 +~~END~~ + + +-- sub query test +SELECT TOP 3 * FROM ( + SELECT ManufactureID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 + FROM + ( + SELECT ManufactureID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) + ) AS pvt2 +) p +ORDER BY 1 +GO +~~START~~ +int#!#int#!#int#!#int#!#int#!#int +1200#!#1#!#1#!#1#!#2#!#0 +1201#!#0#!#0#!#2#!#0#!#0 +1202#!#1#!#0#!#0#!#1#!#0 +~~END~~ + + +-- table variable test +DECLARE @pivot_table_var TABLE ( + ManufactureID INT, + ItemID INT, + StoreID INT +); +INSERT INTO @pivot_table_var SELECT ManufactureID, ItemID, StoreID FROM StoreReceipt; +SELECT TOP 5 ManufactureID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 +FROM + @pivot_table_var +PIVOT ( + COUNT (ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) +) AS pvt2 +ORDER BY 1 +GO +~~ROW COUNT: 200~~ + +~~START~~ +int#!#int#!#int#!#int#!#int#!#int +1200#!#1#!#1#!#1#!#2#!#0 +1201#!#0#!#0#!#2#!#0#!#0 +1202#!#1#!#0#!#0#!#1#!#0 +1203#!#0#!#0#!#0#!#1#!#0 +1204#!#0#!#0#!#0#!#0#!#1 +~~END~~ + + +-- temp table test +SELECT ManufactureID, ItemID, StoreID INTO #pivot_temp_table FROM StoreReceipt; +SELECT TOP 5 ManufactureID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 +FROM + #pivot_temp_table +PIVOT ( + COUNT (ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) +) AS pvt2 +ORDER BY 1 +GO +~~START~~ +int#!#int#!#int#!#int#!#int#!#int +1200#!#1#!#1#!#1#!#2#!#0 +1201#!#0#!#0#!#2#!#0#!#0 +1202#!#1#!#0#!#0#!#1#!#0 +1203#!#0#!#0#!#0#!#1#!#0 +1204#!#0#!#0#!#0#!#0#!#1 +~~END~~ + + +-- procedure test +exec top_n_pivot 10 +GO +~~START~~ +int#!#int#!#int#!#int#!#int#!#int +1200#!#1#!#1#!#1#!#2#!#0 +1201#!#0#!#0#!#2#!#0#!#0 +1202#!#1#!#0#!#0#!#1#!#0 +1203#!#0#!#0#!#0#!#1#!#0 +1204#!#0#!#0#!#0#!#0#!#1 +1205#!#0#!#0#!#0#!#0#!#1 +1206#!#0#!#0#!#1#!#0#!#1 +1207#!#0#!#0#!#1#!#0#!#1 +1208#!#0#!#0#!#1#!#2#!#0 +1209#!#1#!#0#!#0#!#0#!#0 +~~END~~ + + +exec top_n_pivot 5 +GO +~~START~~ +int#!#int#!#int#!#int#!#int#!#int +1200#!#1#!#1#!#1#!#2#!#0 +1201#!#0#!#0#!#2#!#0#!#0 +1202#!#1#!#0#!#0#!#1#!#0 +1203#!#0#!#0#!#0#!#1#!#0 +1204#!#0#!#0#!#0#!#0#!#1 +~~END~~ + + +-- function test +SELECT * FROM test_table_valued_function(12) ORDER BY 1 +GO +~~START~~ +int#!#int#!#int#!#int#!#int#!#int +1200#!#1#!#1#!#1#!#2#!#0 +1201#!#0#!#0#!#2#!#0#!#0 +1202#!#1#!#0#!#0#!#1#!#0 +1203#!#0#!#0#!#0#!#1#!#0 +1204#!#0#!#0#!#0#!#0#!#1 +1205#!#0#!#0#!#0#!#0#!#1 +1206#!#0#!#0#!#1#!#0#!#1 +1207#!#0#!#0#!#1#!#0#!#1 +1208#!#0#!#0#!#1#!#2#!#0 +1209#!#1#!#0#!#0#!#0#!#0 +1210#!#1#!#1#!#0#!#0#!#0 +1211#!#1#!#0#!#1#!#0#!#0 +~~END~~ + + +SELECT * FROM test_table_valued_function(2) ORDER BY 1 +GO +~~START~~ +int#!#int#!#int#!#int#!#int#!#int +1200#!#1#!#1#!#1#!#2#!#0 +1201#!#0#!#0#!#2#!#0#!#0 +~~END~~ + + +-- explain pivot +SET BABELFISH_SHOWPLAN_ALL ON; +SELECT TOP 5 ManufactureID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 +FROM +( + SELECT ManufactureID, ItemID, StoreID + FROM StoreReceipt +)AS srctable +PIVOT ( + COUNT (ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) +) AS pvt +ORDER BY 1 +GO +~~START~~ +text +Query Text: SELECT TOP 5 ManufactureID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 +FROM +( + SELECT ManufactureID, ItemID, StoreID + FROM StoreReceipt +)AS srctable +PIVOT ( + COUNT (ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) +) AS pvt +ORDER BY 1 +Limit (cost=26.61..26.62 rows=5 width=24) + -> Sort (cost=26.61..29.11 rows=1000 width=24) + Sort Key: "ManufactureID" NULLS FIRST + -> Function Scan on bbf_pivot pvt (cost=0.00..10.00 rows=1000 width=24) +~~END~~ + +~~START~~ +text +Babelfish T-SQL Batch Parsing Time: 26.761 ms +~~END~~ + +SET BABELFISH_SHOWPLAN_ALL OFF; +GO + + +-- test column name with indirection (value column) +SELECT TOP 5 ManufactureID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 +FROM +( + SELECT ManufactureID, ItemID, StoreID + FROM StoreReceipt +)AS srctable +PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) +) AS pvt +ORDER BY 1 +GO +~~START~~ +int#!#int#!#int#!#int#!#int#!#int +1200#!#1#!#1#!#1#!#2#!#0 +1201#!#0#!#0#!#2#!#0#!#0 +1202#!#1#!#0#!#0#!#1#!#0 +1203#!#0#!#0#!#0#!#1#!#0 +1204#!#0#!#0#!#0#!#0#!#1 +~~END~~ + + +-- test column name win indirection (category column) +SELECT TOP 5 ManufactureID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 +FROM +( + SELECT ManufactureID, ItemID, StoreID + FROM StoreReceipt +)AS srctable +PIVOT ( + COUNT (ItemID) + FOR srctable.StoreID IN ([2], [3], [4], [5], [6]) +) AS pvt +ORDER BY 1 +GO +~~START~~ +int#!#int#!#int#!#int#!#int#!#int +1200#!#1#!#1#!#1#!#2#!#0 +1201#!#0#!#0#!#2#!#0#!#0 +1202#!#1#!#0#!#0#!#1#!#0 +1203#!#0#!#0#!#0#!#1#!#0 +1204#!#0#!#0#!#0#!#0#!#1 +~~END~~ + + +-- CTE test data +CREATE TABLE #FruitSales +(FruitType VARCHAR(20), SalesYear INT, FruitSales MONEY); +GO + +INSERT INTO #FruitSales VALUES('Orange', 2024, 23425); +INSERT INTO #FruitSales VALUES('Orange', 2024, 54234); +INSERT INTO #FruitSales VALUES('Orange', 2023, 12490); +INSERT INTO #FruitSales VALUES('Orange', 2023, 4535); +INSERT INTO #FruitSales VALUES('Banana', 2024, 45745); +INSERT INTO #FruitSales VALUES('Banana', 2024, 5636); +INSERT INTO #FruitSales VALUES('Banana', 2023, 24654); +INSERT INTO #FruitSales VALUES('Banana', 2023, 6547); +GO +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + + +-- CTE test 1 +WITH +SalesTotal AS +( +SELECT FruitType, + [2023] AS [2023_Total], + [2024] AS [2024_Total] +FROM #FruitSales + PIVOT(SUM(FruitSales) + FOR SalesYear IN([2023], [2024]) + ) AS PivotSales +), +SalesAvg AS +( +SELECT FruitType, + [2023] AS [2023_Avg], + [2024] AS [2024_Avg] +FROM #FruitSales + PIVOT(AVG(FruitSales) + FOR SalesYear IN([2023], [2024]) + ) AS PivotSales +) +SELECT st.FruitType, st.[2023_Total], sa.[2023_Avg], + st.[2024_Total], sa.[2024_Avg] +FROM SalesTotal AS st + INNER JOIN SalesAvg AS sa + ON st.FruitType = sa.FruitType +ORDER BY 1 +GO +~~START~~ +varchar#!#money#!#money#!#money#!#money +Banana#!#31201.0000#!#15600.5000#!#51381.0000#!#25690.5000 +Orange#!#17025.0000#!#8512.5000#!#77659.0000#!#38829.5000 +~~END~~ + + +-- CTE test 2 +WITH +SalesTotal AS +( +SELECT FruitType, + [2023] AS [2023_Total], + [2024] AS [2024_Total] +FROM #FruitSales + PIVOT(SUM(FruitSales) + FOR SalesYear IN([2023], [2024]) + ) AS PivotSales +), +SalesAvg AS +( +SELECT FruitType, + [2023] AS [2023_Avg], + [2024] AS [2024_Avg] +FROM #FruitSales + PIVOT(AVG(FruitSales) + FOR SalesYear IN([2023], [2024]) + ) AS PivotSales +) +SELECT * from SalesTotal ORDER BY FruitType; +GO +~~START~~ +varchar#!#money#!#money +Banana#!#31201.0000#!#51381.0000 +Orange#!#17025.0000#!#77659.0000 +~~END~~ + + +-- CTE test 3 +WITH +SalesTotal AS +( +SELECT FruitType, + [2023] AS [2023_Total], + [2024] AS [2024_Total] +FROM #FruitSales + PIVOT(SUM(FruitSales) + FOR SalesYear IN([2023], [2024]) + ) AS PivotSales +), +SalesAvg AS +( +SELECT FruitType, + [2023] AS [2023_Avg], + [2024] AS [2024_Avg] +FROM #FruitSales + PIVOT(AVG(FruitSales) + FOR SalesYear IN([2023], [2024]) + ) AS PivotSales +) +SELECT * from SalesAvg ORDER BY FruitType; +GO +~~START~~ +varchar#!#money#!#money +Banana#!#15600.5000#!#25690.5000 +Orange#!#8512.5000#!#38829.5000 +~~END~~ + + +-- CTE of 3 expression table +WITH +SalesTotal AS +( +SELECT FruitType, + [2023] AS [2023_Total], + [2024] AS [2024_Total] +FROM #FruitSales + PIVOT(SUM(FruitSales) + FOR SalesYear IN([2023], [2024]) + ) AS PivotSales +), +SalesAvg AS +( +SELECT FruitType, + [2023] AS [2023_Avg], + [2024] AS [2024_Avg] +FROM #FruitSales + PIVOT(AVG(FruitSales) + FOR SalesYear IN([2023], [2024]) + ) AS PivotSales +), +SalesMin AS +( +SELECT FruitType, + [2023] AS [2023_min], + [2024] AS [2024_min] +FROM #FruitSales + PIVOT(MIN(FruitSales) + FOR SalesYear IN([2023], [2024]) + ) AS PivotSales +) +SELECT st.FruitType, st.[2023_Total], sa.[2023_Avg], + st.[2024_Total], sa.[2024_Avg], sm.[2023_min],sm.[2024_min] +FROM SalesTotal AS st + INNER JOIN SalesAvg AS sa + ON st.FruitType = sa.FruitType + INNER JOIN SalesMin as sm + ON sa.FruitType = sm.FruitType +ORDER BY 1 +GO +~~START~~ +varchar#!#money#!#money#!#money#!#money#!#money#!#money +Banana#!#31201.0000#!#15600.5000#!#51381.0000#!#25690.5000#!#6547.0000#!#5636.0000 +Orange#!#17025.0000#!#8512.5000#!#77659.0000#!#38829.5000#!#4535.0000#!#23425.0000 +~~END~~ + + +-- Test stmt of CTE table and PIVOT stmt in different level +WITH +SalesTotal AS +( + SELECT FruitType, + [2023] AS [2023_Total], + [2024] AS [2024_Total] + FROM #FruitSales + PIVOT(SUM(FruitSales) + FOR SalesYear IN([2023], [2024]) + ) AS PivotSales +) +SELECT st.FruitType, st.[2023_Total], sa.[2023_Avg], + st.[2024_Total], sa.[2024_Avg] +FROM SalesTotal AS st +JOIN ( + SELECT FruitType, + [2023] AS [2023_Avg], + [2024] AS [2024_Avg] + FROM #FruitSales + PIVOT(AVG(FruitSales) + FOR SalesYear IN([2023], [2024]) + ) AS PivotSales +) sa ON st.FruitType = sa.FruitType; +GO +~~START~~ +varchar#!#money#!#money#!#money#!#money +Banana#!#31201.0000#!#15600.5000#!#51381.0000#!#25690.5000 +Orange#!#17025.0000#!#8512.5000#!#77659.0000#!#38829.5000 +~~END~~ + + +DROP TABlE IF EXISTS #FruitSales +GO + +-- PIVOT with CTE as source table +WITH cte_table AS ( + SELECT [p].productName, [o].[employeeName] + FROM orders [o] JOIN products AS [p] on (o.productId = p.productId) +) +SELECT CAST('COUNT' AS VARCHAR(10)), [mac],[ipad],[charger] FROM cte_table +PIVOT ( + COUNT(employeeName) + FOR productName IN (mac, [iphone], [ipad], [charger]) +) as pvt +GO +~~START~~ +varchar#!#int#!#int#!#int +COUNT#!#7#!#4#!#1 +~~END~~ + + +-- string is not allowed in PIVOT column value list +WITH cte_table AS ( + SELECT o.[orderId], o.[productId], [p].productName, + [p].productPrice, [o].[employeeName], [o].employeeCode, [o].date + FROM orders [o] JOIN products AS [p] on (o.productId = p.productId) +) +SELECT * FROM cte_table +PIVOT ( + COUNT(orderId) + FOR productName IN ('mac', 'iphone', 'ipad', 'charger') +) as p +GO +~~ERROR (Code: 33557097)~~ + +~~ERROR (Message: syntax error at or near "'mac'")~~ + + +-- aggregate column in PIVOT column value list is not allowed +WITH cte_table AS +( + SELECT + CAST('COUNT' AS VARCHAR(10)) AS COUNT, + [mac], [ipad], [charger], [employeeName] + FROM ( + SELECT [o].employeeName, [p].productName + FROM orders [o] JOIN products AS [p] on ([o].productId = [p].productId) + ) AS dervied_table +PIVOT + ( + COUNT(employeeName) + FOR productName IN ([mac], [employeeName], [iphone], [ipad], [charger]) + ) as pvt +) +SELECT * FROM cte_table +GO +~~ERROR (Code: 33557097)~~ + +~~ERROR (Message: The column name "employeename" specified in the PIVOT operator conflicts with the existing column name in the PIVOT argument.)~~ + + +-- Join stmts inside PIVOT statment (BABEL-4558) +SELECT Oid, [1] AS TYPE1, [2] AS TYPE2, [3] AS TYPE3 +FROM (SELECT OSTable.Oid, STable.Scode, STable.Type + FROM OSTable + INNER JOIN STable + ON OSTable.Sid = STable.Id + ) AS SourceTable +PIVOT ( MAX(Scode) FOR [Type] IN ([1], [2], [3])) + AS os_pivot +ORDER BY 1 +GO +~~START~~ +int#!#varchar#!#varchar#!#varchar +1#!##!##!# +2#!##!##!# +3#!##!##!# +4#!##!##!# +5#!##!#luctus#!# +6#!##!##!# +7#!##!##!# +8#!##!##!# +9#!##!##!# +10#!##!##!# +~~END~~ + + +-- JOIN TEST +SELECT * FROM +( + SELECT TOP 10 EmployeeID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 + FROM + ( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) + ) AS pvt where EmployeeID > 250 +) AS p1 +JOIN +( + SELECT TOP 10 EmployeeID, [7] AS STORE7, [8] AS STORE8, [9] AS STORE9, [10] AS STORE10 + FROM + ( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([7], [8], [9], [10]) + ) AS pvt where EmployeeID > 245 +) AS p2 +ON p1.EmployeeID = p2.EmployeeID ORDER BY 1 +GO +~~START~~ +int#!#int#!#int#!#int#!#int#!#int#!#int#!#int#!#int#!#int#!#int +251#!#1#!#1#!#1#!#0#!#0#!#251#!#0#!#0#!#0#!#0 +252#!#0#!#0#!#0#!#0#!#0#!#252#!#1#!#0#!#0#!#0 +253#!#1#!#0#!#0#!#0#!#0#!#253#!#1#!#0#!#1#!#0 +254#!#0#!#1#!#0#!#0#!#0#!#254#!#0#!#0#!#0#!#0 +255#!#0#!#1#!#0#!#0#!#0#!#255#!#0#!#2#!#0#!#0 +~~END~~ + + +-- INNER JOIN TEST +SELECT * FROM +( + SELECT TOP 10 EmployeeID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 + FROM + ( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) + ) AS pvt where EmployeeID > 250 +) AS p1 +INNER JOIN +( + SELECT TOP 10 EmployeeID, [7] AS STORE7, [8] AS STORE8, [9] AS STORE9, [10] AS STORE10 + FROM + ( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([7], [8], [9], [10]) + ) AS pvt where EmployeeID > 245 +) AS p2 +ON p1.EmployeeID = p2.EmployeeID ORDER BY 1 +GO +~~START~~ +int#!#int#!#int#!#int#!#int#!#int#!#int#!#int#!#int#!#int#!#int +251#!#1#!#1#!#1#!#0#!#0#!#251#!#0#!#0#!#0#!#0 +252#!#0#!#0#!#0#!#0#!#0#!#252#!#1#!#0#!#0#!#0 +253#!#1#!#0#!#0#!#0#!#0#!#253#!#1#!#0#!#1#!#0 +254#!#0#!#1#!#0#!#0#!#0#!#254#!#0#!#0#!#0#!#0 +255#!#0#!#1#!#0#!#0#!#0#!#255#!#0#!#2#!#0#!#0 +~~END~~ + + +-- LEFT JOIN TEST +SELECT * FROM +( + SELECT TOP 10 EmployeeID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 + FROM + ( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) + ) AS pvt where EmployeeID > 250 +) AS p1 +LEFT JOIN +( + SELECT TOP 10 EmployeeID, [7] AS STORE7, [8] AS STORE8, [9] AS STORE9, [10] AS STORE10 + FROM + ( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([7], [8], [9], [10]) + ) AS pvt where EmployeeID > 245 +) AS p2 +ON p1.EmployeeID = p2.EmployeeID ORDER BY 1 +GO +~~START~~ +int#!#int#!#int#!#int#!#int#!#int#!#int#!#int#!#int#!#int#!#int +251#!#1#!#1#!#1#!#0#!#0#!#251#!#0#!#0#!#0#!#0 +252#!#0#!#0#!#0#!#0#!#0#!#252#!#1#!#0#!#0#!#0 +253#!#1#!#0#!#0#!#0#!#0#!#253#!#1#!#0#!#1#!#0 +254#!#0#!#1#!#0#!#0#!#0#!#254#!#0#!#0#!#0#!#0 +255#!#0#!#1#!#0#!#0#!#0#!#255#!#0#!#2#!#0#!#0 +256#!#1#!#0#!#0#!#0#!#0#!##!##!##!##!# +258#!#0#!#0#!#0#!#0#!#0#!##!##!##!##!# +259#!#0#!#1#!#0#!#1#!#0#!##!##!##!##!# +260#!#0#!#0#!#0#!#0#!#0#!##!##!##!##!# +261#!#0#!#0#!#0#!#0#!#0#!##!##!##!##!# +~~END~~ + + +-- RIGHT JOIN TEST +SELECT * FROM +( + SELECT TOP 10 EmployeeID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 + FROM + ( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) + ) AS pvt where EmployeeID > 250 +) AS p1 +RIGHT JOIN +( + SELECT TOP 10 EmployeeID, [7] AS STORE7, [8] AS STORE8, [9] AS STORE9, [10] AS STORE10 + FROM + ( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([7], [8], [9], [10]) + ) AS pvt where EmployeeID > 245 +) AS p2 +ON p1.EmployeeID = p2.EmployeeID ORDER BY 1 +GO +~~START~~ +int#!#int#!#int#!#int#!#int#!#int#!#int#!#int#!#int#!#int#!#int +#!##!##!##!##!##!#246#!#0#!#1#!#0#!#0 +#!##!##!##!##!##!#247#!#0#!#0#!#2#!#0 +#!##!##!##!##!##!#248#!#0#!#0#!#0#!#1 +#!##!##!##!##!##!#249#!#1#!#1#!#0#!#0 +#!##!##!##!##!##!#250#!#0#!#0#!#0#!#1 +251#!#1#!#1#!#1#!#0#!#0#!#251#!#0#!#0#!#0#!#0 +252#!#0#!#0#!#0#!#0#!#0#!#252#!#1#!#0#!#0#!#0 +253#!#1#!#0#!#0#!#0#!#0#!#253#!#1#!#0#!#1#!#0 +254#!#0#!#1#!#0#!#0#!#0#!#254#!#0#!#0#!#0#!#0 +255#!#0#!#1#!#0#!#0#!#0#!#255#!#0#!#2#!#0#!#0 +~~END~~ + + +-- FULL JOIN TEST +SELECT * FROM +( + SELECT TOP 10 EmployeeID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 + FROM + ( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) + ) AS pvt where EmployeeID > 250 +) AS p1 +FULL JOIN +( + SELECT TOP 10 EmployeeID, [7] AS STORE7, [8] AS STORE8, [9] AS STORE9, [10] AS STORE10 + FROM + ( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([7], [8], [9], [10]) + ) AS pvt where EmployeeID > 245 +) AS p2 +ON p1.EmployeeID = p2.EmployeeID ORDER BY 1 +GO +~~START~~ +int#!#int#!#int#!#int#!#int#!#int#!#int#!#int#!#int#!#int#!#int +#!##!##!##!##!##!#250#!#0#!#0#!#0#!#1 +#!##!##!##!##!##!#247#!#0#!#0#!#2#!#0 +#!##!##!##!##!##!#246#!#0#!#1#!#0#!#0 +#!##!##!##!##!##!#248#!#0#!#0#!#0#!#1 +#!##!##!##!##!##!#249#!#1#!#1#!#0#!#0 +251#!#1#!#1#!#1#!#0#!#0#!#251#!#0#!#0#!#0#!#0 +252#!#0#!#0#!#0#!#0#!#0#!#252#!#1#!#0#!#0#!#0 +253#!#1#!#0#!#0#!#0#!#0#!#253#!#1#!#0#!#1#!#0 +254#!#0#!#1#!#0#!#0#!#0#!#254#!#0#!#0#!#0#!#0 +255#!#0#!#1#!#0#!#0#!#0#!#255#!#0#!#2#!#0#!#0 +256#!#1#!#0#!#0#!#0#!#0#!##!##!##!##!# +258#!#0#!#0#!#0#!#0#!#0#!##!##!##!##!# +259#!#0#!#1#!#0#!#1#!#0#!##!##!##!##!# +260#!#0#!#0#!#0#!#0#!#0#!##!##!##!##!# +261#!#0#!#0#!#0#!#0#!#0#!##!##!##!##!# +~~END~~ + + +-- CROSS JOIN TEST +SELECT * FROM +( + SELECT TOP 5 EmployeeID, [2] AS STORE2 + FROM + ( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) + ) AS pvt where EmployeeID > 250 +) AS p1 +CROSS JOIN +( + SELECT TOP 5 EmployeeID, [10] AS STORE10 + FROM + ( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([7], [8], [9], [10]) + ) AS pvt where EmployeeID > 245 +) AS p2 ORDER BY 1 +GO +~~START~~ +int#!#int#!#int#!#int +251#!#1#!#246#!#0 +251#!#1#!#247#!#0 +251#!#1#!#248#!#1 +251#!#1#!#249#!#0 +251#!#1#!#250#!#1 +252#!#0#!#246#!#0 +252#!#0#!#247#!#0 +252#!#0#!#248#!#1 +252#!#0#!#249#!#0 +252#!#0#!#250#!#1 +253#!#1#!#246#!#0 +253#!#1#!#247#!#0 +253#!#1#!#248#!#1 +253#!#1#!#249#!#0 +253#!#1#!#250#!#1 +254#!#0#!#246#!#0 +254#!#0#!#247#!#0 +254#!#0#!#248#!#1 +254#!#0#!#249#!#0 +254#!#0#!#250#!#1 +255#!#0#!#246#!#0 +255#!#0#!#247#!#0 +255#!#0#!#248#!#1 +255#!#0#!#249#!#0 +255#!#0#!#250#!#1 +~~END~~ + + +-- COMMA JOIN TEST +SELECT * FROM +( + SELECT TOP 10 EmployeeID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 + FROM + ( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) + ) AS pvt where EmployeeID > 250 +) AS p1 +, +( + SELECT TOP 10 EmployeeID, [7] AS STORE7, [8] AS STORE8, [9] AS STORE9, [10] AS STORE10 + FROM + ( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([7], [8], [9], [10]) + ) AS pvt where EmployeeID > 245 +) AS p2 +WHERE p1.EmployeeID = p2.EmployeeID ORDER BY 1 +GO +~~START~~ +int#!#int#!#int#!#int#!#int#!#int#!#int#!#int#!#int#!#int#!#int +251#!#1#!#1#!#1#!#0#!#0#!#251#!#0#!#0#!#0#!#0 +252#!#0#!#0#!#0#!#0#!#0#!#252#!#1#!#0#!#0#!#0 +253#!#1#!#0#!#0#!#0#!#0#!#253#!#1#!#0#!#1#!#0 +254#!#0#!#1#!#0#!#0#!#0#!#254#!#0#!#0#!#0#!#0 +255#!#0#!#1#!#0#!#0#!#0#!#255#!#0#!#2#!#0#!#0 +~~END~~ + + +-- COMMA CROSS JOIN TEST +SELECT * FROM +( + SELECT TOP 5 EmployeeID, [2] AS STORE2 + FROM + ( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) + ) AS pvt where EmployeeID > 250 +) AS p1 +, +( + SELECT TOP 5 EmployeeID, [10] AS STORE10 + FROM + ( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([7], [8], [9], [10]) + ) AS pvt where EmployeeID > 245 +) AS p2 ORDER BY 1 +GO +~~START~~ +int#!#int#!#int#!#int +251#!#1#!#246#!#0 +251#!#1#!#247#!#0 +251#!#1#!#248#!#1 +251#!#1#!#249#!#0 +251#!#1#!#250#!#1 +252#!#0#!#246#!#0 +252#!#0#!#247#!#0 +252#!#0#!#248#!#1 +252#!#0#!#249#!#0 +252#!#0#!#250#!#1 +253#!#1#!#246#!#0 +253#!#1#!#247#!#0 +253#!#1#!#248#!#1 +253#!#1#!#249#!#0 +253#!#1#!#250#!#1 +254#!#0#!#246#!#0 +254#!#0#!#247#!#0 +254#!#0#!#248#!#1 +254#!#0#!#249#!#0 +254#!#0#!#250#!#1 +255#!#0#!#246#!#0 +255#!#0#!#247#!#0 +255#!#0#!#248#!#1 +255#!#0#!#249#!#0 +255#!#0#!#250#!#1 +~~END~~ + + +--3+ JOIN TEST +SELECT * FROM +( + SELECT TOP 10 EmployeeID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 + FROM + ( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) + ) AS pvt where EmployeeID > 250 +) AS p1 +JOIN +( + SELECT TOP 10 EmployeeID, [7] AS STORE7, [8] AS STORE8, [9] AS STORE9, [10] AS STORE10 + FROM + ( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([7], [8], [9], [10]) + ) AS pvt where EmployeeID > 245 +) AS p2 +ON p1.EmployeeID = p2.EmployeeID +JOIN +( + SELECT TOP 10 EmployeeID, [1] AS STORE1 + FROM + ( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([1]) + ) AS pvt where EmployeeID > 248 +)AS p3 +ON p2.EmployeeID = p3.EmployeeID ORDER BY 1 +GO +~~START~~ +int#!#int#!#int#!#int#!#int#!#int#!#int#!#int#!#int#!#int#!#int#!#int#!#int +251#!#1#!#1#!#1#!#0#!#0#!#251#!#0#!#0#!#0#!#0#!#251#!#1 +252#!#0#!#0#!#0#!#0#!#0#!#252#!#1#!#0#!#0#!#0#!#252#!#0 +253#!#1#!#0#!#0#!#0#!#0#!#253#!#1#!#0#!#1#!#0#!#253#!#0 +254#!#0#!#1#!#0#!#0#!#0#!#254#!#0#!#0#!#0#!#0#!#254#!#1 +255#!#0#!#1#!#0#!#0#!#0#!#255#!#0#!#2#!#0#!#0#!#255#!#0 +~~END~~ + + + +-- Result Order Test +-- JOIN A +SELECT p2.EmployeeID, STORE7, STORE8, STORE9, STORE10 FROM +( + SELECT TOP 10 EmployeeID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 + FROM + ( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) + ) AS pvt where EmployeeID > 250 +) AS p1 +JOIN +( + SELECT TOP 10 EmployeeID, [7] AS STORE7, [8] AS STORE8, [9] AS STORE9, [10] AS STORE10 + FROM + ( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([7], [8], [9], [10]) + ) AS pvt where EmployeeID > 245 +) AS p2 +ON p1.EmployeeID = p2.EmployeeID ORDER BY 1 +GO +~~START~~ +int#!#int#!#int#!#int#!#int +251#!#0#!#0#!#0#!#0 +252#!#1#!#0#!#0#!#0 +253#!#1#!#0#!#1#!#0 +254#!#0#!#0#!#0#!#0 +255#!#0#!#2#!#0#!#0 +~~END~~ + + +-- JOIN B (Reference) +SELECT * FROM +( + SELECT TOP 10 EmployeeID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 + FROM + ( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) + ) AS pvt where EmployeeID > 250 +) AS p1 +JOIN +( + SELECT TOP 10 EmployeeID, [7] AS STORE7, [8] AS STORE8, [9] AS STORE9, [10] AS STORE10 + FROM + ( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([7], [8], [9], [10]) + ) AS pvt where EmployeeID > 245 +) AS p2 +ON p1.EmployeeID = p2.EmployeeID ORDER BY 1 +GO +~~START~~ +int#!#int#!#int#!#int#!#int#!#int#!#int#!#int#!#int#!#int#!#int +251#!#1#!#1#!#1#!#0#!#0#!#251#!#0#!#0#!#0#!#0 +252#!#0#!#0#!#0#!#0#!#0#!#252#!#1#!#0#!#0#!#0 +253#!#1#!#0#!#0#!#0#!#0#!#253#!#1#!#0#!#1#!#0 +254#!#0#!#1#!#0#!#0#!#0#!#254#!#0#!#0#!#0#!#0 +255#!#0#!#1#!#0#!#0#!#0#!#255#!#0#!#2#!#0#!#0 +~~END~~ + + +-- Test view as a data source in a stmt with pivot operator +SELECT TOP 5 EmployeeID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 +FROM +( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt_view +)AS srctable +PIVOT ( + COUNT (ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) +) AS pvt +GO +~~START~~ +int#!#int#!#int#!#int#!#int#!#int +200#!#0#!#0#!#2#!#0#!#0 +201#!#0#!#0#!#0#!#0#!#0 +202#!#1#!#0#!#0#!#0#!#0 +203#!#0#!#0#!#0#!#1#!#0 +204#!#0#!#1#!#0#!#0#!#1 +~~END~~ + + +-- aggregate string value, when no row is selected, should output NULL +SELECT [seatings], [LEFT], [RIGHT] +FROM +( + SELECT [seatings], left_right + FROM seating_tbl +) AS p1 +PIVOT ( + MAX(left_right) + FOR left_right IN ([LEFT], [RIGHT]) +) AS p2 +ORDER BY 1 +GO +~~START~~ +varchar#!#varchar#!#varchar +SEAT1#!#LEFT#!#RIGHT +SEAT2#!#LEFT#!# +SEAT3#!#LEFT#!#RIGHT +~~END~~ + + +-- test pivot with table in different schemas 1 +SELECT CAST('COUNT' AS VARCHAR(10)) AS COUNT, [mac], [ipad], [charger] +FROM ( + SELECT [o].employeeName, [p].productName + FROM dbo.orders [o] JOIN products AS [p] on ([o].productId = [p].productId) +) AS dervied_table +PIVOT( + COUNT(employeeName) + FOR productName IN ([mac], [iphone], [ipad], [charger]) +) as pvt +GO +~~START~~ +varchar#!#int#!#int#!#int +COUNT#!#7#!#4#!#1 +~~END~~ + + +-- test pivot with table in different schemas 2 +SELECT CAST('COUNT' AS VARCHAR(10)) AS COUNT, [mac], [ipad], [charger] +FROM ( + SELECT [o].employeeName, [p].productName + FROM dbo.orders [o] JOIN pivot_schema.products_sch AS [p] on ([o].productId = [p].productId) +) AS dervied_table +PIVOT( + COUNT(employeeName) + FOR productName IN ([mac], [iphone], [ipad], [charger]) +) as pvt +GO +~~START~~ +varchar#!#int#!#int#!#int +COUNT#!#7#!#4#!#1 +~~END~~ + diff --git a/test/JDBC/expected/pivot-vu-cleanup.out b/test/JDBC/expected/pivot-vu-cleanup.out index baa3bd3a7e6..29a2952abcd 100644 --- a/test/JDBC/expected/pivot-vu-cleanup.out +++ b/test/JDBC/expected/pivot-vu-cleanup.out @@ -1,61 +1,122 @@ -use pivot_test; +USE pivot_test; GO -drop trigger pivot_trigger +DROP VIEW PIVOT_VIEW2 GO -drop table trigger_testing +DROP VIEW PIVOT_VIEW3 GO -drop table OSTable; +DROP VIEW PIVOT_VIEW4 GO -drop table STable; +DROP VIEW TOP_PIVOT GO -drop table seating_tbl; +DROP VIEW DISTINCT_PIVOT GO -drop view StoreReceipt_view; +DROP VIEW UNION_PIVOT GO -drop view pivot_view; +DROP VIEW SUBQUERY_PIVOT GO -~~ERROR (Code: 3701)~~ -~~ERROR (Message: view "pivot_view" does not exist)~~ +DROP VIEW PIVOT_FUNCTION +GO + +DROP VIEW CTE_VIEW1 +GO + +DROP VIEW CTE_VIEW2 +GO + +DROP VIEW CTE_VIEW3 +GO + +DROP VIEW JOIN_PIVOT1 +GO + +DROP VIEW JOIN_PIVOT2 +GO + +DROP VIEW JOIN_PIVOT3 +GO + +DROP VIEW JOIN_PIVOT4 +GO + +DROP VIEW JOIN_PIVOT5 +GO + +DROP VIEW JOIN_PIVOT6 +GO +DROP VIEW JOIN_PIVOT7 +GO + +DROP VIEW JOIN_PIVOT8 +GO + +DROP TRIGGER pivot_trigger +GO + +DROP TABLE trigger_testing +GO + +DROP TABLE OSTable; +GO + +DROP TABLE STable; +GO + +DROP TABLE seating_tbl; +GO + +DROP VIEW StoreReceipt_view; +GO + +DROP TABLE pivot_insert_into; +GO + +DROP TABLE pivot_select_into; +GO -drop table pivot_insert_into; +DROP PROCEDURE top_n_pivot; GO -drop table pivot_select_into; +DROP FUNCTION test_table_valued_function; GO -drop procedure top_n_pivot; +DROP TABLE FruitSalesTable GO -drop function test_table_valued_function; +DROP TABLE StoreReceipt; GO -drop table StoreReceipt; +DROP TABLE orders; GO -drop table orders; +DROP TABLE products; GO -drop table products; +DROP TABLE pivot_schema.products_sch; GO -drop table pivot_schema.products_sch; +DROP SCHEMA pivot_schema; GO -drop schema pivot_schema; +USE master; GO -use master; +DROP DATABASE pivot_test; GO -drop database pivot_test; +--- Check for inconsistent metadata +SELECT COUNT(*) FROM sys.babelfish_inconsistent_metadata() GO +~~START~~ +int +0 +~~END~~ diff --git a/test/JDBC/expected/pivot-vu-prepare.out b/test/JDBC/expected/pivot-vu-prepare.out index eaaa9743fe3..36213af2551 100644 --- a/test/JDBC/expected/pivot-vu-prepare.out +++ b/test/JDBC/expected/pivot-vu-prepare.out @@ -731,29 +731,7 @@ AS SELECT * FROM StoreReceipt; GO --- Test create view for stmt with pivot operator --- Expected to fail --- Create view with pivot is not yet supported -CREATE VIEW pivot_view -AS -SELECT TOP(5) ManufactureID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 -FROM -( - SELECT ManufactureID, ItemID, StoreID - FROM StoreReceipt -)as srctable -PIVOT ( - COUNT (ItemID) - FOR StoreID in ([2], [3], [4], [5], [6]) -) AS pvt -ORDER BY 1 -GO -~~ERROR (Code: 33557097)~~ - -~~ERROR (Message: Create view on stmt with PIVOT operator is not currently supported.)~~ - - --- BABEL-4558 +-- BABEL-4558 CREATE TABLE OSTable( [Oid] [int] NOT NULL, [Sid] [int] NOT NULL @@ -882,3 +860,499 @@ begin ) AS pvt end GO + +CREATE TABLE FruitSalesTable +(FruitType VARCHAR(20), SalesYear INT, FruitSales MONEY); +GO + +INSERT INTO FruitSalesTable VALUES('Orange', 2024, 23425); +INSERT INTO FruitSalesTable VALUES('Orange', 2024, 54234); +INSERT INTO FruitSalesTable VALUES('Orange', 2023, 12490); +INSERT INTO FruitSalesTable VALUES('Orange', 2023, 4535); +INSERT INTO FruitSalesTable VALUES('Banana', 2024, 45745); +INSERT INTO FruitSalesTable VALUES('Banana', 2024, 5636); +INSERT INTO FruitSalesTable VALUES('Banana', 2023, 24654); +INSERT INTO FruitSalesTable VALUES('Banana', 2023, 6547); +GO +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + + +-- PIVOT VIEW TESTS +-- VIEW OF 2 COLUMN SRC TABLE PIVOT +CREATE VIEW PIVOT_VIEW2 +AS +SELECT 'OrderNumbers' AS OrderCountbyStore, [1] AS STORE1, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5 +FROM +( + SELECT StoreID, OrderID + FROM StoreReceipt +)AS SrcTable +PIVOT ( + COUNT (OrderID) + FOR StoreID IN ([1], [2], [3],[4], [5]) +) AS pvt +GO + +-- VIEW OF 3 COLUMN SRC TABLE PIVOT +CREATE VIEW PIVOT_VIEW3 +AS +SELECT TOP(5) ManufactureID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 +FROM +( + SELECT ManufactureID, ItemID, StoreID + FROM StoreReceipt +)as srctable +PIVOT ( + COUNT (ItemID) + FOR StoreID in ([2], [3], [4], [5], [6]) +) AS pvt +ORDER BY 1 +GO + +-- VIEW OF 4 COLUMN SRC TABLE PIVOT +CREATE VIEW PIVOT_VIEW4 +AS +SELECT ManufactureID, EmployeeID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 +FROM +( + SELECT ManufactureID, EmployeeID, ItemID, StoreID + FROM StoreReceipt +)AS srctable +PIVOT ( + COUNT (ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) +) AS pvt +GO + +-- VIEW OF TOP PIVOT +CREATE VIEW TOP_PIVOT +AS +SELECT TOP 5 ManufactureID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 +FROM +( + SELECT ManufactureID, ItemID, StoreID + FROM StoreReceipt +)AS srctable +PIVOT ( + COUNT (ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) +) AS pvt +ORDER BY 1 +GO + +-- VIEW OF DISTINCT PIVOT +CREATE VIEW DISTINCT_PIVOT +AS +SELECT distinct ManufactureID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 +FROM +( + SELECT ManufactureID, ItemID, StoreID + FROM StoreReceipt +)AS srctable +PIVOT ( + COUNT (ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) +) AS pvt +GO + +-- VIEW OF UNION PIVOT +CREATE VIEW UNION_PIVOT +AS +SELECT TOP 5 EmployeeID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 +FROM +( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt +)AS srctable +PIVOT ( + COUNT (ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) +) AS pvt +UNION +SELECT TOP 5 ManufactureID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 +FROM +( + SELECT ManufactureID, ItemID, StoreID + FROM StoreReceipt +)AS srctable +PIVOT ( + COUNT (ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) +) AS pvt2 +ORDER by 1 +GO + +-- VIEW OF PIVOT IN SUBQUERY +CREATE VIEW SUBQUERY_PIVOT +AS +SELECT TOP 3 ManufactureID, STORE2, STORE3 FROM ( + SELECT ManufactureID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 + FROM + ( + SELECT ManufactureID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) + ) AS pvt2 +) p +ORDER BY 1 +GO + +-- VIEW OF PIVOT FUNCTION +CREATE VIEW PIVOT_FUNCTION +AS +SELECT ManufactureID, STORE2, STORE3 FROM test_table_valued_function(12) +GO + +-- VIEW OF CTE PIVOT AS SRC TABLE +CREATE VIEW CTE_VIEW1 +AS +WITH +SalesTotal AS +( +SELECT FruitType, + [2023] AS [2023_Total], + [2024] AS [2024_Total] +FROM FruitSalesTable + PIVOT(SUM(FruitSales) + FOR SalesYear IN([2023], [2024]) + ) AS PivotSales +), +SalesAvg AS +( +SELECT FruitType, + [2023] AS [2023_Avg], + [2024] AS [2024_Avg] +FROM FruitSalesTable + PIVOT(AVG(FruitSales) + FOR SalesYear IN([2023], [2024]) + ) AS PivotSales +) +SELECT st.FruitType, st.[2023_Total], sa.[2023_Avg], + st.[2024_Total], sa.[2024_Avg] +FROM SalesTotal AS st + INNER JOIN SalesAvg AS sa + ON st.FruitType = sa.FruitType +ORDER BY 1 +GO + +-- VIEW OF CTE PIVOT AS SRC TABLE JOIN ANOTHER PIVOT +CREATE VIEW CTE_VIEW2 +AS +WITH +SalesTotal AS +( + SELECT FruitType, + [2023] AS [2023_Total], + [2024] AS [2024_Total] + FROM FruitSalesTable + PIVOT(SUM(FruitSales) + FOR SalesYear IN([2023], [2024]) + ) AS PivotSales +) +SELECT st.FruitType, st.[2023_Total], sa.[2023_Avg], + st.[2024_Total], sa.[2024_Avg] +FROM SalesTotal AS st +JOIN ( + SELECT FruitType, + [2023] AS [2023_Avg], + [2024] AS [2024_Avg] + FROM FruitSalesTable + PIVOT(AVG(FruitSales) + FOR SalesYear IN([2023], [2024]) + ) AS PivotSales +) sa ON st.FruitType = sa.FruitType; +GO + +-- VIEW OF CTE TABLE AS PIVOT SRC TABLE +CREATE VIEW CTE_VIEW3 +AS +WITH cte_table AS ( + SELECT [p].productName, [o].[employeeName] + FROM orders [o] JOIN products AS [p] on (o.productId = p.productId) +) +SELECT CAST('COUNT' AS VARCHAR(10)), [mac],[ipad],[charger] FROM cte_table +PIVOT ( + COUNT(employeeName) + FOR productName IN (mac, [iphone], [ipad], [charger]) +) as pvt +GO + +-- VIEW OF JOIN PIVOT +CREATE VIEW JOIN_PIVOT1 +AS +SELECT p1.EmployeeID as p1_EmployeeID, STORE2, STORE3, p2.EmployeeID as p2_EmployeeID, STORE7, STORE8 FROM +( + SELECT TOP 10 EmployeeID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 + FROM + ( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) + ) AS pvt where EmployeeID > 250 +) AS p1 +JOIN +( + SELECT TOP 10 EmployeeID, [7] AS STORE7, [8] AS STORE8, [9] AS STORE9, [10] AS STORE10 + FROM + ( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([7], [8], [9], [10]) + ) AS pvt where EmployeeID > 245 +) AS p2 +ON p1.EmployeeID = p2.EmployeeID +GO + +-- VIEW OF INNER JOIN PIVOT +CREATE VIEW JOIN_PIVOT2 +AS +SELECT p1.EmployeeID as p1_EmployeeID, STORE2, STORE3, p2.EmployeeID as p2_EmployeeID, STORE7, STORE8 FROM +( + SELECT TOP 10 EmployeeID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 + FROM + ( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) + ) AS pvt where EmployeeID > 250 +) AS p1 +INNER JOIN +( + SELECT TOP 10 EmployeeID, [7] AS STORE7, [8] AS STORE8, [9] AS STORE9, [10] AS STORE10 + FROM + ( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([7], [8], [9], [10]) + ) AS pvt where EmployeeID > 245 +) AS p2 +ON p1.EmployeeID = p2.EmployeeID +GO + +-- VIEW OF LEFT JOIN PIVOT +CREATE VIEW JOIN_PIVOT3 +AS +SELECT p1.EmployeeID as p1_EmployeeID, STORE2, STORE3, p2.EmployeeID as p2_EmployeeID, STORE7, STORE8 FROM +( + SELECT TOP 10 EmployeeID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 + FROM + ( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) + ) AS pvt where EmployeeID > 250 +) AS p1 +LEFT JOIN +( + SELECT TOP 10 EmployeeID, [7] AS STORE7, [8] AS STORE8, [9] AS STORE9, [10] AS STORE10 + FROM + ( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([7], [8], [9], [10]) + ) AS pvt where EmployeeID > 245 +) AS p2 +ON p1.EmployeeID = p2.EmployeeID +GO + +-- VIEW OF RIGHT JOIN PIVOT +CREATE VIEW JOIN_PIVOT4 +AS +SELECT p1.EmployeeID as p1_EmployeeID, STORE2, STORE3, p2.EmployeeID as p2_EmployeeID, STORE7, STORE8 FROM +( + SELECT TOP 10 EmployeeID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 + FROM + ( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) + ) AS pvt where EmployeeID > 250 +) AS p1 +RIGHT JOIN +( + SELECT TOP 10 EmployeeID, [7] AS STORE7, [8] AS STORE8, [9] AS STORE9, [10] AS STORE10 + FROM + ( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([7], [8], [9], [10]) + ) AS pvt where EmployeeID > 245 +) AS p2 +ON p1.EmployeeID = p2.EmployeeID +GO + +-- VIEW OF FULL JOIN PIVOT +CREATE VIEW JOIN_PIVOT5 +AS +SELECT p1.EmployeeID as p1_EmployeeID, STORE2, STORE3, p2.EmployeeID as p2_EmployeeID, STORE7, STORE8 FROM +( + SELECT TOP 10 EmployeeID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 + FROM + ( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) + ) AS pvt where EmployeeID > 250 +) AS p1 +FULL JOIN +( + SELECT TOP 10 EmployeeID, [7] AS STORE7, [8] AS STORE8, [9] AS STORE9, [10] AS STORE10 + FROM + ( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([7], [8], [9], [10]) + ) AS pvt where EmployeeID > 245 +) AS p2 +ON p1.EmployeeID = p2.EmployeeID +GO + +-- VIEW OF CROSS JOIN PIVOT +CREATE VIEW JOIN_PIVOT6 +AS +SELECT p1.EmployeeID as p1_EmployeeID, STORE2, p2.EmployeeID as p2_EmployeeID, STORE10 FROM +( + SELECT TOP 5 EmployeeID, [2] AS STORE2 + FROM + ( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) + ) AS pvt where EmployeeID > 250 +) AS p1 +CROSS JOIN +( + SELECT TOP 5 EmployeeID, [10] AS STORE10 + FROM + ( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([7], [8], [9], [10]) + ) AS pvt where EmployeeID > 245 +) AS p2 +GO + +-- VIEW OF COMMA JOIN PIVOT +CREATE VIEW JOIN_PIVOT7 +AS +SELECT p1.EmployeeID as p1_EmployeeID, STORE2, STORE3, p2.EmployeeID as p2_EmployeeID, STORE7, STORE8 FROM +( + SELECT TOP 10 EmployeeID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 + FROM + ( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) + ) AS pvt where EmployeeID > 250 +) AS p1 +, +( + SELECT TOP 10 EmployeeID, [7] AS STORE7, [8] AS STORE8, [9] AS STORE9, [10] AS STORE10 + FROM + ( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([7], [8], [9], [10]) + ) AS pvt where EmployeeID > 245 +) AS p2 +WHERE p1.EmployeeID = p2.EmployeeID +GO + +-- VIEW OF COMMA CROSS JOIN PIVOT +CREATE VIEW JOIN_PIVOT8 +AS +SELECT p1.EmployeeID as p1_EmployeeID, STORE2, p2.EmployeeID as p2_EmployeeID, STORE10 FROM +( + SELECT TOP 5 EmployeeID, [2] AS STORE2 + FROM + ( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) + ) AS pvt where EmployeeID > 250 +) AS p1 +, +( + SELECT TOP 5 EmployeeID, [10] AS STORE10 + FROM + ( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([7], [8], [9], [10]) + ) AS pvt where EmployeeID > 245 +) AS p2 +GO + +--- Check for inconsistent metadata before upgrade +SELECT COUNT(*) FROM sys.babelfish_inconsistent_metadata(); +GO +~~START~~ +int +0 +~~END~~ + diff --git a/test/JDBC/expected/pivot-vu-verify.out b/test/JDBC/expected/pivot-vu-verify.out index 1a159e01b80..2cc36e5e752 100644 --- a/test/JDBC/expected/pivot-vu-verify.out +++ b/test/JDBC/expected/pivot-vu-verify.out @@ -1,6 +1,15 @@ use pivot_test GO +--- Check for inconsistent metadata after upgrade +SELECT COUNT(*) FROM sys.babelfish_inconsistent_metadata(); +GO +~~START~~ +int +0 +~~END~~ + + -- 2 column in src table pivot SELECT 'OrderNumbers' AS OrderCountbyStore, [1] AS STORE1, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5 FROM @@ -1009,7 +1018,7 @@ Limit (cost=26.61..26.62 rows=5 width=24) ~~START~~ text -Babelfish T-SQL Batch Parsing Time: 22.186 ms +Babelfish T-SQL Batch Parsing Time: 27.497 ms ~~END~~ SET BABELFISH_SHOWPLAN_ALL OFF; @@ -1882,18 +1891,6 @@ int#!#int#!#int#!#int#!#int#!#int ~~END~~ --- Test view of a stmt with pivot operator --- Expected to fail since we failed to create view with pivot at prepare script. --- Create view with pivot is not yet supported, -SELECT ManufactureID, STORE2, STORE3, STORE4, STORE5, STORE6 -FROM pivot_view -ORDER BY ManufactureID -GO -~~ERROR (Code: 33557097)~~ - -~~ERROR (Message: relation "pivot_view" does not exist)~~ - - -- aggregate string value, when no row is selected, should output NULL SELECT [seatings], [LEFT], [RIGHT] FROM @@ -1948,3 +1945,544 @@ varchar#!#int#!#int#!#int COUNT#!#7#!#4#!#1 ~~END~~ + +--PIVOT VIEW TESTS +-- VIEW WITH 2 COLUMN SRC TABLE PIVOT +SELECT * FROM PIVOT_VIEW2 ORDER BY 1 +GO +~~START~~ +varchar#!#int#!#int#!#int#!#int#!#int +OrderNumbers#!#19#!#19#!#19#!#16#!#14 +~~END~~ + + +-- VIEW WITH 3 COLUMN SRC TABLE PIVOT +SELECT * FROM PIVOT_VIEW3 ORDER BY 1 +GO +~~START~~ +int#!#int#!#int#!#int#!#int#!#int +1200#!#1#!#1#!#1#!#2#!#0 +1201#!#0#!#0#!#2#!#0#!#0 +1202#!#1#!#0#!#0#!#1#!#0 +1203#!#0#!#0#!#0#!#1#!#0 +1204#!#0#!#0#!#0#!#0#!#1 +~~END~~ + + +-- VIEW OF 4 COLUMN SRC TABLE PIVOT +SELECT * FROM PIVOT_VIEW4 ORDER BY 1 +GO +~~START~~ +int#!#int#!#int#!#int#!#int#!#int#!#int +1200#!#200#!#0#!#0#!#0#!#0#!#0 +1200#!#203#!#0#!#0#!#0#!#1#!#0 +1200#!#220#!#0#!#0#!#0#!#0#!#0 +1200#!#222#!#0#!#0#!#0#!#1#!#0 +1200#!#259#!#0#!#0#!#0#!#0#!#0 +1200#!#264#!#0#!#1#!#0#!#0#!#0 +1200#!#266#!#0#!#0#!#1#!#0#!#0 +1200#!#269#!#1#!#0#!#0#!#0#!#0 +1201#!#213#!#0#!#0#!#1#!#0#!#0 +1201#!#229#!#0#!#0#!#1#!#0#!#0 +1201#!#232#!#0#!#0#!#0#!#0#!#0 +1201#!#267#!#0#!#0#!#0#!#0#!#0 +1202#!#228#!#0#!#0#!#0#!#0#!#0 +1202#!#246#!#0#!#0#!#0#!#0#!#0 +1202#!#272#!#0#!#0#!#0#!#0#!#0 +1202#!#287#!#0#!#0#!#0#!#0#!#0 +1202#!#288#!#1#!#0#!#0#!#1#!#0 +1203#!#214#!#0#!#0#!#0#!#0#!#0 +1203#!#220#!#0#!#0#!#0#!#1#!#0 +1203#!#249#!#0#!#0#!#0#!#0#!#0 +1203#!#262#!#0#!#0#!#0#!#0#!#0 +1204#!#224#!#0#!#0#!#0#!#0#!#1 +1204#!#232#!#0#!#0#!#0#!#0#!#0 +1204#!#267#!#0#!#0#!#0#!#0#!#0 +1204#!#282#!#0#!#0#!#0#!#0#!#0 +1204#!#299#!#0#!#0#!#0#!#0#!#0 +1205#!#201#!#0#!#0#!#0#!#0#!#0 +1205#!#234#!#0#!#0#!#0#!#0#!#1 +1206#!#227#!#0#!#0#!#0#!#0#!#0 +1206#!#248#!#0#!#0#!#1#!#0#!#0 +1206#!#289#!#0#!#0#!#0#!#0#!#1 +1207#!#218#!#0#!#0#!#1#!#0#!#0 +1207#!#250#!#0#!#0#!#0#!#0#!#1 +1207#!#279#!#0#!#0#!#0#!#0#!#0 +1208#!#203#!#0#!#0#!#0#!#0#!#0 +1208#!#217#!#0#!#0#!#0#!#1#!#0 +1208#!#223#!#0#!#0#!#0#!#1#!#0 +1208#!#238#!#0#!#0#!#1#!#0#!#0 +1208#!#270#!#0#!#0#!#0#!#0#!#0 +1209#!#252#!#0#!#0#!#0#!#0#!#0 +1209#!#267#!#1#!#0#!#0#!#0#!#0 +1210#!#267#!#1#!#0#!#0#!#0#!#0 +1210#!#298#!#0#!#1#!#0#!#0#!#0 +1211#!#212#!#0#!#0#!#1#!#0#!#0 +1211#!#218#!#0#!#0#!#0#!#0#!#0 +1211#!#219#!#1#!#0#!#0#!#0#!#0 +1211#!#243#!#0#!#0#!#0#!#0#!#0 +1212#!#204#!#0#!#1#!#0#!#0#!#0 +1212#!#247#!#1#!#0#!#0#!#0#!#0 +1212#!#250#!#0#!#0#!#0#!#0#!#1 +1212#!#253#!#1#!#0#!#0#!#0#!#0 +1213#!#276#!#0#!#0#!#0#!#0#!#0 +1213#!#288#!#0#!#0#!#0#!#0#!#0 +1213#!#294#!#0#!#0#!#0#!#0#!#0 +1214#!#211#!#0#!#0#!#0#!#0#!#0 +1214#!#236#!#0#!#0#!#1#!#0#!#0 +1214#!#278#!#0#!#0#!#0#!#0#!#0 +1214#!#289#!#0#!#0#!#0#!#0#!#0 +1214#!#291#!#0#!#0#!#0#!#0#!#1 +1214#!#292#!#0#!#0#!#1#!#0#!#0 +1214#!#296#!#0#!#0#!#0#!#0#!#0 +1215#!#211#!#1#!#0#!#0#!#0#!#0 +1215#!#239#!#0#!#0#!#0#!#0#!#0 +1215#!#249#!#1#!#0#!#0#!#0#!#0 +1215#!#265#!#0#!#0#!#0#!#0#!#0 +1215#!#278#!#0#!#0#!#0#!#0#!#0 +1215#!#283#!#0#!#0#!#0#!#0#!#0 +1215#!#294#!#0#!#0#!#0#!#0#!#0 +1216#!#227#!#0#!#0#!#0#!#0#!#0 +1217#!#230#!#0#!#1#!#0#!#0#!#0 +1217#!#264#!#0#!#0#!#0#!#0#!#0 +1217#!#269#!#0#!#1#!#0#!#0#!#0 +1217#!#292#!#0#!#0#!#0#!#0#!#0 +1218#!#201#!#0#!#0#!#0#!#0#!#0 +1218#!#203#!#0#!#0#!#0#!#0#!#0 +1218#!#214#!#0#!#0#!#0#!#0#!#0 +1218#!#226#!#0#!#0#!#0#!#0#!#0 +1218#!#235#!#0#!#0#!#0#!#0#!#0 +1218#!#240#!#0#!#0#!#0#!#0#!#0 +1218#!#247#!#0#!#0#!#0#!#0#!#0 +1218#!#253#!#0#!#0#!#0#!#0#!#0 +1218#!#254#!#0#!#1#!#0#!#0#!#0 +1219#!#221#!#0#!#0#!#0#!#0#!#0 +1219#!#227#!#0#!#0#!#0#!#0#!#0 +1220#!#234#!#0#!#0#!#0#!#0#!#0 +1220#!#285#!#0#!#0#!#0#!#0#!#0 +1220#!#288#!#0#!#0#!#0#!#1#!#0 +1221#!#204#!#0#!#0#!#0#!#0#!#1 +1221#!#215#!#0#!#0#!#0#!#0#!#0 +1221#!#223#!#0#!#0#!#0#!#1#!#0 +1221#!#225#!#0#!#0#!#0#!#1#!#0 +1221#!#274#!#0#!#0#!#0#!#0#!#0 +1222#!#231#!#0#!#0#!#0#!#0#!#0 +1223#!#208#!#0#!#0#!#0#!#0#!#0 +1223#!#258#!#0#!#0#!#0#!#0#!#0 +1223#!#293#!#0#!#0#!#0#!#0#!#0 +1224#!#210#!#0#!#0#!#0#!#0#!#0 +1224#!#253#!#0#!#0#!#0#!#0#!#0 +1225#!#224#!#0#!#0#!#0#!#0#!#0 +1225#!#278#!#0#!#0#!#0#!#0#!#0 +1226#!#200#!#0#!#0#!#1#!#0#!#0 +1226#!#202#!#1#!#0#!#0#!#0#!#0 +1226#!#214#!#0#!#1#!#0#!#0#!#0 +1226#!#251#!#1#!#0#!#0#!#0#!#0 +1226#!#292#!#1#!#0#!#0#!#0#!#0 +1227#!#209#!#0#!#0#!#0#!#0#!#0 +1227#!#269#!#0#!#1#!#0#!#0#!#0 +1228#!#247#!#0#!#0#!#0#!#0#!#0 +1228#!#248#!#0#!#0#!#1#!#0#!#0 +1229#!#200#!#0#!#0#!#1#!#0#!#0 +1229#!#224#!#0#!#0#!#0#!#0#!#0 +1229#!#225#!#0#!#1#!#0#!#0#!#0 +1229#!#229#!#0#!#0#!#0#!#0#!#0 +1229#!#243#!#0#!#0#!#0#!#0#!#0 +1229#!#270#!#0#!#0#!#0#!#0#!#1 +1229#!#276#!#0#!#0#!#0#!#0#!#0 +1231#!#227#!#0#!#0#!#0#!#0#!#1 +1231#!#228#!#0#!#1#!#0#!#0#!#0 +1231#!#256#!#1#!#0#!#0#!#0#!#0 +1231#!#296#!#0#!#0#!#0#!#0#!#0 +1232#!#211#!#0#!#0#!#0#!#0#!#0 +1232#!#229#!#0#!#0#!#0#!#0#!#0 +1233#!#221#!#0#!#0#!#0#!#0#!#0 +1233#!#254#!#0#!#0#!#0#!#0#!#0 +1233#!#275#!#0#!#0#!#0#!#0#!#0 +1234#!#222#!#0#!#0#!#1#!#0#!#0 +1234#!#230#!#0#!#0#!#0#!#0#!#0 +1234#!#296#!#0#!#0#!#0#!#0#!#0 +1235#!#217#!#0#!#0#!#0#!#0#!#0 +1235#!#255#!#0#!#0#!#0#!#0#!#0 +1235#!#285#!#0#!#0#!#0#!#1#!#0 +1236#!#211#!#1#!#0#!#0#!#0#!#0 +1236#!#235#!#0#!#0#!#0#!#0#!#0 +1236#!#262#!#0#!#1#!#0#!#0#!#0 +1236#!#286#!#0#!#0#!#0#!#0#!#0 +1237#!#241#!#0#!#0#!#0#!#0#!#0 +1237#!#251#!#0#!#1#!#0#!#0#!#0 +1237#!#290#!#0#!#0#!#0#!#0#!#0 +1237#!#291#!#0#!#1#!#0#!#0#!#0 +1238#!#209#!#0#!#1#!#0#!#0#!#0 +1238#!#219#!#0#!#0#!#0#!#0#!#1 +1238#!#234#!#0#!#0#!#0#!#0#!#0 +1238#!#264#!#1#!#0#!#0#!#0#!#0 +1238#!#279#!#0#!#0#!#0#!#0#!#0 +1238#!#300#!#0#!#0#!#0#!#0#!#1 +1239#!#208#!#0#!#1#!#0#!#0#!#0 +1239#!#255#!#0#!#0#!#0#!#0#!#0 +1239#!#259#!#0#!#0#!#0#!#1#!#0 +1239#!#269#!#0#!#0#!#0#!#0#!#0 +1239#!#271#!#0#!#1#!#0#!#0#!#0 +1239#!#277#!#0#!#0#!#0#!#0#!#0 +1240#!#201#!#0#!#0#!#0#!#0#!#0 +1240#!#204#!#0#!#0#!#0#!#0#!#0 +1240#!#251#!#0#!#0#!#0#!#0#!#0 +1240#!#277#!#0#!#0#!#1#!#0#!#0 +1240#!#296#!#1#!#0#!#0#!#0#!#0 +1241#!#243#!#0#!#0#!#0#!#0#!#0 +1241#!#277#!#0#!#0#!#0#!#1#!#0 +1241#!#299#!#0#!#0#!#0#!#0#!#0 +1242#!#210#!#0#!#0#!#0#!#0#!#0 +1242#!#213#!#0#!#0#!#0#!#0#!#0 +1242#!#249#!#0#!#0#!#0#!#0#!#0 +1242#!#280#!#0#!#0#!#1#!#0#!#0 +1242#!#283#!#0#!#0#!#0#!#0#!#0 +1242#!#290#!#0#!#0#!#0#!#0#!#0 +1243#!#213#!#0#!#0#!#0#!#0#!#0 +1243#!#226#!#1#!#0#!#0#!#0#!#0 +1243#!#227#!#0#!#0#!#0#!#0#!#0 +1243#!#232#!#0#!#0#!#0#!#0#!#0 +1243#!#245#!#0#!#0#!#0#!#0#!#0 +1243#!#289#!#0#!#0#!#0#!#0#!#0 +1244#!#248#!#0#!#0#!#0#!#0#!#0 +1244#!#250#!#0#!#0#!#0#!#0#!#0 +1245#!#230#!#1#!#0#!#0#!#0#!#0 +1245#!#237#!#1#!#0#!#0#!#0#!#0 +1245#!#251#!#0#!#0#!#1#!#0#!#0 +1245#!#260#!#0#!#0#!#0#!#0#!#0 +1245#!#289#!#0#!#0#!#0#!#0#!#0 +1245#!#292#!#0#!#0#!#0#!#0#!#0 +1246#!#216#!#0#!#1#!#0#!#0#!#0 +1246#!#259#!#0#!#1#!#0#!#0#!#0 +1246#!#271#!#0#!#0#!#0#!#0#!#0 +1246#!#279#!#0#!#0#!#0#!#0#!#0 +1246#!#286#!#0#!#0#!#0#!#0#!#0 +1246#!#296#!#0#!#0#!#0#!#0#!#0 +1247#!#207#!#0#!#0#!#0#!#0#!#0 +1247#!#231#!#0#!#0#!#0#!#0#!#0 +1247#!#255#!#0#!#1#!#0#!#0#!#0 +1247#!#284#!#0#!#0#!#0#!#0#!#0 +1248#!#297#!#0#!#0#!#0#!#1#!#0 +1249#!#256#!#0#!#0#!#0#!#0#!#0 +1249#!#261#!#0#!#0#!#0#!#0#!#0 +1250#!#203#!#0#!#0#!#0#!#0#!#0 +1250#!#205#!#0#!#0#!#0#!#0#!#1 +1250#!#229#!#0#!#0#!#0#!#0#!#0 +1250#!#232#!#0#!#0#!#0#!#0#!#1 +1250#!#287#!#0#!#0#!#0#!#1#!#0 +~~END~~ + + +-- VIEW OF TOP PIVOT +SELECT * FROM TOP_PIVOT ORDER BY 1 +GO +~~START~~ +int#!#int#!#int#!#int#!#int#!#int +1200#!#1#!#1#!#1#!#2#!#0 +1201#!#0#!#0#!#2#!#0#!#0 +1202#!#1#!#0#!#0#!#1#!#0 +1203#!#0#!#0#!#0#!#1#!#0 +1204#!#0#!#0#!#0#!#0#!#1 +~~END~~ + + +-- VIEW OF DISTINCT PIVOT +SELECT * FROM DISTINCT_PIVOT ORDER BY 1 +GO +~~START~~ +int#!#int#!#int#!#int#!#int#!#int +1200#!#1#!#1#!#1#!#2#!#0 +1201#!#0#!#0#!#2#!#0#!#0 +1202#!#1#!#0#!#0#!#1#!#0 +1203#!#0#!#0#!#0#!#1#!#0 +1204#!#0#!#0#!#0#!#0#!#1 +1205#!#0#!#0#!#0#!#0#!#1 +1206#!#0#!#0#!#1#!#0#!#1 +1207#!#0#!#0#!#1#!#0#!#1 +1208#!#0#!#0#!#1#!#2#!#0 +1209#!#1#!#0#!#0#!#0#!#0 +1210#!#1#!#1#!#0#!#0#!#0 +1211#!#1#!#0#!#1#!#0#!#0 +1212#!#2#!#1#!#0#!#0#!#1 +1213#!#0#!#0#!#0#!#0#!#0 +1214#!#0#!#0#!#2#!#0#!#1 +1215#!#2#!#0#!#0#!#0#!#0 +1216#!#0#!#0#!#0#!#0#!#0 +1217#!#0#!#2#!#0#!#0#!#0 +1218#!#0#!#1#!#0#!#0#!#0 +1219#!#0#!#0#!#0#!#0#!#0 +1220#!#0#!#0#!#0#!#1#!#0 +1221#!#0#!#0#!#0#!#2#!#1 +1222#!#0#!#0#!#0#!#0#!#0 +1223#!#0#!#0#!#0#!#0#!#0 +1224#!#0#!#0#!#0#!#0#!#0 +1225#!#0#!#0#!#0#!#0#!#0 +1226#!#3#!#1#!#1#!#0#!#0 +1227#!#0#!#1#!#0#!#0#!#0 +1228#!#0#!#0#!#1#!#0#!#0 +1229#!#0#!#1#!#1#!#0#!#1 +1231#!#1#!#1#!#0#!#0#!#1 +1232#!#0#!#0#!#0#!#0#!#0 +1233#!#0#!#0#!#0#!#0#!#0 +1234#!#0#!#0#!#1#!#0#!#0 +1235#!#0#!#0#!#0#!#1#!#0 +1236#!#1#!#1#!#0#!#0#!#0 +1237#!#0#!#2#!#0#!#0#!#0 +1238#!#1#!#1#!#0#!#0#!#2 +1239#!#0#!#2#!#0#!#1#!#0 +1240#!#1#!#0#!#1#!#0#!#0 +1241#!#0#!#0#!#0#!#1#!#0 +1242#!#0#!#0#!#1#!#0#!#0 +1243#!#1#!#0#!#0#!#0#!#0 +1244#!#0#!#0#!#0#!#0#!#0 +1245#!#2#!#0#!#1#!#0#!#0 +1246#!#0#!#2#!#0#!#0#!#0 +1247#!#0#!#1#!#0#!#0#!#0 +1248#!#0#!#0#!#0#!#1#!#0 +1249#!#0#!#0#!#0#!#0#!#0 +1250#!#0#!#0#!#0#!#1#!#2 +~~END~~ + + +-- VIEW OF UNION PIVOT +SELECT * FROM UNION_PIVOT ORDER BY 1 +GO +~~START~~ +int#!#int#!#int#!#int#!#int#!#int +200#!#0#!#0#!#2#!#0#!#0 +201#!#0#!#0#!#0#!#0#!#0 +202#!#1#!#0#!#0#!#0#!#0 +203#!#0#!#0#!#0#!#1#!#0 +204#!#0#!#1#!#0#!#0#!#1 +1200#!#1#!#1#!#1#!#2#!#0 +1201#!#0#!#0#!#2#!#0#!#0 +1202#!#1#!#0#!#0#!#1#!#0 +1203#!#0#!#0#!#0#!#1#!#0 +1204#!#0#!#0#!#0#!#0#!#1 +~~END~~ + + +-- VIEW OF PIVOT IN SUBQUERY +SELECT * FROM SUBQUERY_PIVOT ORDER BY 1 +GO +~~START~~ +int#!#int#!#int +1200#!#1#!#1 +1201#!#0#!#0 +1202#!#1#!#0 +~~END~~ + + +-- VIEW OF PIVOT FUNCTION +SELECT * FROM PIVOT_FUNCTION ORDER BY 1 +GO +~~START~~ +int#!#int#!#int +1200#!#1#!#1 +1201#!#0#!#0 +1202#!#1#!#0 +1203#!#0#!#0 +1204#!#0#!#0 +1205#!#0#!#0 +1206#!#0#!#0 +1207#!#0#!#0 +1208#!#0#!#0 +1209#!#1#!#0 +1210#!#1#!#1 +1211#!#1#!#0 +~~END~~ + + +-- VIEW OF CTE PIVOT SRC TABLE +SELECT * FROM CTE_VIEW1 ORDER BY 1 +GO +~~START~~ +varchar#!#money#!#money#!#money#!#money +Banana#!#31201.0000#!#15600.5000#!#51381.0000#!#25690.5000 +Orange#!#17025.0000#!#8512.5000#!#77659.0000#!#38829.5000 +~~END~~ + + +-- VIEW OF CTE PIVOT SRC TABLE JOIN ANOTHER PIVOT +SELECT * FROM CTE_VIEW2 ORDER BY 1 +GO +~~START~~ +varchar#!#money#!#money#!#money#!#money +Banana#!#31201.0000#!#15600.5000#!#51381.0000#!#25690.5000 +Orange#!#17025.0000#!#8512.5000#!#77659.0000#!#38829.5000 +~~END~~ + + +-- VIEW OF CTE TABLE PIVOT SRC TABLE +SELECT * FROM CTE_VIEW3 ORDER BY 1 +GO +~~START~~ +varchar#!#int#!#int#!#int +COUNT#!#7#!#4#!#1 +~~END~~ + + +-- VIEW OF JOIN PIVOT +SELECT * FROM JOIN_PIVOT1 ORDER BY 1 +GO +~~START~~ +int#!#int#!#int#!#int#!#int#!#int +251#!#1#!#1#!#251#!#0#!#0 +252#!#0#!#0#!#252#!#1#!#0 +253#!#1#!#0#!#253#!#1#!#0 +254#!#0#!#1#!#254#!#0#!#0 +255#!#0#!#1#!#255#!#0#!#2 +~~END~~ + + +-- VIEW OF INNER JOIN PIVOT +SELECT * FROM JOIN_PIVOT2 ORDER BY 1 +GO +~~START~~ +int#!#int#!#int#!#int#!#int#!#int +251#!#1#!#1#!#251#!#0#!#0 +252#!#0#!#0#!#252#!#1#!#0 +253#!#1#!#0#!#253#!#1#!#0 +254#!#0#!#1#!#254#!#0#!#0 +255#!#0#!#1#!#255#!#0#!#2 +~~END~~ + + +-- VIEW OF LEFT JOIN PIVOT +SELECT * FROM JOIN_PIVOT3 ORDER BY 1 +GO +~~START~~ +int#!#int#!#int#!#int#!#int#!#int +251#!#1#!#1#!#251#!#0#!#0 +252#!#0#!#0#!#252#!#1#!#0 +253#!#1#!#0#!#253#!#1#!#0 +254#!#0#!#1#!#254#!#0#!#0 +255#!#0#!#1#!#255#!#0#!#2 +256#!#1#!#0#!##!##!# +258#!#0#!#0#!##!##!# +259#!#0#!#1#!##!##!# +260#!#0#!#0#!##!##!# +261#!#0#!#0#!##!##!# +~~END~~ + + +-- VIEW OF RIGHT JOIN PIVOT +SELECT * FROM JOIN_PIVOT4 ORDER BY 1 +GO +~~START~~ +int#!#int#!#int#!#int#!#int#!#int +#!##!##!#246#!#0#!#1 +#!##!##!#247#!#0#!#0 +#!##!##!#248#!#0#!#0 +#!##!##!#249#!#1#!#1 +#!##!##!#250#!#0#!#0 +251#!#1#!#1#!#251#!#0#!#0 +252#!#0#!#0#!#252#!#1#!#0 +253#!#1#!#0#!#253#!#1#!#0 +254#!#0#!#1#!#254#!#0#!#0 +255#!#0#!#1#!#255#!#0#!#2 +~~END~~ + + +-- VIEW OF FULL JOIN PIVOT +SELECT * FROM JOIN_PIVOT5 ORDER BY 1 +GO +~~START~~ +int#!#int#!#int#!#int#!#int#!#int +#!##!##!#250#!#0#!#0 +#!##!##!#247#!#0#!#0 +#!##!##!#246#!#0#!#1 +#!##!##!#248#!#0#!#0 +#!##!##!#249#!#1#!#1 +251#!#1#!#1#!#251#!#0#!#0 +252#!#0#!#0#!#252#!#1#!#0 +253#!#1#!#0#!#253#!#1#!#0 +254#!#0#!#1#!#254#!#0#!#0 +255#!#0#!#1#!#255#!#0#!#2 +256#!#1#!#0#!##!##!# +258#!#0#!#0#!##!##!# +259#!#0#!#1#!##!##!# +260#!#0#!#0#!##!##!# +261#!#0#!#0#!##!##!# +~~END~~ + + +-- VIEW OF CROSS JOIN PIVOT +SELECT * FROM JOIN_PIVOT6 ORDER BY 1 +GO +~~START~~ +int#!#int#!#int#!#int +251#!#1#!#246#!#0 +251#!#1#!#247#!#0 +251#!#1#!#248#!#1 +251#!#1#!#249#!#0 +251#!#1#!#250#!#1 +252#!#0#!#246#!#0 +252#!#0#!#247#!#0 +252#!#0#!#248#!#1 +252#!#0#!#249#!#0 +252#!#0#!#250#!#1 +253#!#1#!#246#!#0 +253#!#1#!#247#!#0 +253#!#1#!#248#!#1 +253#!#1#!#249#!#0 +253#!#1#!#250#!#1 +254#!#0#!#246#!#0 +254#!#0#!#247#!#0 +254#!#0#!#248#!#1 +254#!#0#!#249#!#0 +254#!#0#!#250#!#1 +255#!#0#!#246#!#0 +255#!#0#!#247#!#0 +255#!#0#!#248#!#1 +255#!#0#!#249#!#0 +255#!#0#!#250#!#1 +~~END~~ + + +-- VIEW OF COMMA JOIN PIVOT +SELECT * FROM JOIN_PIVOT7 ORDER BY 1 +GO +~~START~~ +int#!#int#!#int#!#int#!#int#!#int +251#!#1#!#1#!#251#!#0#!#0 +252#!#0#!#0#!#252#!#1#!#0 +253#!#1#!#0#!#253#!#1#!#0 +254#!#0#!#1#!#254#!#0#!#0 +255#!#0#!#1#!#255#!#0#!#2 +~~END~~ + + +-- VIEW OF COMMA CROSS JOIN PIVOT +SELECT * FROM JOIN_PIVOT8 ORDER BY 1 +GO +~~START~~ +int#!#int#!#int#!#int +251#!#1#!#246#!#0 +251#!#1#!#247#!#0 +251#!#1#!#248#!#1 +251#!#1#!#249#!#0 +251#!#1#!#250#!#1 +252#!#0#!#246#!#0 +252#!#0#!#247#!#0 +252#!#0#!#248#!#1 +252#!#0#!#249#!#0 +252#!#0#!#250#!#1 +253#!#1#!#246#!#0 +253#!#1#!#247#!#0 +253#!#1#!#248#!#1 +253#!#1#!#249#!#0 +253#!#1#!#250#!#1 +254#!#0#!#246#!#0 +254#!#0#!#247#!#0 +254#!#0#!#248#!#1 +254#!#0#!#249#!#0 +254#!#0#!#250#!#1 +255#!#0#!#246#!#0 +255#!#0#!#247#!#0 +255#!#0#!#248#!#1 +255#!#0#!#249#!#0 +255#!#0#!#250#!#1 +~~END~~ + diff --git a/test/JDBC/input/pivot-before-15_8-or-16_4-vu-cleanup.sql b/test/JDBC/input/pivot-before-15_8-or-16_4-vu-cleanup.sql new file mode 100644 index 00000000000..6a397073ffd --- /dev/null +++ b/test/JDBC/input/pivot-before-15_8-or-16_4-vu-cleanup.sql @@ -0,0 +1,54 @@ +use pivot_test; +GO + +drop trigger pivot_trigger +GO + +drop table trigger_testing +GO + +drop table OSTable; +GO + +drop table STable; +GO + +drop table seating_tbl; +GO + +drop view StoreReceipt_view; +GO + +drop table pivot_insert_into; +GO + +drop table pivot_select_into; +GO + +drop procedure top_n_pivot; +GO + +drop function test_table_valued_function; +GO + +drop table StoreReceipt; +GO + +drop table orders; +GO + +drop table products; +GO + +drop table pivot_schema.products_sch; +GO + +drop schema pivot_schema; +GO + +use master; +GO + +drop database pivot_test; +GO + diff --git a/test/JDBC/input/pivot-before-15_8-or-16_4-vu-prepare.sql b/test/JDBC/input/pivot-before-15_8-or-16_4-vu-prepare.sql new file mode 100644 index 00000000000..c524102a6d1 --- /dev/null +++ b/test/JDBC/input/pivot-before-15_8-or-16_4-vu-prepare.sql @@ -0,0 +1,404 @@ +create database pivot_test +GO + +use pivot_test +GO + +create table StoreReceipt ( + OrderID INT, + ItemID INT, + Price DECIMAL(6,2), + EmployeeID INT, + StoreID INT, + ManufactureID INT, + PurchaseDate DATE +); +GO + + +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (1, 2006, 485.14, 252, 7, 1209, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (2, 2146, 681.23, 296, 9, 1234, '2023-10-27'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (3, 2074, 960.42, 251, 4, 1245, '2023-10-27'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (4, 2005, 830.57, 220, 9, 1203, '2023-10-26'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (5, 2050, 649.41, 203, 5, 1200, '2023-10-30'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (6, 2082, 695.76, 269, 2, 1200, '2023-10-25'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (7, 2145, 766.23, 256, 9, 1249, '2023-10-28'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (8, 2085, 146.58, 201, 8, 1240, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (9, 2127, 819.74, 288, 5, 1202, '2023-10-25'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (10, 2036, 803.59, 270, 9, 1208, '2023-10-28'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (11, 2138, 704.37, 223, 5, 1208, '2023-10-26'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (12, 2016, 949.56, 287, 5, 1250, '2023-10-25'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (13, 2114, 187.16, 222, 5, 1200, '2023-10-27'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (14, 2081, 545.96, 269, 3, 1217, '2023-10-25'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (15, 2084, 843.16, 247, 9, 1218, '2023-10-28'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (16, 2004, 152.79, 251, 1, 1240, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (17, 2100, 313.51, 232, 8, 1201, '2023-10-27'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (18, 2001, 34.63, 211, 10, 1232, '2023-10-27'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (19, 2072, 76.61, 247, 9, 1228, '2023-10-27'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (20, 2069, 878.9, 209, 7, 1227, '2023-10-27'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (21, 2074, 124.01, 200, 4, 1226, '2023-10-30'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (22, 2061, 429.58, 204, 3, 1212, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (23, 2027, 709.99, 300, 6, 1238, '2023-10-30'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (24, 2056, 267.88, 202, 2, 1226, '2023-10-25'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (25, 2031, 271.77, 248, 4, 1228, '2023-10-28'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (26, 2080, 397.51, 220, 10, 1200, '2023-10-27'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (27, 2006, 525.4, 207, 8, 1247, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (28, 2010, 343.29, 276, 7, 1229, '2023-10-25'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (29, 2044, 808.24, 227, 1, 1216, '2023-10-27'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (30, 2073, 451.15, 228, 3, 1231, '2023-10-26'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (31, 2074, 808.82, 296, 9, 1214, '2023-10-28'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (32, 2018, 985.56, 221, 9, 1219, '2023-10-26'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (33, 2120, 18.1, 227, 10, 1243, '2023-10-26'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (34, 2094, 532.7, 234, 1, 1238, '2023-10-30'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (35, 2018, 675.61, 212, 4, 1211, '2023-10-28'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (36, 2052, 286.88, 201, 1, 1205, '2023-10-25'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (37, 2079, 351.51, 264, 1, 1217, '2023-10-25'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (38, 2089, 834.46, 264, 3, 1200, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (39, 2111, 564.39, 288, 9, 1213, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (40, 2045, 332.85, 278, 8, 1214, '2023-10-25'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (41, 2139, 814.19, 288, 5, 1220, '2023-10-30'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (42, 2106, 645.39, 218, 4, 1207, '2023-10-25'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (43, 2082, 185.88, 230, 9, 1234, '2023-10-28'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (44, 2078, 235.07, 232, 6, 1250, '2023-10-28'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (45, 2077, 307.92, 297, 5, 1248, '2023-10-30'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (46, 2021, 606.12, 262, 1, 1203, '2023-10-26'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (47, 2028, 622.14, 296, 7, 1246, '2023-10-25'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (48, 2092, 2.41, 224, 10, 1225, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (49, 2142, 447.79, 260, 7, 1245, '2023-10-26'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (50, 2006, 970.28, 272, 8, 1202, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (51, 2078, 459.75, 274, 9, 1221, '2023-10-27'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (52, 2128, 376.82, 294, 8, 1215, '2023-10-30'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (53, 2059, 357.59, 219, 2, 1211, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (54, 2058, 535.53, 271, 8, 1246, '2023-10-25'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (55, 2127, 661.96, 227, 1, 1219, '2023-10-27'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (56, 2053, 885.07, 275, 7, 1233, '2023-10-25'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (57, 2094, 55.32, 238, 4, 1208, '2023-10-25'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (58, 2055, 420.27, 264, 2, 1238, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (59, 2117, 306.36, 222, 4, 1234, '2023-10-27'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (60, 2077, 504.6, 266, 4, 1200, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (61, 2120, 279.1, 292, 2, 1226, '2023-10-26'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (62, 2113, 904.88, 299, 1, 1241, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (63, 2051, 496.42, 249, 7, 1203, '2023-10-25'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (64, 2136, 508.71, 262, 3, 1236, '2023-10-26'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (65, 2144, 421.24, 286, 9, 1236, '2023-10-25'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (66, 2119, 236.49, 277, 5, 1241, '2023-10-27'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (67, 2030, 215.66, 216, 3, 1246, '2023-10-26'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (68, 2024, 243.15, 245, 9, 1243, '2023-10-27'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (69, 2073, 397.63, 255, 8, 1235, '2023-10-30'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (70, 2079, 163.06, 229, 4, 1201, '2023-10-25'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (71, 2070, 550.83, 289, 7, 1214, '2023-10-25'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (72, 2069, 676.38, 278, 7, 1225, '2023-10-27'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (73, 2135, 778.12, 211, 10, 1214, '2023-10-27'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (74, 2127, 563.12, 258, 9, 1223, '2023-10-25'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (75, 2010, 502.25, 214, 7, 1218, '2023-10-30'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (76, 2050, 171.66, 271, 3, 1239, '2023-10-27'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (77, 2112, 364.88, 249, 2, 1215, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (78, 2090, 821.38, 269, 1, 1239, '2023-10-30'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (79, 2079, 19.88, 228, 1, 1202, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (80, 2047, 730.79, 255, 8, 1239, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (81, 2080, 664.81, 283, 10, 1215, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (82, 2137, 340.03, 236, 4, 1214, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (83, 2092, 4.28, 203, 10, 1218, '2023-10-26'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (84, 2003, 100.14, 253, 7, 1224, '2023-10-28'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (85, 2001, 952.61, 247, 2, 1212, '2023-10-28'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (86, 2054, 773.2, 210, 8, 1224, '2023-10-25'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (87, 2037, 65.9, 291, 6, 1214, '2023-10-26'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (88, 2092, 904.74, 224, 6, 1204, '2023-10-28'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (89, 2036, 485.19, 214, 10, 1203, '2023-10-26'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (90, 2148, 946.4, 211, 2, 1236, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (91, 2045, 703.15, 232, 7, 1204, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (92, 2093, 711.61, 200, 4, 1229, '2023-10-27'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (93, 2084, 103.15, 267, 2, 1209, '2023-10-25'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (94, 2049, 202.91, 289, 1, 1245, '2023-10-26'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (95, 2038, 760.1, 243, 8, 1241, '2023-10-28'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (96, 2026, 759.33, 253, 2, 1212, '2023-10-26'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (97, 2105, 125.73, 226, 10, 1218, '2023-10-30'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (98, 2011, 176.87, 294, 10, 1213, '2023-10-25'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (99, 2120, 501.65, 204, 9, 1240, '2023-10-28'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (100, 2138, 490.44, 232, 7, 1243, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (101, 2014, 346.61, 265, 9, 1215, '2023-10-27'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (102, 2062, 176.8, 285, 5, 1235, '2023-10-26'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (103, 2112, 113.92, 224, 8, 1229, '2023-10-28'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (104, 2073, 160.8, 267, 2, 1210, '2023-10-26'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (105, 2082, 588.15, 225, 3, 1229, '2023-10-26'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (106, 2138, 571.21, 213, 1, 1242, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (107, 2092, 814.36, 213, 9, 1243, '2023-10-30'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (108, 2089, 221.8, 220, 5, 1203, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (109, 2040, 501.46, 248, 10, 1244, '2023-10-25'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (110, 2096, 974.47, 204, 6, 1221, '2023-10-25'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (111, 2078, 914.56, 208, 3, 1239, '2023-10-25'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (112, 2118, 287.53, 215, 10, 1221, '2023-10-26'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (113, 2106, 415.27, 249, 8, 1242, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (114, 2145, 283.31, 227, 6, 1231, '2023-10-26'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (115, 2148, 950.09, 243, 10, 1211, '2023-10-27'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (116, 2137, 132.57, 269, 3, 1227, '2023-10-25'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (117, 2082, 440.25, 267, 9, 1204, '2023-10-30'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (118, 2015, 749.85, 229, 8, 1232, '2023-10-28'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (119, 2021, 209.93, 229, 9, 1250, '2023-10-28'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (120, 2006, 540.63, 283, 8, 1242, '2023-10-27'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (121, 2030, 197.56, 278, 9, 1215, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (122, 2123, 153.87, 259, 5, 1239, '2023-10-30'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (123, 2079, 444.55, 259, 1, 1200, '2023-10-30'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (124, 2146, 437.87, 231, 10, 1247, '2023-10-30'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (125, 2094, 74.57, 241, 8, 1237, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (126, 2084, 660.65, 251, 3, 1237, '2023-10-27'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (127, 2085, 366.69, 209, 3, 1238, '2023-10-26'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (128, 2031, 560.65, 254, 1, 1233, '2023-10-30'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (129, 2064, 410.85, 217, 5, 1208, '2023-10-25'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (130, 2095, 241.41, 289, 10, 1243, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (131, 2106, 163.57, 235, 9, 1218, '2023-10-27'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (132, 2128, 764.88, 291, 3, 1237, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (133, 2014, 936.97, 201, 10, 1218, '2023-10-26'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (134, 2141, 351.46, 287, 1, 1202, '2023-10-30'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (135, 2094, 277.08, 218, 1, 1211, '2023-10-27'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (136, 2064, 489.19, 251, 2, 1226, '2023-10-28'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (137, 2001, 190.54, 231, 7, 1222, '2023-10-26'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (138, 2007, 252.7, 290, 8, 1242, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (139, 2058, 413.1, 214, 3, 1226, '2023-10-28'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (140, 2140, 230.58, 227, 8, 1206, '2023-10-25'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (141, 2074, 940.96, 200, 8, 1200, '2023-10-28'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (142, 2071, 618.94, 203, 9, 1250, '2023-10-30'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (143, 2002, 115.65, 213, 4, 1201, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (144, 2010, 22.85, 254, 3, 1218, '2023-10-25'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (145, 2023, 901.21, 230, 2, 1245, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (146, 2139, 173.7, 246, 8, 1202, '2023-10-27'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (147, 2047, 848.18, 225, 5, 1221, '2023-10-28'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (148, 2084, 254.96, 250, 10, 1244, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (149, 2004, 298.15, 296, 10, 1231, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (150, 2009, 413.91, 292, 9, 1245, '2023-10-27'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (151, 2009, 664.17, 277, 4, 1240, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (152, 2049, 748.86, 205, 6, 1250, '2023-10-26'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (153, 2064, 935.97, 253, 9, 1218, '2023-10-25'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (154, 2129, 577.5, 290, 9, 1237, '2023-10-25'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (155, 2052, 496.99, 211, 2, 1215, '2023-10-28'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (156, 2144, 753.54, 270, 6, 1229, '2023-10-30'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (157, 2143, 644.8, 267, 7, 1201, '2023-10-27'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (158, 2131, 710.66, 292, 8, 1217, '2023-10-26'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (159, 2051, 336.83, 229, 9, 1229, '2023-10-26'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (160, 2031, 592.09, 248, 4, 1206, '2023-10-30'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (161, 2046, 129.18, 279, 10, 1207, '2023-10-27'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (162, 2101, 536.8, 282, 7, 1204, '2023-10-27'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (163, 2112, 960.31, 296, 2, 1240, '2023-10-28'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (164, 2100, 127.35, 235, 8, 1236, '2023-10-25'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (165, 2031, 352.12, 203, 9, 1208, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (166, 2035, 110.15, 243, 10, 1229, '2023-10-30'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (167, 2105, 531.13, 234, 7, 1220, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (168, 2046, 483.93, 279, 8, 1238, '2023-10-28'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (169, 2083, 669.86, 226, 2, 1243, '2023-10-28'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (170, 2040, 373.61, 208, 10, 1223, '2023-10-28'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (171, 2060, 355.5, 220, 10, 1200, '2023-10-28'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (172, 2120, 28.3, 284, 9, 1247, '2023-10-28'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (173, 2040, 357.99, 250, 6, 1212, '2023-10-26'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (174, 2103, 980.82, 288, 2, 1202, '2023-10-28'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (175, 2035, 813.47, 217, 1, 1235, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (176, 2110, 399.64, 285, 9, 1220, '2023-10-25'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (177, 2016, 44.06, 250, 6, 1207, '2023-10-30'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (178, 2096, 66.57, 292, 4, 1214, '2023-10-28'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (179, 2030, 33.38, 239, 10, 1215, '2023-10-30'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (180, 2073, 459.77, 240, 8, 1218, '2023-10-30'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (181, 2071, 875.42, 230, 3, 1217, '2023-10-26'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (182, 2041, 380.94, 255, 3, 1247, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (183, 2097, 914.44, 298, 3, 1210, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (184, 2105, 329.25, 210, 1, 1242, '2023-10-30'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (185, 2000, 457.91, 256, 2, 1231, '2023-10-28'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (186, 2098, 901.2, 261, 10, 1249, '2023-10-30'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (187, 2146, 236.33, 293, 10, 1223, '2023-10-28'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (188, 2117, 405.01, 279, 8, 1246, '2023-10-30'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (189, 2099, 272.14, 234, 6, 1205, '2023-10-25'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (190, 2145, 42.04, 299, 8, 1204, '2023-10-26'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (191, 2017, 399.9, 280, 4, 1242, '2023-10-28'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (192, 2058, 733.45, 277, 9, 1239, '2023-10-28'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (193, 2124, 809.67, 259, 3, 1246, '2023-10-25'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (194, 2059, 167.54, 221, 10, 1233, '2023-10-30'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (195, 2032, 441.79, 219, 6, 1238, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (196, 2101, 720.37, 286, 1, 1246, '2023-10-27'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (197, 2103, 820.5, 289, 6, 1206, '2023-10-28'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (198, 2010, 433.08, 276, 9, 1213, '2023-10-29'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (199, 2147, 779.36, 237, 2, 1245, '2023-10-25'); +insert into StoreReceipt (OrderID, ItemID, Price, EmployeeID, StoreID, ManufactureID, PurchaseDate) values (200, 2084, 735.91, 223, 5, 1221, '2023-10-30'); +GO + +CREATE TABLE orders ( + orderId INT PRIMARY KEY, + productId INT, + employeeName VARCHAR(4), + employeeCode VARBINARY(30), + date DATE); +GO + +CREATE TABLE products ( + productId int PRIMARY KEY, + productName VARCHAR(30), + productPrice INT +) + +INSERT INTO products VALUES + (1, 'mac', 250000), + (2, 'iphone', 80000), + (3, 'airpods', 20000), + (4, 'charger', 2900), + (5, 'ipad', 50000) +GO + +INSERT INTO orders VALUES + (101, 5,'empA', 0x656D7041, '2024-05-01'), + (102, 3,'empA', 0x656D7041, '2024-05-01'), + (103, 1,'empA', 0x656D7041, '2024-05-01'), + (104, 2,'empA', 0x656D7041, '2024-05-01'), + (105, 1,'empB', 0x656D7042, '2024-05-01'), + (106, 2,'empB', 0x656D7042, '2024-05-01'), + (110, 3,'empB', 0x656D7042, '2024-05-01'), + (109, 4,'empB', 0x656D7042, '2024-05-01'), + (108, 5,'empB', 0x656D7042, '2024-05-01'), + (107, 5,'empB', 0x656D7042, '2024-05-01'), + (111, 1,'empC', 0x656D7043, '2024-05-01'), + (113, 1,'empC', 0x656D7043, '2024-05-01'), + (115, 1,'empC', 0x656D7043, '2024-05-01'), + (119, 1,'empC', 0x656D7043, '2024-05-01'), + (201, 2,'empC', 0x656D7043, '2024-05-01'), + (223, 2,'empC', 0x656D7043, '2024-05-01'), + (224, 5,'empD', 0x656D7044, '2024-05-01'), + (202, 3,'empD', 0x656D7044, '2024-05-01'), + (190, 1,'empD', 0x656D7044, '2024-05-01'); +GO + +create schema pivot_schema; +GO + +CREATE TABLE pivot_schema.products_sch ( + productId int PRIMARY KEY, + productName VARCHAR(30), + productPrice INT +) +GO + +INSERT INTO pivot_schema.products_sch VALUES + (1, 'mac', 250000), + (2, 'iphone', 80000), + (3, 'airpods', 20000), + (4, 'charger', 2900), + (5, 'ipad', 50000) +GO + +create table pivot_insert_into(ManufactureID int, EmployeeID int, p1 int, p2 int, p3 int, p4 int, p5 int); +GO + +CREATE PROCEDURE top_n_pivot + ( + @Number int = 5 + ) +AS +BEGIN + SELECT TOP(@Number) ManufactureID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 + FROM + ( + SELECT ManufactureID, ItemID, StoreID + FROM StoreReceipt + )as srctable + PIVOT ( + COUNT (ItemID) + FOR StoreID in ([2], [3], [4], [5], [6]) + ) AS pvt2 + ORDER BY 1 +END; +GO + +CREATE FUNCTION test_table_valued_function(@Number int) +RETURNS TABLE +AS +RETURN + SELECT TOP(@Number) ManufactureID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 + FROM + ( + SELECT ManufactureID, ItemID, StoreID + FROM StoreReceipt + )as srctable + PIVOT ( + COUNT (ItemID) + FOR StoreID in ([2], [3], [4], [5], [6]) + ) AS pvt2 + ORDER BY 1 +GO + +CREATE VIEW StoreReceipt_view +AS +SELECT * FROM StoreReceipt; +GO + +-- BABEL-4558 +CREATE TABLE OSTable( + [Oid] [int] NOT NULL, + [Sid] [int] NOT NULL +) +GO + + +CREATE TABLE STable( + [Id] [int] IDENTITY(1,1) NOT NULL, + [Scode] [varchar](10) NOT NULL, + [Type] [smallint] NOT NULL +) +GO + +insert into OSTable (Oid, Sid) values (1, 2); +insert into OSTable (Oid, Sid) values (2, 8); +insert into OSTable (Oid, Sid) values (3, 5); +insert into OSTable (Oid, Sid) values (4, 11); +insert into OSTable (Oid, Sid) values (5, 12); +insert into OSTable (Oid, Sid) values (6, 8); +insert into OSTable (Oid, Sid) values (7, 5); +insert into OSTable (Oid, Sid) values (8, 2); +insert into OSTable (Oid, Sid) values (9, 15); +insert into OSTable (Oid, Sid) values (10, 1); +GO + +insert into STable (Scode, Type) values ('vestibulum', 11); +insert into STable (Scode, Type) values ('eget', 15); +insert into STable (Scode, Type) values ('pharetra', 13); +insert into STable (Scode, Type) values ('nam', 15); +insert into STable (Scode, Type) values ('fermentum', 13); +insert into STable (Scode, Type) values ('hac', 12); +insert into STable (Scode, Type) values ('molestie', 10); +insert into STable (Scode, Type) values ('justo', 11); +insert into STable (Scode, Type) values ('lobortis', 7); +insert into STable (Scode, Type) values ('at', 3); +insert into STable (Scode, Type) values ('augue', 9); +insert into STable (Scode, Type) values ('luctus', 2); +insert into STable (Scode, Type) values ('nisi', 9); +insert into STable (Scode, Type) values ('sociis', 1); +insert into STable (Scode, Type) values ('ultrices', 14); +GO + +-- table for aggregate with string value +CREATE TABLE seating_tbl ( + seatings VARCHAR(20) NOT NULL, + left_right VARCHAR(20) NOT NULL +); +GO + +INSERT INTO seating_tbl (seatings, left_right) +VALUES ('SEAT1', 'LEFT'), + ('SEAT1', 'RIGHT'), + ('SEAT2', 'LEFT'), + ('SEAT3', 'LEFT'), + ('SEAT3', 'RIGHT'); +GO + +create table trigger_testing(col nvarchar(60)) +GO + +create trigger pivot_trigger on trigger_testing after insert +as +begin + SELECT 'OrderNumbers' AS OrderCountbyStore, [1] AS STORE1, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5 + FROM + ( + SELECT StoreID, OrderID + FROM StoreReceipt + )AS SrcTable + PIVOT ( + COUNT (OrderID) + FOR StoreID IN ([1], [2], [3],[4], [5]) + ) AS pvt +end +GO \ No newline at end of file diff --git a/test/JDBC/input/pivot-before-15_8-or-16_4-vu-verify.sql b/test/JDBC/input/pivot-before-15_8-or-16_4-vu-verify.sql new file mode 100644 index 00000000000..d493a04db17 --- /dev/null +++ b/test/JDBC/input/pivot-before-15_8-or-16_4-vu-verify.sql @@ -0,0 +1,904 @@ +use pivot_test +GO + +-- 2 column in src table pivot +SELECT 'OrderNumbers' AS OrderCountbyStore, [1] AS STORE1, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5 +FROM +( + SELECT StoreID, OrderID + FROM StoreReceipt +)AS SrcTable +PIVOT ( + COUNT (OrderID) + FOR StoreID IN ([1], [2], [3],[4], [5]) +) AS pvt +ORDER BY 1 +GO + +-- testing trigger with pivot +insert into trigger_testing (col) select N'Muffler' +GO + +-- 3 column in src table pivot +SELECT EmployeeID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 +FROM +( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt +)AS srctable +PIVOT ( + COUNT (ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) +) AS pvt +ORDER BY 1 +GO + +-- 3+ column IN src table pivot +SELECT ManufactureID, EmployeeID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 +FROM +( + SELECT ManufactureID, EmployeeID, ItemID, StoreID + FROM StoreReceipt +)AS srctable +PIVOT ( + COUNT (ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) +) AS pvt +ORDER BY 1 +GO + +-- ORDER by test +SELECT ManufactureID, EmployeeID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 +FROM +( + SELECT ManufactureID, EmployeeID, ItemID, StoreID + FROM StoreReceipt +)AS srctable +PIVOT ( + COUNT (ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) +) AS pvt +ORDER by EmployeeID +GO + +-- whereclause test +SELECT ManufactureID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 +FROM +( + SELECT ManufactureID, ItemID, StoreID + FROM StoreReceipt +)AS srctable +PIVOT ( + COUNT (ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) +) AS pvt +WHERE ManufactureID < 1220 +ORDER BY 1 +GO + +-- groupby, having clause test +SELECT EmployeeID, ManufactureID, [2] AS STORE2 +FROM +( + SELECT EmployeeID, ManufactureID, ItemID, StoreID + FROM StoreReceipt +)AS srctable +PIVOT ( + COUNT (ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) +) AS pvt +WHERE EmployeeID < 210 +group by EmployeeID, ManufactureID, [2] +having ManufactureID < 1250 +ORDER by 1,2 +GO + + +-- TOP test +SELECT TOP 5 ManufactureID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 +FROM +( + SELECT ManufactureID, ItemID, StoreID + FROM StoreReceipt +)AS srctable +PIVOT ( + COUNT (ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) +) AS pvt +ORDER BY 1 +GO + +-- distinct test +SELECT distinct ManufactureID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 +FROM +( + SELECT ManufactureID, ItemID, StoreID + FROM StoreReceipt +)AS srctable +PIVOT ( + COUNT (ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) +) AS pvt +ORDER BY 1 +GO + +-- INSERT INTO test +INSERT INTO pivot_insert_into +SELECT ManufactureID, EmployeeID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 +FROM +( + SELECT ManufactureID, EmployeeID, ItemID, StoreID + FROM StoreReceipt +)AS srctable +PIVOT ( + COUNT (ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) +) AS pvt +ORDER BY 1 + +SELECT TOP 10 * FROM pivot_insert_into ORDER by 1, 2; +GO + +-- SELECT INTO test +SELECT ManufactureID, EmployeeID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 +INTO pivot_SELECT_into +FROM +( + SELECT ManufactureID, EmployeeID, ItemID, StoreID + FROM StoreReceipt +)AS srctable +PIVOT ( + COUNT (ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) +) AS pvt +ORDER BY 1 + +SELECT TOP 10 * FROM pivot_SELECT_into ORDER by 1, 2; +GO + +-- union test +SELECT TOP 5 EmployeeID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 +FROM +( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt +)AS srctable +PIVOT ( + COUNT (ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) +) AS pvt +UNION +SELECT TOP 5 ManufactureID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 +FROM +( + SELECT ManufactureID, ItemID, StoreID + FROM StoreReceipt +)AS srctable +PIVOT ( + COUNT (ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) +) AS pvt2 +ORDER by 1 +GO + +-- sub query test +SELECT TOP 3 * FROM ( + SELECT ManufactureID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 + FROM + ( + SELECT ManufactureID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) + ) AS pvt2 +) p +ORDER BY 1 +GO + +-- table variable test +DECLARE @pivot_table_var TABLE ( + ManufactureID INT, + ItemID INT, + StoreID INT +); +INSERT INTO @pivot_table_var SELECT ManufactureID, ItemID, StoreID FROM StoreReceipt; +SELECT TOP 5 ManufactureID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 +FROM + @pivot_table_var +PIVOT ( + COUNT (ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) +) AS pvt2 +ORDER BY 1 +GO + +-- temp table test +SELECT ManufactureID, ItemID, StoreID INTO #pivot_temp_table FROM StoreReceipt; +SELECT TOP 5 ManufactureID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 +FROM + #pivot_temp_table +PIVOT ( + COUNT (ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) +) AS pvt2 +ORDER BY 1 +GO + +-- procedure test +exec top_n_pivot 10 +GO + +exec top_n_pivot 5 +GO + +-- function test +SELECT * FROM test_table_valued_function(12) ORDER BY 1 +GO + +SELECT * FROM test_table_valued_function(2) ORDER BY 1 +GO + +-- explain pivot +SET BABELFISH_SHOWPLAN_ALL ON; +SELECT TOP 5 ManufactureID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 +FROM +( + SELECT ManufactureID, ItemID, StoreID + FROM StoreReceipt +)AS srctable +PIVOT ( + COUNT (ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) +) AS pvt +ORDER BY 1 +GO +SET BABELFISH_SHOWPLAN_ALL OFF; +GO + + +-- test column name with indirection (value column) +SELECT TOP 5 ManufactureID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 +FROM +( + SELECT ManufactureID, ItemID, StoreID + FROM StoreReceipt +)AS srctable +PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) +) AS pvt +ORDER BY 1 +GO + +-- test column name win indirection (category column) +SELECT TOP 5 ManufactureID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 +FROM +( + SELECT ManufactureID, ItemID, StoreID + FROM StoreReceipt +)AS srctable +PIVOT ( + COUNT (ItemID) + FOR srctable.StoreID IN ([2], [3], [4], [5], [6]) +) AS pvt +ORDER BY 1 +GO + +-- CTE test data +CREATE TABLE #FruitSales +(FruitType VARCHAR(20), SalesYear INT, FruitSales MONEY); +GO + +INSERT INTO #FruitSales VALUES('Orange', 2024, 23425); +INSERT INTO #FruitSales VALUES('Orange', 2024, 54234); +INSERT INTO #FruitSales VALUES('Orange', 2023, 12490); +INSERT INTO #FruitSales VALUES('Orange', 2023, 4535); +INSERT INTO #FruitSales VALUES('Banana', 2024, 45745); +INSERT INTO #FruitSales VALUES('Banana', 2024, 5636); +INSERT INTO #FruitSales VALUES('Banana', 2023, 24654); +INSERT INTO #FruitSales VALUES('Banana', 2023, 6547); +GO + +-- CTE test 1 +WITH +SalesTotal AS +( +SELECT FruitType, + [2023] AS [2023_Total], + [2024] AS [2024_Total] +FROM #FruitSales + PIVOT(SUM(FruitSales) + FOR SalesYear IN([2023], [2024]) + ) AS PivotSales +), +SalesAvg AS +( +SELECT FruitType, + [2023] AS [2023_Avg], + [2024] AS [2024_Avg] +FROM #FruitSales + PIVOT(AVG(FruitSales) + FOR SalesYear IN([2023], [2024]) + ) AS PivotSales +) +SELECT st.FruitType, st.[2023_Total], sa.[2023_Avg], + st.[2024_Total], sa.[2024_Avg] +FROM SalesTotal AS st + INNER JOIN SalesAvg AS sa + ON st.FruitType = sa.FruitType +ORDER BY 1 +GO + +-- CTE test 2 +WITH +SalesTotal AS +( +SELECT FruitType, + [2023] AS [2023_Total], + [2024] AS [2024_Total] +FROM #FruitSales + PIVOT(SUM(FruitSales) + FOR SalesYear IN([2023], [2024]) + ) AS PivotSales +), +SalesAvg AS +( +SELECT FruitType, + [2023] AS [2023_Avg], + [2024] AS [2024_Avg] +FROM #FruitSales + PIVOT(AVG(FruitSales) + FOR SalesYear IN([2023], [2024]) + ) AS PivotSales +) +SELECT * from SalesTotal ORDER BY FruitType; +GO + +-- CTE test 3 +WITH +SalesTotal AS +( +SELECT FruitType, + [2023] AS [2023_Total], + [2024] AS [2024_Total] +FROM #FruitSales + PIVOT(SUM(FruitSales) + FOR SalesYear IN([2023], [2024]) + ) AS PivotSales +), +SalesAvg AS +( +SELECT FruitType, + [2023] AS [2023_Avg], + [2024] AS [2024_Avg] +FROM #FruitSales + PIVOT(AVG(FruitSales) + FOR SalesYear IN([2023], [2024]) + ) AS PivotSales +) +SELECT * from SalesAvg ORDER BY FruitType; +GO + +-- CTE of 3 expression table +WITH +SalesTotal AS +( +SELECT FruitType, + [2023] AS [2023_Total], + [2024] AS [2024_Total] +FROM #FruitSales + PIVOT(SUM(FruitSales) + FOR SalesYear IN([2023], [2024]) + ) AS PivotSales +), +SalesAvg AS +( +SELECT FruitType, + [2023] AS [2023_Avg], + [2024] AS [2024_Avg] +FROM #FruitSales + PIVOT(AVG(FruitSales) + FOR SalesYear IN([2023], [2024]) + ) AS PivotSales +), +SalesMin AS +( +SELECT FruitType, + [2023] AS [2023_min], + [2024] AS [2024_min] +FROM #FruitSales + PIVOT(MIN(FruitSales) + FOR SalesYear IN([2023], [2024]) + ) AS PivotSales +) +SELECT st.FruitType, st.[2023_Total], sa.[2023_Avg], + st.[2024_Total], sa.[2024_Avg], sm.[2023_min],sm.[2024_min] +FROM SalesTotal AS st + INNER JOIN SalesAvg AS sa + ON st.FruitType = sa.FruitType + INNER JOIN SalesMin as sm + ON sa.FruitType = sm.FruitType +ORDER BY 1 +GO + +-- Test stmt of CTE table and PIVOT stmt in different level +WITH +SalesTotal AS +( + SELECT FruitType, + [2023] AS [2023_Total], + [2024] AS [2024_Total] + FROM #FruitSales + PIVOT(SUM(FruitSales) + FOR SalesYear IN([2023], [2024]) + ) AS PivotSales +) +SELECT st.FruitType, st.[2023_Total], sa.[2023_Avg], + st.[2024_Total], sa.[2024_Avg] +FROM SalesTotal AS st +JOIN ( + SELECT FruitType, + [2023] AS [2023_Avg], + [2024] AS [2024_Avg] + FROM #FruitSales + PIVOT(AVG(FruitSales) + FOR SalesYear IN([2023], [2024]) + ) AS PivotSales +) sa ON st.FruitType = sa.FruitType; +GO + +DROP TABlE IF EXISTS #FruitSales +GO + +-- PIVOT with CTE as source table +WITH cte_table AS ( + SELECT [p].productName, [o].[employeeName] + FROM orders [o] JOIN products AS [p] on (o.productId = p.productId) +) +SELECT CAST('COUNT' AS VARCHAR(10)), [mac],[ipad],[charger] FROM cte_table +PIVOT ( + COUNT(employeeName) + FOR productName IN (mac, [iphone], [ipad], [charger]) +) as pvt +GO + +-- string is not allowed in PIVOT column value list +WITH cte_table AS ( + SELECT o.[orderId], o.[productId], [p].productName, + [p].productPrice, [o].[employeeName], [o].employeeCode, [o].date + FROM orders [o] JOIN products AS [p] on (o.productId = p.productId) +) +SELECT * FROM cte_table +PIVOT ( + COUNT(orderId) + FOR productName IN ('mac', 'iphone', 'ipad', 'charger') +) as p +GO + +-- aggregate column in PIVOT column value list is not allowed +WITH cte_table AS +( + SELECT + CAST('COUNT' AS VARCHAR(10)) AS COUNT, + [mac], [ipad], [charger], [employeeName] + FROM ( + SELECT [o].employeeName, [p].productName + FROM orders [o] JOIN products AS [p] on ([o].productId = [p].productId) + ) AS dervied_table +PIVOT + ( + COUNT(employeeName) + FOR productName IN ([mac], [employeeName], [iphone], [ipad], [charger]) + ) as pvt +) +SELECT * FROM cte_table +GO + +-- Join stmts inside PIVOT statment (BABEL-4558) +SELECT Oid, [1] AS TYPE1, [2] AS TYPE2, [3] AS TYPE3 +FROM (SELECT OSTable.Oid, STable.Scode, STable.Type + FROM OSTable + INNER JOIN STable + ON OSTable.Sid = STable.Id + ) AS SourceTable +PIVOT ( MAX(Scode) FOR [Type] IN ([1], [2], [3])) + AS os_pivot +ORDER BY 1 +GO + +-- JOIN TEST +SELECT * FROM +( + SELECT TOP 10 EmployeeID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 + FROM + ( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) + ) AS pvt where EmployeeID > 250 +) AS p1 +JOIN +( + SELECT TOP 10 EmployeeID, [7] AS STORE7, [8] AS STORE8, [9] AS STORE9, [10] AS STORE10 + FROM + ( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([7], [8], [9], [10]) + ) AS pvt where EmployeeID > 245 +) AS p2 +ON p1.EmployeeID = p2.EmployeeID ORDER BY 1 +GO + +-- INNER JOIN TEST +SELECT * FROM +( + SELECT TOP 10 EmployeeID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 + FROM + ( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) + ) AS pvt where EmployeeID > 250 +) AS p1 +INNER JOIN +( + SELECT TOP 10 EmployeeID, [7] AS STORE7, [8] AS STORE8, [9] AS STORE9, [10] AS STORE10 + FROM + ( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([7], [8], [9], [10]) + ) AS pvt where EmployeeID > 245 +) AS p2 +ON p1.EmployeeID = p2.EmployeeID ORDER BY 1 +GO + +-- LEFT JOIN TEST +SELECT * FROM +( + SELECT TOP 10 EmployeeID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 + FROM + ( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) + ) AS pvt where EmployeeID > 250 +) AS p1 +LEFT JOIN +( + SELECT TOP 10 EmployeeID, [7] AS STORE7, [8] AS STORE8, [9] AS STORE9, [10] AS STORE10 + FROM + ( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([7], [8], [9], [10]) + ) AS pvt where EmployeeID > 245 +) AS p2 +ON p1.EmployeeID = p2.EmployeeID ORDER BY 1 +GO + +-- RIGHT JOIN TEST +SELECT * FROM +( + SELECT TOP 10 EmployeeID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 + FROM + ( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) + ) AS pvt where EmployeeID > 250 +) AS p1 +RIGHT JOIN +( + SELECT TOP 10 EmployeeID, [7] AS STORE7, [8] AS STORE8, [9] AS STORE9, [10] AS STORE10 + FROM + ( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([7], [8], [9], [10]) + ) AS pvt where EmployeeID > 245 +) AS p2 +ON p1.EmployeeID = p2.EmployeeID ORDER BY 1 +GO + +-- FULL JOIN TEST +SELECT * FROM +( + SELECT TOP 10 EmployeeID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 + FROM + ( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) + ) AS pvt where EmployeeID > 250 +) AS p1 +FULL JOIN +( + SELECT TOP 10 EmployeeID, [7] AS STORE7, [8] AS STORE8, [9] AS STORE9, [10] AS STORE10 + FROM + ( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([7], [8], [9], [10]) + ) AS pvt where EmployeeID > 245 +) AS p2 +ON p1.EmployeeID = p2.EmployeeID ORDER BY 1 +GO + +-- CROSS JOIN TEST +SELECT * FROM +( + SELECT TOP 5 EmployeeID, [2] AS STORE2 + FROM + ( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) + ) AS pvt where EmployeeID > 250 +) AS p1 +CROSS JOIN +( + SELECT TOP 5 EmployeeID, [10] AS STORE10 + FROM + ( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([7], [8], [9], [10]) + ) AS pvt where EmployeeID > 245 +) AS p2 ORDER BY 1 +GO + +-- COMMA JOIN TEST +SELECT * FROM +( + SELECT TOP 10 EmployeeID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 + FROM + ( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) + ) AS pvt where EmployeeID > 250 +) AS p1 +, +( + SELECT TOP 10 EmployeeID, [7] AS STORE7, [8] AS STORE8, [9] AS STORE9, [10] AS STORE10 + FROM + ( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([7], [8], [9], [10]) + ) AS pvt where EmployeeID > 245 +) AS p2 +WHERE p1.EmployeeID = p2.EmployeeID ORDER BY 1 +GO + +-- COMMA CROSS JOIN TEST +SELECT * FROM +( + SELECT TOP 5 EmployeeID, [2] AS STORE2 + FROM + ( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) + ) AS pvt where EmployeeID > 250 +) AS p1 +, +( + SELECT TOP 5 EmployeeID, [10] AS STORE10 + FROM + ( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([7], [8], [9], [10]) + ) AS pvt where EmployeeID > 245 +) AS p2 ORDER BY 1 +GO + +--3+ JOIN TEST +SELECT * FROM +( + SELECT TOP 10 EmployeeID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 + FROM + ( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) + ) AS pvt where EmployeeID > 250 +) AS p1 +JOIN +( + SELECT TOP 10 EmployeeID, [7] AS STORE7, [8] AS STORE8, [9] AS STORE9, [10] AS STORE10 + FROM + ( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([7], [8], [9], [10]) + ) AS pvt where EmployeeID > 245 +) AS p2 +ON p1.EmployeeID = p2.EmployeeID +JOIN +( + SELECT TOP 10 EmployeeID, [1] AS STORE1 + FROM + ( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([1]) + ) AS pvt where EmployeeID > 248 +)AS p3 +ON p2.EmployeeID = p3.EmployeeID ORDER BY 1 +GO + + +-- Result Order Test +-- JOIN A +SELECT p2.EmployeeID, STORE7, STORE8, STORE9, STORE10 FROM +( + SELECT TOP 10 EmployeeID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 + FROM + ( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) + ) AS pvt where EmployeeID > 250 +) AS p1 +JOIN +( + SELECT TOP 10 EmployeeID, [7] AS STORE7, [8] AS STORE8, [9] AS STORE9, [10] AS STORE10 + FROM + ( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([7], [8], [9], [10]) + ) AS pvt where EmployeeID > 245 +) AS p2 +ON p1.EmployeeID = p2.EmployeeID ORDER BY 1 +GO + +-- JOIN B (Reference) +SELECT * FROM +( + SELECT TOP 10 EmployeeID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 + FROM + ( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) + ) AS pvt where EmployeeID > 250 +) AS p1 +JOIN +( + SELECT TOP 10 EmployeeID, [7] AS STORE7, [8] AS STORE8, [9] AS STORE9, [10] AS STORE10 + FROM + ( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([7], [8], [9], [10]) + ) AS pvt where EmployeeID > 245 +) AS p2 +ON p1.EmployeeID = p2.EmployeeID ORDER BY 1 +GO + +-- Test view as a data source in a stmt with pivot operator +SELECT TOP 5 EmployeeID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 +FROM +( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt_view +)AS srctable +PIVOT ( + COUNT (ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) +) AS pvt +GO + +-- aggregate string value, when no row is selected, should output NULL +SELECT [seatings], [LEFT], [RIGHT] +FROM +( + SELECT [seatings], left_right + FROM seating_tbl +) AS p1 +PIVOT ( + MAX(left_right) + FOR left_right IN ([LEFT], [RIGHT]) +) AS p2 +ORDER BY 1 +GO + +-- test pivot with table in different schemas 1 +SELECT CAST('COUNT' AS VARCHAR(10)) AS COUNT, [mac], [ipad], [charger] +FROM ( + SELECT [o].employeeName, [p].productName + FROM dbo.orders [o] JOIN products AS [p] on ([o].productId = [p].productId) +) AS dervied_table +PIVOT( + COUNT(employeeName) + FOR productName IN ([mac], [iphone], [ipad], [charger]) +) as pvt +GO + +-- test pivot with table in different schemas 2 +SELECT CAST('COUNT' AS VARCHAR(10)) AS COUNT, [mac], [ipad], [charger] +FROM ( + SELECT [o].employeeName, [p].productName + FROM dbo.orders [o] JOIN pivot_schema.products_sch AS [p] on ([o].productId = [p].productId) +) AS dervied_table +PIVOT( + COUNT(employeeName) + FOR productName IN ([mac], [iphone], [ipad], [charger]) +) as pvt +GO \ No newline at end of file diff --git a/test/JDBC/input/pivot-vu-cleanup.sql b/test/JDBC/input/pivot-vu-cleanup.sql index dac520866f3..7d69068a3b2 100644 --- a/test/JDBC/input/pivot-vu-cleanup.sql +++ b/test/JDBC/input/pivot-vu-cleanup.sql @@ -1,57 +1,117 @@ -use pivot_test; +USE pivot_test; GO -drop trigger pivot_trigger +DROP VIEW PIVOT_VIEW2 GO -drop table trigger_testing +DROP VIEW PIVOT_VIEW3 GO -drop table OSTable; +DROP VIEW PIVOT_VIEW4 GO -drop table STable; +DROP VIEW TOP_PIVOT GO -drop table seating_tbl; +DROP VIEW DISTINCT_PIVOT GO -drop view StoreReceipt_view; +DROP VIEW UNION_PIVOT GO -drop view pivot_view; +DROP VIEW SUBQUERY_PIVOT GO -drop table pivot_insert_into; +DROP VIEW PIVOT_FUNCTION GO -drop table pivot_select_into; +DROP VIEW CTE_VIEW1 GO -drop procedure top_n_pivot; +DROP VIEW CTE_VIEW2 GO -drop function test_table_valued_function; +DROP VIEW CTE_VIEW3 GO -drop table StoreReceipt; +DROP VIEW JOIN_PIVOT1 GO -drop table orders; +DROP VIEW JOIN_PIVOT2 GO -drop table products; +DROP VIEW JOIN_PIVOT3 GO -drop table pivot_schema.products_sch; +DROP VIEW JOIN_PIVOT4 GO -drop schema pivot_schema; +DROP VIEW JOIN_PIVOT5 GO -use master; +DROP VIEW JOIN_PIVOT6 GO -drop database pivot_test; +DROP VIEW JOIN_PIVOT7 GO +DROP VIEW JOIN_PIVOT8 +GO + +DROP TRIGGER pivot_trigger +GO + +DROP TABLE trigger_testing +GO + +DROP TABLE OSTable; +GO + +DROP TABLE STable; +GO + +DROP TABLE seating_tbl; +GO + +DROP VIEW StoreReceipt_view; +GO + +DROP TABLE pivot_insert_into; +GO + +DROP TABLE pivot_select_into; +GO + +DROP PROCEDURE top_n_pivot; +GO + +DROP FUNCTION test_table_valued_function; +GO + +DROP TABLE FruitSalesTable +GO + +DROP TABLE StoreReceipt; +GO + +DROP TABLE orders; +GO + +DROP TABLE products; +GO + +DROP TABLE pivot_schema.products_sch; +GO + +DROP SCHEMA pivot_schema; +GO + +USE master; +GO + +DROP DATABASE pivot_test; +GO + +--- Check for inconsistent metadata +SELECT COUNT(*) FROM sys.babelfish_inconsistent_metadata() +GO \ No newline at end of file diff --git a/test/JDBC/input/pivot-vu-prepare.sql b/test/JDBC/input/pivot-vu-prepare.sql index 57d70d06af6..3af0d2fe8f8 100644 --- a/test/JDBC/input/pivot-vu-prepare.sql +++ b/test/JDBC/input/pivot-vu-prepare.sql @@ -325,25 +325,7 @@ AS SELECT * FROM StoreReceipt; GO --- Test create view for stmt with pivot operator --- Expected to fail --- Create view with pivot is not yet supported -CREATE VIEW pivot_view -AS -SELECT TOP(5) ManufactureID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 -FROM -( - SELECT ManufactureID, ItemID, StoreID - FROM StoreReceipt -)as srctable -PIVOT ( - COUNT (ItemID) - FOR StoreID in ([2], [3], [4], [5], [6]) -) AS pvt -ORDER BY 1 -GO - --- BABEL-4558 +-- BABEL-4558 CREATE TABLE OSTable( [Oid] [int] NOT NULL, [Sid] [int] NOT NULL @@ -419,4 +401,479 @@ begin FOR StoreID IN ([1], [2], [3],[4], [5]) ) AS pvt end +GO + +CREATE TABLE FruitSalesTable +(FruitType VARCHAR(20), SalesYear INT, FruitSales MONEY); +GO + +INSERT INTO FruitSalesTable VALUES('Orange', 2024, 23425); +INSERT INTO FruitSalesTable VALUES('Orange', 2024, 54234); +INSERT INTO FruitSalesTable VALUES('Orange', 2023, 12490); +INSERT INTO FruitSalesTable VALUES('Orange', 2023, 4535); +INSERT INTO FruitSalesTable VALUES('Banana', 2024, 45745); +INSERT INTO FruitSalesTable VALUES('Banana', 2024, 5636); +INSERT INTO FruitSalesTable VALUES('Banana', 2023, 24654); +INSERT INTO FruitSalesTable VALUES('Banana', 2023, 6547); +GO + +-- PIVOT VIEW TESTS +-- VIEW OF 2 COLUMN SRC TABLE PIVOT +CREATE VIEW PIVOT_VIEW2 +AS +SELECT 'OrderNumbers' AS OrderCountbyStore, [1] AS STORE1, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5 +FROM +( + SELECT StoreID, OrderID + FROM StoreReceipt +)AS SrcTable +PIVOT ( + COUNT (OrderID) + FOR StoreID IN ([1], [2], [3],[4], [5]) +) AS pvt +GO + +-- VIEW OF 3 COLUMN SRC TABLE PIVOT +CREATE VIEW PIVOT_VIEW3 +AS +SELECT TOP(5) ManufactureID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 +FROM +( + SELECT ManufactureID, ItemID, StoreID + FROM StoreReceipt +)as srctable +PIVOT ( + COUNT (ItemID) + FOR StoreID in ([2], [3], [4], [5], [6]) +) AS pvt +ORDER BY 1 +GO + +-- VIEW OF 4 COLUMN SRC TABLE PIVOT +CREATE VIEW PIVOT_VIEW4 +AS +SELECT ManufactureID, EmployeeID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 +FROM +( + SELECT ManufactureID, EmployeeID, ItemID, StoreID + FROM StoreReceipt +)AS srctable +PIVOT ( + COUNT (ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) +) AS pvt +GO + +-- VIEW OF TOP PIVOT +CREATE VIEW TOP_PIVOT +AS +SELECT TOP 5 ManufactureID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 +FROM +( + SELECT ManufactureID, ItemID, StoreID + FROM StoreReceipt +)AS srctable +PIVOT ( + COUNT (ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) +) AS pvt +ORDER BY 1 +GO + +-- VIEW OF DISTINCT PIVOT +CREATE VIEW DISTINCT_PIVOT +AS +SELECT distinct ManufactureID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 +FROM +( + SELECT ManufactureID, ItemID, StoreID + FROM StoreReceipt +)AS srctable +PIVOT ( + COUNT (ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) +) AS pvt +GO + +-- VIEW OF UNION PIVOT +CREATE VIEW UNION_PIVOT +AS +SELECT TOP 5 EmployeeID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 +FROM +( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt +)AS srctable +PIVOT ( + COUNT (ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) +) AS pvt +UNION +SELECT TOP 5 ManufactureID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 +FROM +( + SELECT ManufactureID, ItemID, StoreID + FROM StoreReceipt +)AS srctable +PIVOT ( + COUNT (ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) +) AS pvt2 +ORDER by 1 +GO + +-- VIEW OF PIVOT IN SUBQUERY +CREATE VIEW SUBQUERY_PIVOT +AS +SELECT TOP 3 ManufactureID, STORE2, STORE3 FROM ( + SELECT ManufactureID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 + FROM + ( + SELECT ManufactureID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) + ) AS pvt2 +) p +ORDER BY 1 +GO + +-- VIEW OF PIVOT FUNCTION +CREATE VIEW PIVOT_FUNCTION +AS +SELECT ManufactureID, STORE2, STORE3 FROM test_table_valued_function(12) +GO + +-- VIEW OF CTE PIVOT AS SRC TABLE +CREATE VIEW CTE_VIEW1 +AS +WITH +SalesTotal AS +( +SELECT FruitType, + [2023] AS [2023_Total], + [2024] AS [2024_Total] +FROM FruitSalesTable + PIVOT(SUM(FruitSales) + FOR SalesYear IN([2023], [2024]) + ) AS PivotSales +), +SalesAvg AS +( +SELECT FruitType, + [2023] AS [2023_Avg], + [2024] AS [2024_Avg] +FROM FruitSalesTable + PIVOT(AVG(FruitSales) + FOR SalesYear IN([2023], [2024]) + ) AS PivotSales +) +SELECT st.FruitType, st.[2023_Total], sa.[2023_Avg], + st.[2024_Total], sa.[2024_Avg] +FROM SalesTotal AS st + INNER JOIN SalesAvg AS sa + ON st.FruitType = sa.FruitType +ORDER BY 1 +GO + +-- VIEW OF CTE PIVOT AS SRC TABLE JOIN ANOTHER PIVOT +CREATE VIEW CTE_VIEW2 +AS +WITH +SalesTotal AS +( + SELECT FruitType, + [2023] AS [2023_Total], + [2024] AS [2024_Total] + FROM FruitSalesTable + PIVOT(SUM(FruitSales) + FOR SalesYear IN([2023], [2024]) + ) AS PivotSales +) +SELECT st.FruitType, st.[2023_Total], sa.[2023_Avg], + st.[2024_Total], sa.[2024_Avg] +FROM SalesTotal AS st +JOIN ( + SELECT FruitType, + [2023] AS [2023_Avg], + [2024] AS [2024_Avg] + FROM FruitSalesTable + PIVOT(AVG(FruitSales) + FOR SalesYear IN([2023], [2024]) + ) AS PivotSales +) sa ON st.FruitType = sa.FruitType; +GO + +-- VIEW OF CTE TABLE AS PIVOT SRC TABLE +CREATE VIEW CTE_VIEW3 +AS +WITH cte_table AS ( + SELECT [p].productName, [o].[employeeName] + FROM orders [o] JOIN products AS [p] on (o.productId = p.productId) +) +SELECT CAST('COUNT' AS VARCHAR(10)), [mac],[ipad],[charger] FROM cte_table +PIVOT ( + COUNT(employeeName) + FOR productName IN (mac, [iphone], [ipad], [charger]) +) as pvt +GO + +-- VIEW OF JOIN PIVOT +CREATE VIEW JOIN_PIVOT1 +AS +SELECT p1.EmployeeID as p1_EmployeeID, STORE2, STORE3, p2.EmployeeID as p2_EmployeeID, STORE7, STORE8 FROM +( + SELECT TOP 10 EmployeeID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 + FROM + ( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) + ) AS pvt where EmployeeID > 250 +) AS p1 +JOIN +( + SELECT TOP 10 EmployeeID, [7] AS STORE7, [8] AS STORE8, [9] AS STORE9, [10] AS STORE10 + FROM + ( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([7], [8], [9], [10]) + ) AS pvt where EmployeeID > 245 +) AS p2 +ON p1.EmployeeID = p2.EmployeeID +GO + +-- VIEW OF INNER JOIN PIVOT +CREATE VIEW JOIN_PIVOT2 +AS +SELECT p1.EmployeeID as p1_EmployeeID, STORE2, STORE3, p2.EmployeeID as p2_EmployeeID, STORE7, STORE8 FROM +( + SELECT TOP 10 EmployeeID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 + FROM + ( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) + ) AS pvt where EmployeeID > 250 +) AS p1 +INNER JOIN +( + SELECT TOP 10 EmployeeID, [7] AS STORE7, [8] AS STORE8, [9] AS STORE9, [10] AS STORE10 + FROM + ( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([7], [8], [9], [10]) + ) AS pvt where EmployeeID > 245 +) AS p2 +ON p1.EmployeeID = p2.EmployeeID +GO + +-- VIEW OF LEFT JOIN PIVOT +CREATE VIEW JOIN_PIVOT3 +AS +SELECT p1.EmployeeID as p1_EmployeeID, STORE2, STORE3, p2.EmployeeID as p2_EmployeeID, STORE7, STORE8 FROM +( + SELECT TOP 10 EmployeeID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 + FROM + ( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) + ) AS pvt where EmployeeID > 250 +) AS p1 +LEFT JOIN +( + SELECT TOP 10 EmployeeID, [7] AS STORE7, [8] AS STORE8, [9] AS STORE9, [10] AS STORE10 + FROM + ( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([7], [8], [9], [10]) + ) AS pvt where EmployeeID > 245 +) AS p2 +ON p1.EmployeeID = p2.EmployeeID +GO + +-- VIEW OF RIGHT JOIN PIVOT +CREATE VIEW JOIN_PIVOT4 +AS +SELECT p1.EmployeeID as p1_EmployeeID, STORE2, STORE3, p2.EmployeeID as p2_EmployeeID, STORE7, STORE8 FROM +( + SELECT TOP 10 EmployeeID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 + FROM + ( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) + ) AS pvt where EmployeeID > 250 +) AS p1 +RIGHT JOIN +( + SELECT TOP 10 EmployeeID, [7] AS STORE7, [8] AS STORE8, [9] AS STORE9, [10] AS STORE10 + FROM + ( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([7], [8], [9], [10]) + ) AS pvt where EmployeeID > 245 +) AS p2 +ON p1.EmployeeID = p2.EmployeeID +GO + +-- VIEW OF FULL JOIN PIVOT +CREATE VIEW JOIN_PIVOT5 +AS +SELECT p1.EmployeeID as p1_EmployeeID, STORE2, STORE3, p2.EmployeeID as p2_EmployeeID, STORE7, STORE8 FROM +( + SELECT TOP 10 EmployeeID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 + FROM + ( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) + ) AS pvt where EmployeeID > 250 +) AS p1 +FULL JOIN +( + SELECT TOP 10 EmployeeID, [7] AS STORE7, [8] AS STORE8, [9] AS STORE9, [10] AS STORE10 + FROM + ( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([7], [8], [9], [10]) + ) AS pvt where EmployeeID > 245 +) AS p2 +ON p1.EmployeeID = p2.EmployeeID +GO + +-- VIEW OF CROSS JOIN PIVOT +CREATE VIEW JOIN_PIVOT6 +AS +SELECT p1.EmployeeID as p1_EmployeeID, STORE2, p2.EmployeeID as p2_EmployeeID, STORE10 FROM +( + SELECT TOP 5 EmployeeID, [2] AS STORE2 + FROM + ( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) + ) AS pvt where EmployeeID > 250 +) AS p1 +CROSS JOIN +( + SELECT TOP 5 EmployeeID, [10] AS STORE10 + FROM + ( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([7], [8], [9], [10]) + ) AS pvt where EmployeeID > 245 +) AS p2 +GO + +-- VIEW OF COMMA JOIN PIVOT +CREATE VIEW JOIN_PIVOT7 +AS +SELECT p1.EmployeeID as p1_EmployeeID, STORE2, STORE3, p2.EmployeeID as p2_EmployeeID, STORE7, STORE8 FROM +( + SELECT TOP 10 EmployeeID, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5, [6] AS STORE6 + FROM + ( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) + ) AS pvt where EmployeeID > 250 +) AS p1 +, +( + SELECT TOP 10 EmployeeID, [7] AS STORE7, [8] AS STORE8, [9] AS STORE9, [10] AS STORE10 + FROM + ( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([7], [8], [9], [10]) + ) AS pvt where EmployeeID > 245 +) AS p2 +WHERE p1.EmployeeID = p2.EmployeeID +GO + +-- VIEW OF COMMA CROSS JOIN PIVOT +CREATE VIEW JOIN_PIVOT8 +AS +SELECT p1.EmployeeID as p1_EmployeeID, STORE2, p2.EmployeeID as p2_EmployeeID, STORE10 FROM +( + SELECT TOP 5 EmployeeID, [2] AS STORE2 + FROM + ( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([2], [3], [4], [5], [6]) + ) AS pvt where EmployeeID > 250 +) AS p1 +, +( + SELECT TOP 5 EmployeeID, [10] AS STORE10 + FROM + ( + SELECT EmployeeID, ItemID, StoreID + FROM StoreReceipt + )AS srctable + PIVOT ( + COUNT (srctable.ItemID) + FOR StoreID IN ([7], [8], [9], [10]) + ) AS pvt where EmployeeID > 245 +) AS p2 +GO + +--- Check for inconsistent metadata before upgrade +SELECT COUNT(*) FROM sys.babelfish_inconsistent_metadata(); GO \ No newline at end of file diff --git a/test/JDBC/input/pivot-vu-verify.sql b/test/JDBC/input/pivot-vu-verify.sql index 0a4a0b5d8fa..5936aa5da2f 100644 --- a/test/JDBC/input/pivot-vu-verify.sql +++ b/test/JDBC/input/pivot-vu-verify.sql @@ -1,6 +1,10 @@ use pivot_test GO +--- Check for inconsistent metadata after upgrade +SELECT COUNT(*) FROM sys.babelfish_inconsistent_metadata(); +GO + -- 2 column in src table pivot SELECT 'OrderNumbers' AS OrderCountbyStore, [1] AS STORE1, [2] AS STORE2, [3] AS STORE3, [4] AS STORE4, [5] AS STORE5 FROM @@ -865,14 +869,6 @@ PIVOT ( ) AS pvt GO --- Test view of a stmt with pivot operator --- Expected to fail since we failed to create view with pivot at prepare script. --- Create view with pivot is not yet supported, -SELECT ManufactureID, STORE2, STORE3, STORE4, STORE5, STORE6 -FROM pivot_view -ORDER BY ManufactureID -GO - -- aggregate string value, when no row is selected, should output NULL SELECT [seatings], [LEFT], [RIGHT] FROM @@ -909,4 +905,81 @@ PIVOT( COUNT(employeeName) FOR productName IN ([mac], [iphone], [ipad], [charger]) ) as pvt +GO + +--PIVOT VIEW TESTS +-- VIEW WITH 2 COLUMN SRC TABLE PIVOT +SELECT * FROM PIVOT_VIEW2 ORDER BY 1 +GO + +-- VIEW WITH 3 COLUMN SRC TABLE PIVOT +SELECT * FROM PIVOT_VIEW3 ORDER BY 1 +GO + +-- VIEW OF 4 COLUMN SRC TABLE PIVOT +SELECT * FROM PIVOT_VIEW4 ORDER BY 1 +GO + +-- VIEW OF TOP PIVOT +SELECT * FROM TOP_PIVOT ORDER BY 1 +GO + +-- VIEW OF DISTINCT PIVOT +SELECT * FROM DISTINCT_PIVOT ORDER BY 1 +GO + +-- VIEW OF UNION PIVOT +SELECT * FROM UNION_PIVOT ORDER BY 1 +GO + +-- VIEW OF PIVOT IN SUBQUERY +SELECT * FROM SUBQUERY_PIVOT ORDER BY 1 +GO + +-- VIEW OF PIVOT FUNCTION +SELECT * FROM PIVOT_FUNCTION ORDER BY 1 +GO + +-- VIEW OF CTE PIVOT SRC TABLE +SELECT * FROM CTE_VIEW1 ORDER BY 1 +GO + +-- VIEW OF CTE PIVOT SRC TABLE JOIN ANOTHER PIVOT +SELECT * FROM CTE_VIEW2 ORDER BY 1 +GO + +-- VIEW OF CTE TABLE PIVOT SRC TABLE +SELECT * FROM CTE_VIEW3 ORDER BY 1 +GO + +-- VIEW OF JOIN PIVOT +SELECT * FROM JOIN_PIVOT1 ORDER BY 1 +GO + +-- VIEW OF INNER JOIN PIVOT +SELECT * FROM JOIN_PIVOT2 ORDER BY 1 +GO + +-- VIEW OF LEFT JOIN PIVOT +SELECT * FROM JOIN_PIVOT3 ORDER BY 1 +GO + +-- VIEW OF RIGHT JOIN PIVOT +SELECT * FROM JOIN_PIVOT4 ORDER BY 1 +GO + +-- VIEW OF FULL JOIN PIVOT +SELECT * FROM JOIN_PIVOT5 ORDER BY 1 +GO + +-- VIEW OF CROSS JOIN PIVOT +SELECT * FROM JOIN_PIVOT6 ORDER BY 1 +GO + +-- VIEW OF COMMA JOIN PIVOT +SELECT * FROM JOIN_PIVOT7 ORDER BY 1 +GO + +-- VIEW OF COMMA CROSS JOIN PIVOT +SELECT * FROM JOIN_PIVOT8 ORDER BY 1 GO \ No newline at end of file diff --git a/test/JDBC/upgrade/15_5/schedule b/test/JDBC/upgrade/15_5/schedule index 6469d779523..75d801e2ca3 100644 --- a/test/JDBC/upgrade/15_5/schedule +++ b/test/JDBC/upgrade/15_5/schedule @@ -483,7 +483,7 @@ sys_certificates sys_database_permissions BABEL-4279 BABEL-4484 -pivot +pivot-before-15_8-or-16_4 #AUTO_ANALYZE #uncomment this test when preparing for new minor version cast_eliminate order_by_offset_fetch_rows-before-15_6-or-16_2 diff --git a/test/JDBC/upgrade/15_6/schedule b/test/JDBC/upgrade/15_6/schedule index 46ade8313b0..a2bc3b16f0a 100644 --- a/test/JDBC/upgrade/15_6/schedule +++ b/test/JDBC/upgrade/15_6/schedule @@ -499,7 +499,7 @@ sys_certificates sys_database_permissions BABEL-4279 BABEL-4484 -pivot +pivot-before-15_8-or-16_4 #AUTO_ANALYZE #uncomment this test when preparing for new minor version cast_eliminate TestDatatypeAggSort diff --git a/test/JDBC/upgrade/15_7/schedule b/test/JDBC/upgrade/15_7/schedule index 1c8fae9d7a1..675e671ac87 100644 --- a/test/JDBC/upgrade/15_7/schedule +++ b/test/JDBC/upgrade/15_7/schedule @@ -500,7 +500,7 @@ sys_certificates sys_database_permissions BABEL-4279 BABEL-4484 -pivot +pivot-before-15_8-or-16_4 #AUTO_ANALYZE #uncomment this test when preparing for new minor version cast_eliminate TestDatatypeAggSort diff --git a/test/python/expected/sql_validation_framework/expected_create.out b/test/python/expected/sql_validation_framework/expected_create.out index aa18184d0c3..f0884f13e9c 100644 --- a/test/python/expected/sql_validation_framework/expected_create.out +++ b/test/python/expected/sql_validation_framework/expected_create.out @@ -70,6 +70,7 @@ Could not find tests for procedure sys.printarg Could not find tests for procedure sys.sp_cursor_list Could not find tests for procedure sys.sp_describe_cursor Could not find tests for table sys.babelfish_helpcollation +Could not find tests for table sys.babelfish_pivot_view Could not find tests for table sys.babelfish_syslanguages Could not find tests for table sys.service_settings Could not find tests for table sys.spt_datatype_info_table @@ -191,6 +192,7 @@ Could not find upgrade tests for procedure sys.sp_unprepare Could not find upgrade tests for procedure sys.sp_updatestats Could not find upgrade tests for table sys.babelfish_configurations Could not find upgrade tests for table sys.babelfish_helpcollation +Could not find upgrade tests for table sys.babelfish_pivot_view Could not find upgrade tests for table sys.babelfish_syslanguages Could not find upgrade tests for table sys.service_settings Could not find upgrade tests for table sys.spt_datatype_info_table diff --git a/test/python/expected/upgrade_validation/expected_dependency.out b/test/python/expected/upgrade_validation/expected_dependency.out index 5d7295a0702..2a74b0e635e 100644 --- a/test/python/expected/upgrade_validation/expected_dependency.out +++ b/test/python/expected/upgrade_validation/expected_dependency.out @@ -222,7 +222,6 @@ Function sys.bbf_get_context_info() Function sys.bbf_get_current_physical_schema_name(text) Function sys.bbf_get_immediate_base_type_of_udt(oid) Function sys.bbf_is_shared_schema(text) -Function sys.bbf_pivot() Function sys.bbf_varbinary(sys.geography) Function sys.bbf_varbinary(sys.geometry) Function sys.bbf_varbinary_binary_cmp(sys.bbf_varbinary,sys.bbf_binary)