From 9af0593bd1366d94659d4a9bf04231384e5396f1 Mon Sep 17 00:00:00 2001 From: Yanjie Xu Date: Tue, 24 Sep 2024 11:58:25 +0000 Subject: [PATCH] Support View usage for PIVOT operator Support create/select/drop view on stmt with pivot operator We switched to deparsed psql-text approach for VIEW/JOIN/CTE, all pivot metadata will be passed to bbf_pivot function arguments, so we removed all funcCall/funcExpr context field related code. Task: BABEL-4673 Signed-off-by: Yanjie Xu --- contrib/babelfishpg_tsql/runtime/functions.c | 137 +- .../babelfishpg_tsql/sql/sys_functions.sql | 2 +- .../babelfishpg_tsql--4.3.0--4.4.0.sql | 7 + contrib/babelfishpg_tsql/src/hooks.c | 100 +- .../src/tsqlUnsupportedFeatureHandler.cpp | 15 +- .../pivot-before-15_9-or-16_5-vu-cleanup.out | 54 + .../pivot-before-15_9-or-16_5-vu-prepare.out | 862 ++++++++ .../pivot-before-15_9-or-16_5-vu-verify.out | 1938 +++++++++++++++++ test/JDBC/expected/pivot-vu-cleanup.out | 100 +- test/JDBC/expected/pivot-vu-prepare.out | 531 ++++- test/JDBC/expected/pivot-vu-verify.out | 564 ++++- .../pivot-before-15_9-or-16_5-vu-cleanup.sql | 54 + .../pivot-before-15_9-or-16_5-vu-prepare.sql | 404 ++++ .../pivot-before-15_9-or-16_5-vu-verify.sql | 904 ++++++++ test/JDBC/input/pivot-vu-cleanup.sql | 100 +- test/JDBC/input/pivot-vu-prepare.sql | 513 ++++- test/JDBC/input/pivot-vu-verify.sql | 91 +- test/JDBC/upgrade/15_5/schedule | 2 +- test/JDBC/upgrade/15_6/schedule | 2 +- test/JDBC/upgrade/15_7/schedule | 2 +- test/JDBC/upgrade/15_8/schedule | 2 +- test/JDBC/upgrade/16_1/schedule | 2 +- test/JDBC/upgrade/16_2/schedule | 2 +- test/JDBC/upgrade/16_3/schedule | 2 +- test/JDBC/upgrade/16_4/schedule | 2 +- .../expected_dependency.out | 1 - 26 files changed, 6103 insertions(+), 290 deletions(-) create mode 100644 test/JDBC/expected/pivot-before-15_9-or-16_5-vu-cleanup.out create mode 100644 test/JDBC/expected/pivot-before-15_9-or-16_5-vu-prepare.out create mode 100644 test/JDBC/expected/pivot-before-15_9-or-16_5-vu-verify.out create mode 100644 test/JDBC/input/pivot-before-15_9-or-16_5-vu-cleanup.sql create mode 100644 test/JDBC/input/pivot-before-15_9-or-16_5-vu-prepare.sql create mode 100644 test/JDBC/input/pivot-before-15_9-or-16_5-vu-verify.sql diff --git a/contrib/babelfishpg_tsql/runtime/functions.c b/contrib/babelfishpg_tsql/runtime/functions.c index 7fca673c2fe..ac90df07868 100644 --- a/contrib/babelfishpg_tsql/runtime/functions.c +++ b/contrib/babelfishpg_tsql/runtime/functions.c @@ -204,14 +204,12 @@ void *get_language(void); 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 Tuplestorestate *get_bbf_pivot_tuplestore(RawStmt *sql, - const char *sourcetext, - const char *funcName, - HTAB *bbf_pivot_hash, - TupleDesc tupdesc, - bool randomAccess); +static HTAB *load_categories_hash(const char *sourcetext, MemoryContext per_query_ctx); +static Tuplestorestate *get_bbf_pivot_tuplestore(const char *sourcetext, + const char *funcName, + HTAB *bbf_pivot_hash, + TupleDesc tupdesc, + bool randomAccess); extern bool canCommitTransaction(void); extern bool is_ms_shipped(char *object_name, int type, Oid schema_id); @@ -4581,68 +4579,6 @@ objectproperty_internal(PG_FUNCTION_ARGS) PG_RETURN_NULL(); } -/* -* We transformed tsql pivot stmt to 3 parsetree. The outer parsetree is a wrapper stmt -* while the other two are helper stmts. Since postgres does not natively support execute -* raw parsetree, and we can only get raw parsetree after the analyzer, we created this -* SPI function to help execute raw parsetree. -*/ -int -SPI_execute_raw_parsetree(RawStmt *parsetree, const char * sourcetext, bool read_only, long tcount) -{ - _SPI_plan plan; - int ret = 0; - List *plancache_list; - CachedPlanSource *plansource; - int prev_sql_dialect; - - if (parsetree == NULL || tcount < 0) - return SPI_ERROR_ARGUMENT; - - /* - * set sql_dialect to tsql, which is needed for raw parsetree parsing - * and processing - */ - prev_sql_dialect = sql_dialect; - sql_dialect = SQL_DIALECT_TSQL; - - memset(&plan, 0, sizeof(_SPI_plan)); - plan.magic = _SPI_PLAN_MAGIC; - plan.parse_mode = RAW_PARSE_DEFAULT; - plan.cursor_options = CURSOR_OPT_PARALLEL_OK; - - /* - * Construct plancache entries, but don't do parse analysis yet. - */ - plancache_list = NIL; - - /* - * there are some parsetree node copied from the orginial parsetree, - * and the node's location is fixed with the sourcetext. So we are - * passing the orginal sourcetext to the following function to prevent - * analyzing error. - */ - plansource = CreateOneShotCachedPlan(parsetree, - sourcetext, - CreateCommandTag(parsetree->stmt)); - - plancache_list = lappend(plancache_list, plansource); - plan.plancache_list = plancache_list; - plan.oneshot = true; - PG_TRY(); - { - ret = SPI_execute_plan_with_paramlist(&plan, NULL, read_only, tcount); - } - PG_FINALLY(); - { - /* reset sql_dialect */ - sql_dialect = prev_sql_dialect; - } - PG_END_TRY(); - - return ret; -} - PG_FUNCTION_INFO_V1(bbf_pivot); Datum bbf_pivot(PG_FUNCTION_ARGS) @@ -4652,13 +4588,9 @@ bbf_pivot(PG_FUNCTION_ARGS) MemoryContext per_query_ctx; MemoryContext oldcontext; HTAB *bbf_pivot_hash; - RawStmt *bbf_pivot_src_sql; - RawStmt *bbf_pivot_cat_sql; - List *pivot_parsetree; - List *pivot_extrainfo; - char *query_string; + char *src_sql_string; + char *cat_sql_string; char *funcName; - Node *node; /* check to see if caller supports us returning a tuplestore */ if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo)) @@ -4671,33 +4603,19 @@ 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."))); + src_sql_string = text_to_cstring(PG_GETARG_TEXT_PP(0)); + cat_sql_string = text_to_cstring(PG_GETARG_TEXT_PP(1)); + funcName = text_to_cstring(PG_GETARG_TEXT_PP(2)); - 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) + /* check if babelfish pivot metadata is complete */ + if (src_sql_string == NULL || cat_sql_string == NULL || funcName == NULL + || strlen(src_sql_string) == 0 || strlen(src_sql_string) == 0 || strlen(funcName) == 0) { ereport(ERROR, (errcode(ERRCODE_CHECK_VIOLATION), errmsg("Babelfish PIVOT is not properly initialized."))); } - 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); @@ -4718,14 +4636,13 @@ 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(cat_sql_string, per_query_ctx); /* let the caller know we're sending back a tuplestore */ rsinfo->returnMode = SFRM_Materialize; /* now go build it */ - rsinfo->setResult = get_bbf_pivot_tuplestore(bbf_pivot_src_sql, - query_string, + rsinfo->setResult = get_bbf_pivot_tuplestore(src_sql_string, funcName, bbf_pivot_hash, tupdesc, @@ -4748,7 +4665,8 @@ 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(const char *sourcetext, + MemoryContext per_query_ctx) { HTAB *bbf_pivot_hash; HASHCTL ctl; @@ -4776,7 +4694,7 @@ load_categories_hash(RawStmt *cats_sql, const char * sourcetext, MemoryContext p 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); + ret = SPI_execute(sourcetext, true, 0); tuple_processed = SPI_processed; /* Check for qualifying tuples */ @@ -4835,18 +4753,15 @@ load_categories_hash(RawStmt *cats_sql, const char * sourcetext, MemoryContext p return bbf_pivot_hash; } - - /* * create and populate the bbf_pivot tuplestore */ static Tuplestorestate * -get_bbf_pivot_tuplestore(RawStmt *sql, - const char *sourcetext, - const char *funcName, - HTAB *bbf_pivot_hash, - TupleDesc tupdesc, - bool randomAccess) +get_bbf_pivot_tuplestore(const char *sourcetext, + const char *funcName, + HTAB *bbf_pivot_hash, + TupleDesc tupdesc, + bool randomAccess) { Tuplestorestate *tupstore; int num_categories = hash_get_num_entries(bbf_pivot_hash); @@ -4865,7 +4780,7 @@ get_bbf_pivot_tuplestore(RawStmt *sql, 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); + ret = SPI_execute(sourcetext, true, 0); tuple_processed = SPI_processed; /* Check for qualifying tuples */ diff --git a/contrib/babelfishpg_tsql/sql/sys_functions.sql b/contrib/babelfishpg_tsql/sql/sys_functions.sql index 9f408b8db7b..d48e1891bd6 100644 --- a/contrib/babelfishpg_tsql/sql/sys_functions.sql +++ b/contrib/babelfishpg_tsql/sql/sys_functions.sql @@ -5201,7 +5201,7 @@ END; $body$ LANGUAGE plpgsql STABLE; -CREATE OR REPLACE FUNCTION sys.bbf_pivot() +CREATE OR REPLACE FUNCTION sys.bbf_pivot(IN src_sql TEXT, IN cat_sql TEXT, IN agg_func TEXT) RETURNS setof record AS 'babelfishpg_tsql', 'bbf_pivot' LANGUAGE C STABLE; diff --git a/contrib/babelfishpg_tsql/sql/upgrades/babelfishpg_tsql--4.3.0--4.4.0.sql b/contrib/babelfishpg_tsql/sql/upgrades/babelfishpg_tsql--4.3.0--4.4.0.sql index 236e293ccc6..fe5767701de 100644 --- a/contrib/babelfishpg_tsql/sql/upgrades/babelfishpg_tsql--4.3.0--4.4.0.sql +++ b/contrib/babelfishpg_tsql/sql/upgrades/babelfishpg_tsql--4.3.0--4.4.0.sql @@ -37,6 +37,13 @@ LANGUAGE plpgsql; * So make sure that any SQL statement (DDL/DML) being added here can be executed multiple times without affecting * final behaviour. */ +ALTER FUNCTION sys.bbf_pivot() RENAME TO bbf_pivot_deprecated_in_4_4_0; +CALL sys.babelfish_drop_deprecated_object('function', 'sys', 'bbf_pivot_deprecated_in_4_4_0'); + +CREATE OR REPLACE FUNCTION sys.bbf_pivot(IN src_sql TEXT, IN cat_sql TEXT, IN agg_func TEXT) +RETURNS setof record +AS 'babelfishpg_tsql', 'bbf_pivot' +LANGUAGE C STABLE; -- Assigning dbo role to the db_owner login DO $$ diff --git a/contrib/babelfishpg_tsql/src/hooks.c b/contrib/babelfishpg_tsql/src/hooks.c index 54ad1ec59f6..325a17bc99a 100644 --- a/contrib/babelfishpg_tsql/src/hooks.c +++ b/contrib/babelfishpg_tsql/src/hooks.c @@ -195,7 +195,6 @@ extern bool called_for_tsql_itvf_func(); static void is_function_pg_stat_valid(FunctionCallInfo fcinfo, PgStat_FunctionCallUsage *fcu, char prokind, bool finalize); -static void pass_pivot_data_to_fcinfo(FunctionCallInfo fcinfo, Expr *expr); static AclResult pltsql_ExecFuncProc_AclCheck(Oid funcid); /***************************************** @@ -274,7 +273,6 @@ static set_local_schema_for_func_hook_type prev_set_local_schema_for_func_hook = static bbf_get_sysadmin_oid_hook_type prev_bbf_get_sysadmin_oid_hook = NULL; static get_bbf_admin_oid_hook_type prev_get_bbf_admin_oid_hook = NULL; static transform_pivot_clause_hook_type pre_transform_pivot_clause_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; static exec_tsql_cast_value_hook_type pre_exec_tsql_cast_value_hook = NULL; @@ -461,9 +459,6 @@ InstallExtendedHooks(void) pre_transform_pivot_clause_hook = transform_pivot_clause_hook; transform_pivot_clause_hook = transform_pivot_clause; - pre_pass_pivot_data_to_fcinfo_hook = pass_pivot_data_to_fcinfo_hook; - pass_pivot_data_to_fcinfo_hook = pass_pivot_data_to_fcinfo; - prev_optimize_explicit_cast_hook = optimize_explicit_cast_hook; optimize_explicit_cast_hook = optimize_explicit_cast; @@ -492,7 +487,7 @@ InstallExtendedHooks(void) pltsql_replace_non_determinstic_hook = pltsql_replace_non_determinstic; prev_pltsql_is_partitioned_table_reloptions_allowed_hook = pltsql_is_partitioned_table_reloptions_allowed_hook; - pltsql_is_partitioned_table_reloptions_allowed_hook = is_partitioned_table_reloptions_allowed; + pltsql_is_partitioned_table_reloptions_allowed_hook = is_partitioned_table_reloptions_allowed; handle_param_collation_hook = set_param_collation; handle_default_collation_hook = default_collation_for_builtin_type; @@ -565,7 +560,7 @@ UninstallExtendedHooks(void) pltsql_unique_constraint_nulls_ordering_hook = prev_pltsql_unique_constraint_nulls_ordering_hook; pltsql_strpos_non_determinstic_hook = prev_pltsql_strpos_non_determinstic_hook; pltsql_replace_non_determinstic_hook = prev_pltsql_replace_non_determinstic_hook; - pltsql_is_partitioned_table_reloptions_allowed_hook = prev_pltsql_is_partitioned_table_reloptions_allowed_hook; + pltsql_is_partitioned_table_reloptions_allowed_hook = prev_pltsql_is_partitioned_table_reloptions_allowed_hook; ExecFuncProc_AclCheck_hook = prev_ExecFuncProc_AclCheck_hook; bbf_InitializeParallelDSM_hook = NULL; @@ -5062,6 +5057,19 @@ make_restarget_from_cstr_list(List * l) return tempResTarget; } +static A_Const * +makeStringConst(char *str, int location) +{ + A_Const *node; + + node = makeNode(A_Const); + node->val.sval.type = T_String; + node->val.sval.sval = str; + + node->location = location; + return node; +} + static void transform_pivot_clause(ParseState *pstate, SelectStmt *stmt) { @@ -5072,7 +5080,6 @@ transform_pivot_clause(ParseState *pstate, SelectStmt *stmt) List *src_sql_groupbylist; List *src_sql_sortbylist; List *src_sql_fromClause_copy; - List *pivot_context_list; char *pivot_colstr; char *value_colstr; String *funcName; @@ -5080,14 +5087,21 @@ transform_pivot_clause(ParseState *pstate, SelectStmt *stmt) TargetEntry *aggfunc_te; RangeFunction *wrapperSelect_RangeFunction; SelectStmt *pivot_src_sql; - RawStmt *s_sql; - RawStmt *c_sql; FuncCall *pivot_func; WithClause *with_clause; + RawStmt *src_sql_rawstmt; + RawStmt *cat_sql_rawstmt; + Query *src_sql_query; + Query *cat_sql_query; + char *src_sql_string; + char *cat_sql_string; + 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; @@ -5221,53 +5235,39 @@ transform_pivot_clause(ParseState *pstate, SelectStmt *stmt) wrapperSelect_RangeFunction->coldeflist = new_pivot_aliaslist; + src_sql_rawstmt = makeNode(RawStmt); + cat_sql_rawstmt = makeNode(RawStmt); - 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; + src_sql_rawstmt->stmt = (Node *) pivot_src_sql; + src_sql_rawstmt->stmt_location = 0; + src_sql_rawstmt->stmt_len = 0; - c_sql->stmt = (Node *) stmt->catSql; - c_sql->stmt_location = 0; - c_sql->stmt_len = 0; + cat_sql_rawstmt->stmt = (Node *) stmt->catSql; + cat_sql_rawstmt->stmt_location = 0; + cat_sql_rawstmt->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)) - ) - ); + /* get psql-text of src_sql and cat_sql */ + src_sql_query = parse_analyze_fixedparams((RawStmt *) copyObject(src_sql_rawstmt), + pstrdup(pstate->p_sourcetext), + NULL, 0, NULL); + src_sql_string = pg_get_querydef(src_sql_query, true); + + cat_sql_query = parse_analyze_fixedparams((RawStmt *) copyObject(cat_sql_rawstmt), + pstrdup(pstate->p_sourcetext), + NULL, 0, NULL); + cat_sql_string = pg_get_querydef(cat_sql_query, true); /* 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->context = (Node *) pivot_context_list; + pivot_func = makeFuncCall(list_make2(makeString("sys"), makeString("bbf_pivot")), + list_make3((Node *) makeStringConst(src_sql_string, -1), + (Node *) makeStringConst(cat_sql_string, -1), + (Node *) makeStringConst(pstrdup(funcName->sval), -1) + ), + COERCE_EXPLICIT_CALL, + -1); wrapperSelect_RangeFunction->functions = list_make1(list_make2((Node *) pivot_func, NIL)); } -static void -pass_pivot_data_to_fcinfo(FunctionCallInfo fcinfo, Expr *expr) -{ - /* if current FuncExpr is a bbf_pivot function, we set the fcinfo context to pivot data */ - if (sql_dialect != SQL_DIALECT_TSQL) - return; - - if (IsA(expr, FuncExpr) - && ((FuncExpr*) expr)->context != NULL - && (IsA(((FuncExpr*) expr)->context, List))) - { - Node *node; - node = list_nth((List *)((FuncExpr*) expr)->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) - { - fcinfo->context = ((FuncExpr*) expr)->context; - } - } -} static Node* optimize_explicit_cast(ParseState *pstate, Node *node) { @@ -5487,4 +5487,4 @@ default_collation_for_builtin_type(Type typ, bool handle_pg_type) } return oid; -} \ No newline at end of file +} diff --git a/contrib/babelfishpg_tsql/src/tsqlUnsupportedFeatureHandler.cpp b/contrib/babelfishpg_tsql/src/tsqlUnsupportedFeatureHandler.cpp index fb8e5c5668f..63e7ecae13d 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); @@ -523,11 +522,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) @@ -1394,14 +1389,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/pivot-before-15_9-or-16_5-vu-cleanup.out b/test/JDBC/expected/pivot-before-15_9-or-16_5-vu-cleanup.out new file mode 100644 index 00000000000..6a397073ffd --- /dev/null +++ b/test/JDBC/expected/pivot-before-15_9-or-16_5-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_9-or-16_5-vu-prepare.out b/test/JDBC/expected/pivot-before-15_9-or-16_5-vu-prepare.out new file mode 100644 index 00000000000..48a8f4f60b0 --- /dev/null +++ b/test/JDBC/expected/pivot-before-15_9-or-16_5-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_9-or-16_5-vu-verify.out b/test/JDBC/expected/pivot-before-15_9-or-16_5-vu-verify.out new file mode 100644 index 00000000000..0a31371a94b --- /dev/null +++ b/test/JDBC/expected/pivot-before-15_9-or-16_5-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..a1a9fa49382 100644 --- a/test/JDBC/expected/pivot-vu-cleanup.out +++ b/test/JDBC/expected/pivot-vu-cleanup.out @@ -1,61 +1,119 @@ -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 table pivot_insert_into; +DROP VIEW JOIN_PIVOT8 GO -drop table pivot_select_into; +DROP VIEW pivot_schema.QUAL_NAME_VIEW GO -drop procedure top_n_pivot; +DROP TRIGGER pivot_trigger GO -drop function test_table_valued_function; +DROP TABLE trigger_testing GO -drop table StoreReceipt; +DROP TABLE OSTable; GO -drop table orders; +DROP TABLE STable; GO -drop table products; +DROP TABLE seating_tbl; GO -drop table pivot_schema.products_sch; +DROP VIEW StoreReceipt_view; GO -drop schema pivot_schema; +DROP VIEW pivot_schema.data_src GO -use master; +DROP TABLE pivot_insert_into; GO -drop database pivot_test; +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 diff --git a/test/JDBC/expected/pivot-vu-prepare.out b/test/JDBC/expected/pivot-vu-prepare.out index eaaa9743fe3..d57d9baf4b9 100644 --- a/test/JDBC/expected/pivot-vu-prepare.out +++ b/test/JDBC/expected/pivot-vu-prepare.out @@ -669,6 +669,11 @@ GO create schema pivot_schema; GO +create view pivot_schema.data_src +as +select * from StoreReceipt +GO + CREATE TABLE pivot_schema.products_sch ( productId int PRIMARY KEY, productName VARCHAR(30), @@ -731,29 +736,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 +865,505 @@ 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 + +-- VIEW HAS SAME SCHEMA NAME AS SOURCE TABLE'S +CREATE VIEW pivot_schema.QUAL_NAME_VIEW +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 pivot_schema.data_src +)AS SrcTable +PIVOT ( + COUNT (OrderID) + FOR StoreID IN ([1], [2], [3],[4], [5]) +) AS pvt +GO diff --git a/test/JDBC/expected/pivot-vu-verify.out b/test/JDBC/expected/pivot-vu-verify.out index 1a159e01b80..4a6cad36cf1 100644 --- a/test/JDBC/expected/pivot-vu-verify.out +++ b/test/JDBC/expected/pivot-vu-verify.out @@ -1009,7 +1009,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.081 ms ~~END~~ SET BABELFISH_SHOWPLAN_ALL OFF; @@ -1882,18 +1882,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 +1936,553 @@ 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~~ + + +-- TEST VIEW HAS SAME SCHEMA NAME AS SOURCE TABLE'S +SELECT * FROM pivot_schema.QUAL_NAME_VIEW ORDER BY 1 +GO +~~START~~ +varchar#!#int#!#int#!#int#!#int#!#int +OrderNumbers#!#19#!#19#!#19#!#16#!#14 +~~END~~ + diff --git a/test/JDBC/input/pivot-before-15_9-or-16_5-vu-cleanup.sql b/test/JDBC/input/pivot-before-15_9-or-16_5-vu-cleanup.sql new file mode 100644 index 00000000000..6a397073ffd --- /dev/null +++ b/test/JDBC/input/pivot-before-15_9-or-16_5-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_9-or-16_5-vu-prepare.sql b/test/JDBC/input/pivot-before-15_9-or-16_5-vu-prepare.sql new file mode 100644 index 00000000000..c524102a6d1 --- /dev/null +++ b/test/JDBC/input/pivot-before-15_9-or-16_5-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_9-or-16_5-vu-verify.sql b/test/JDBC/input/pivot-before-15_9-or-16_5-vu-verify.sql new file mode 100644 index 00000000000..d493a04db17 --- /dev/null +++ b/test/JDBC/input/pivot-before-15_9-or-16_5-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..a1a9fa49382 100644 --- a/test/JDBC/input/pivot-vu-cleanup.sql +++ b/test/JDBC/input/pivot-vu-cleanup.sql @@ -1,57 +1,119 @@ -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 VIEW pivot_schema.QUAL_NAME_VIEW +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 VIEW pivot_schema.data_src +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 diff --git a/test/JDBC/input/pivot-vu-prepare.sql b/test/JDBC/input/pivot-vu-prepare.sql index 57d70d06af6..2e7bc5bce54 100644 --- a/test/JDBC/input/pivot-vu-prepare.sql +++ b/test/JDBC/input/pivot-vu-prepare.sql @@ -265,6 +265,11 @@ GO create schema pivot_schema; GO +create view pivot_schema.data_src +as +select * from StoreReceipt +GO + CREATE TABLE pivot_schema.products_sch ( productId int PRIMARY KEY, productName VARCHAR(30), @@ -325,25 +330,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 +406,490 @@ begin FOR StoreID IN ([1], [2], [3],[4], [5]) ) AS pvt end -GO \ No newline at end of file +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 + +-- VIEW HAS SAME SCHEMA NAME AS SOURCE TABLE'S +CREATE VIEW pivot_schema.QUAL_NAME_VIEW +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 pivot_schema.data_src +)AS SrcTable +PIVOT ( + COUNT (OrderID) + FOR StoreID IN ([1], [2], [3],[4], [5]) +) AS pvt +GO diff --git a/test/JDBC/input/pivot-vu-verify.sql b/test/JDBC/input/pivot-vu-verify.sql index 0a4a0b5d8fa..00048f58534 100644 --- a/test/JDBC/input/pivot-vu-verify.sql +++ b/test/JDBC/input/pivot-vu-verify.sql @@ -865,14 +865,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 +901,85 @@ PIVOT( COUNT(employeeName) FOR productName IN ([mac], [iphone], [ipad], [charger]) ) as pvt -GO \ No newline at end of file +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 + +-- TEST VIEW HAS SAME SCHEMA NAME AS SOURCE TABLE'S +SELECT * FROM pivot_schema.QUAL_NAME_VIEW ORDER BY 1 +GO diff --git a/test/JDBC/upgrade/15_5/schedule b/test/JDBC/upgrade/15_5/schedule index 4727a5ff648..7969757b3f9 100644 --- a/test/JDBC/upgrade/15_5/schedule +++ b/test/JDBC/upgrade/15_5/schedule @@ -485,7 +485,7 @@ sys_certificates sys_database_permissions #BABEL-4279 BABEL-4484 -pivot +pivot-before-15_9-or-16_5 #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 8467d9e58fa..92cb04ef936 100644 --- a/test/JDBC/upgrade/15_6/schedule +++ b/test/JDBC/upgrade/15_6/schedule @@ -498,7 +498,7 @@ sys_certificates sys_database_permissions #BABEL-4279 BABEL-4484 -pivot +pivot-before-15_9-or-16_5 #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 e57c0005aec..ef41d3ad221 100644 --- a/test/JDBC/upgrade/15_7/schedule +++ b/test/JDBC/upgrade/15_7/schedule @@ -499,7 +499,7 @@ sys_certificates sys_database_permissions #BABEL-4279 BABEL-4484 -pivot +pivot-before-15_9-or-16_5 #AUTO_ANALYZE #uncomment this test when preparing for new minor version cast_eliminate TestDatatypeAggSort diff --git a/test/JDBC/upgrade/15_8/schedule b/test/JDBC/upgrade/15_8/schedule index cfd2fe2b22a..2c3a41e4404 100644 --- a/test/JDBC/upgrade/15_8/schedule +++ b/test/JDBC/upgrade/15_8/schedule @@ -496,7 +496,7 @@ sys_certificates sys_database_permissions #BABEL-4279 BABEL-4484 -pivot +pivot-before-15_9-or-16_5 #AUTO_ANALYZE #uncomment this test when preparing for new minor version cast_eliminate TestDatatypeAggSort diff --git a/test/JDBC/upgrade/16_1/schedule b/test/JDBC/upgrade/16_1/schedule index 081876f1f17..af72bac6291 100644 --- a/test/JDBC/upgrade/16_1/schedule +++ b/test/JDBC/upgrade/16_1/schedule @@ -489,7 +489,7 @@ sys_certificates sys_database_permissions #BABEL-4279 BABEL-4484 -pivot +pivot-before-15_9-or-16_5 #AUTO_ANALYZE #uncomment this test when preparing for new minor version cast_eliminate TestDatatypeAggSort diff --git a/test/JDBC/upgrade/16_2/schedule b/test/JDBC/upgrade/16_2/schedule index 5267eff2b1f..426f5e369a1 100644 --- a/test/JDBC/upgrade/16_2/schedule +++ b/test/JDBC/upgrade/16_2/schedule @@ -502,7 +502,7 @@ sys_certificates sys_database_permissions #BABEL-4279 BABEL-4484 -pivot +pivot-before-15_9-or-16_5 #AUTO_ANALYZE #uncomment this test when preparing for new minor version cast_eliminate TestDatatypeAggSort diff --git a/test/JDBC/upgrade/16_3/schedule b/test/JDBC/upgrade/16_3/schedule index ded9725a5dd..84c9a3d6ac2 100644 --- a/test/JDBC/upgrade/16_3/schedule +++ b/test/JDBC/upgrade/16_3/schedule @@ -503,7 +503,7 @@ sys_certificates sys_database_permissions #BABEL-4279 BABEL-4484 -pivot +pivot-before-15_9-or-16_5 #AUTO_ANALYZE #uncomment this test when preparing for new minor version cast_eliminate TestDatatypeAggSort diff --git a/test/JDBC/upgrade/16_4/schedule b/test/JDBC/upgrade/16_4/schedule index ea849406514..a3ea1678924 100644 --- a/test/JDBC/upgrade/16_4/schedule +++ b/test/JDBC/upgrade/16_4/schedule @@ -508,7 +508,7 @@ sys_certificates sys_database_permissions #BABEL-4279 BABEL-4484 -pivot +pivot-before-15_9-or-16_5 #AUTO_ANALYZE #uncomment this test when preparing for new minor version cast_eliminate TestDatatypeAggSort diff --git a/test/python/expected/upgrade_validation/expected_dependency.out b/test/python/expected/upgrade_validation/expected_dependency.out index 23e217493bb..80511dc5f7e 100644 --- a/test/python/expected/upgrade_validation/expected_dependency.out +++ b/test/python/expected/upgrade_validation/expected_dependency.out @@ -214,7 +214,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_string_agg_finalfn_nvarchar(internal) Function sys.bbf_string_agg_finalfn_varchar(internal) Function sys.bbf_varbinary(sys.geography)