Questions tagged [dynamic-pivot]

The PIVOT syntax converts row data into columnar data. A dynamic PIVOT is needed when there are an unknown number of row values that need to be converted to columns.

The PIVOT syntax converts row data into columnar data. A dynamic PIVOT is needed when there are an unknown number of row values that need to be converted to columns.

Not all databases support a PIVOT operation but it can be replicated using decision logic (CASE statements) and aggregate functions.

Dynamic SQL is an enhanced form of SQL (Structured Query Language) whose main difference from traditional SQL is that Dynamic SQL allows to build the PIVOT statement dynamically at runtime, which eases the automatic generation and execution of program statements.

177 questions
33
votes
1 answer

Dynamically create columns sql

I have a table of Customers Customer ID Name 1 John 2 Lewis 3 Mary I have another table CustomerRewards TypeID Description 1 …
CR41G14
  • 5,464
  • 5
  • 43
  • 64
21
votes
11 answers

Dynamic Pivot in Oracle's SQL

... pivot (sum(A) for B in (X)) Now B is of datatype varchar2 and X is a string of varchar2 values separated by commas. Values for X are select distinct values from a column(say CL) of same table. This way pivot query was working. But the…
prabhakar
  • 385
  • 1
  • 2
  • 8
17
votes
3 answers

SQL Server - Dynamic PIVOT Table - SQL Injection

Sorry for the long question but this contains all the SQL I've used to test the scenario to hopefully make it clear as to what I'm doing. I'm build up some dynamic SQL to produce a PIVOT table in SQL Server 2005. Below is code to do this. With…
Robin Day
  • 100,552
  • 23
  • 116
  • 167
15
votes
2 answers

How to pivot dynamically with date as column

I have a table with product id's and names, and another table with the stock of these products on certain dates. Such as Item1 had 6 stock on 1-1-2014 and 8 stock on 2-1-2014. I'm trying to show these in a stored procedure so that it looks like a…
Craphex
  • 153
  • 1
  • 1
  • 5
8
votes
4 answers

SQL Dynamic Pivot - how to order columns

I'm working on a dynamic pivot query on a table that contains: OID - OrderID Size - size of the product BucketNum - the order that the sizes should go quantity - how many ordered The size column contains different sizes depending upon the…
Chris Burgess
  • 5,787
  • 13
  • 54
  • 69
7
votes
1 answer

How to pivot a table in Presto?

Let be a table named data with columns time, sensor, value : I want to pivot this table on Athena (Presto) to get a new table like this one : To do so, one can run the following query : SELECT time, sensor_value['temperature'] as "temperature",…
kakarotto
  • 180
  • 1
  • 11
6
votes
2 answers

Row and column total in dynamic pivot

In SQL Server 2008, I have a table (tblStock) with 3 columns: PartCode (NVARCHAR (50)) StockQty (INT) Location (NVARCHAR(50)) some example data below: PartCode StockQty Location ......... ......... ......... A …
Sokea
  • 327
  • 5
  • 19
5
votes
1 answer

Pivoting a table in SQL Server 2005 to contain same column multiple times

I need to pivot the following table named tblGameRoleName - Game Role Name VolleyBall Coach Sujatha VolleyBall Player Rajendran VolleyBall Player Juno VolleyBall Player Indira VolleyBall Player Ganesh VolleyBall Player …
MediumOne
  • 804
  • 3
  • 11
  • 28
5
votes
3 answers

MySQL dynamic-pivot

I have a table of product parts like this: Parts part_id part_type product_id -------------------------------------- 1 A 1 2 B 1 3 A 2 4 B 2 5 …
Vaughan
  • 162
  • 3
  • 9
4
votes
1 answer

SQL join to join values in primary table with child table values as different columns in a single row

I have a survey_datas table contain data's as like this survey_data_id | title 1 | Paul 3 | Anna 4 | Alan Another table project_playlist_indexes contain the data's like this survey_id …
Rosa Mystica
  • 243
  • 1
  • 3
  • 17
3
votes
1 answer

Generate report converting rows into column using multiple tables

I am working on a report with SQL Server database. My tables are: Table Name: appointments id appointment_number applicant_name appointment_date appointment_time ----- ------------------ ------------------------ …
Sanjay S
  • 33
  • 4
3
votes
3 answers

Dynamic pivoting SQL Server 2012

I am making attempts to run my first dynamic pivot in SQL Server 2012. My #temp table that I am using for the dynamic pivoting looks like this. YearMonth Agreement nr Discount ------------------------------------ 201303 123 …
user3197575
  • 269
  • 6
  • 13
3
votes
1 answer

Arithmetic operators against dynamic column in SQL Server 2008

I have query to create table and result below: -- tblPart PartCode[NVARCHAR(50)],UnitPrice[Decimal(18,2)] SELECT * INTO #tblPart FROM( SELECT 'A' PartCode, '10' UnitPrice UNION All SELECT 'B','11' UNION All SELECT 'C','38' UNION All SELECT…
Sokea
  • 327
  • 5
  • 19
3
votes
1 answer

How do I combine two columns into single column in dynamic pivot

I have a schema like this demo(id, val, month, year, decide) Demo data and schema is given in this fiddle http://sqlfiddle.com/#!3/dd89d5/1 In output i want to transform the rows to columns. In the output i want, ID (11 14) (12 14) (2 15) ... …
2
votes
1 answer

SQL Server Pivot Dynamic Sql with two columns

Update 2022-03-04 Here is my fiddle, showing the tables and sample values CREATE TABLE code ( id int primary key , labVal varchar(50) , Label varchar(50) ) INSERT INTO code VALUES (1,'Code1','Important 1') ,(2,'Code2','Important…
XKZ
  • 126
  • 7
1
2 3
11 12