unpivot

MySQL dynamic transpose/pivot of a table

醉酒当歌 提交于 2019-12-13 04:54:07
问题 I've been trying to pivot a very sample table: TYPE | COUNT ___________________ 'CHAINS' | '38' 'INDEP' | '64' 'SUPER' | '25' Yup, it's that simple. I've been reading tutorials, Stackoverflow answers and I've been looking for a good tutorial about transposing a table. I just can't understand it very well and I haven't been able to accomplish the following result: CHAINS | INDEP | SUPER _______________________ 38 | 64 | 25 I know there are other questions similar to this one, but I just can't

static column name in pivot table

白昼怎懂夜的黑 提交于 2019-12-13 04:07:16
问题 DECLARE @cols AS NVARCHAR(MAX) DECLARE @query AS NVARCHAR(MAX) select @cols = '[1015],[1060],[1261],[1373]' print @cols set @query = 'SELECT header, ' + @cols + ' from ( select product_id, header, value from ( select cast(product_id as varchar(100))product_id , cast(product_name as varchar(100)) product_name, cast(product_price as varchar(100)) product_price, cast(product_weight as varchar(100))product_weight, cast((select TOP 1 image_name from tblProductImage where tblProductImage.product_id

UNPIVOT on multiple columns to return multiple columns

孤街醉人 提交于 2019-12-13 03:44:44
问题 Below is the requirement to be acheived in PL/SQL- The table format is CREATE TABLE NETWORK_TABLE ( ORIG_CODE NUMBER, ORIG_SV NUMBER, DEST_CODE NUMBER, DEST_SV NUMBER ) Sample Data - INSERT INTO network_table VALUES ( 14, 1, 15, 1); INSERT INTO network_table VALUES ( 18, 4, 11, 1); INSERT INTO network_table VALUES ( 15, 1, 22, 3); INSERT INTO network_table VALUES ( 23, 2, 21, 1); INSERT INTO network_table VALUES ( 14, 3, 11, 1); INSERT INTO network_table VALUES ( 12, 2, 22, 2); Table data

Taking the “transpose” of a table using SQL

ぐ巨炮叔叔 提交于 2019-12-12 12:26:36
问题 I don't know if there is a name for this operation but it's similar to the transpose in linear algebra. Is there a way to turn an 1 by n table T1 such as c_1|c_2|c_3|...|a_n ------------------- 1 |2 |3 |...|n Into a n by 2 table like the following key|val ------- c_1|1 b_2|2 c_3|3 . |. . |. a_n|n I am assuming that each column c_i in T1 can be unlikely identified. 回答1: Basically, you need to UNPIVOT this data, you can perform this using a UNION ALL : select 'c_1' col, c_1 value from yourtable

Pivot and Unpivot for 4 joined table SQL Server

核能气质少年 提交于 2019-12-12 03:53:49
问题 I'm using SQL Server 2012, and I want to create a pivot table with 4 joined tables. Here's my query : SELECT a.Itemno, a.Qty as PlanMilling, ISNULL(b.MinimStock, 0) as MinStock, CAST(a.ScheduleDate as Date) AS Schedule, ISNULL(SUM(c.Qty), 0) as QtyBuilding, ISNULL(d.RunQty, 0) as QtyStock, d.itemcode, ISNULL((a.Qty + d.RunQty) - SUM(c.Qty), 0) as Balance FROM Schedule a LEFT OUTER JOIN Item b ON a.ItemNo = b.ItemNo LEFT OUTER JOIN ShopOrderList c on a.ItemNo = c.ItemNo and a.ScheduleDate = c

Change data in vertical table to individual rows

馋奶兔 提交于 2019-12-12 02:45:54
问题 I have an Excel workbook with the following format: | A | B | C | D | E | |----|-------|-------|-------|-------| 1| | po#1 | po#2 | po#3 | po#1 | 2| | date1 | date2 | date3 | date4 | 3|sku1| qty1 | qty4 | qty7 | qty10 | 4|sku2| qty2 | qty5 | qty8 | qty11 | 5|sku3| qty3 | qty6 | qty9 | qty12 | that I need to convert to the following format: | A | B | C | D | |--------|-------|-------|-------| 1| po#1 | date1 | sku1 | qty1 | 2| po#1 | date1 | sku2 | qty2 | 3| po#1 | date1 | sku3 | qty3 | 4| po

Sorting data for PIVOT source

落爺英雄遲暮 提交于 2019-12-12 00:13:35
问题 Imagine a table (key-value) with the Attributes and the parent table with Lots. LotId SomeText ----------- -------- 1 Hello 2 World AttributeId LotId Val Kind ----------- ----------- -------- -------- 1 1 Foo1 Kind1 2 1 Foo2 Kind2 3 2 Bar1 Kind1 4 2 Bar2 Kind2 5 2 Bar3 Kind3 I am using UNPIVOT - PIVOT operation to get the data in the form of: LotId SomeText AttributeId LotId Kind1Val Kind AttributeId LotId Kind2Val Kind AttributeId LotId Kind3Val Kind ----------- -------- ----------- --------

MySql : Convert Column data to row

删除回忆录丶 提交于 2019-12-11 16:59:33
问题 I have a table in mysql which looks like below. id cust_id date data 1 1 1/1/2018 a b c d e f g 2 1 2/1/2018 h I j k l m n Here in this example data column is having huge data seperated by space like a b c d, I would like to show case as in row like below id cust_id date data 1 1 1/1/2018 a 1 1 1/1/2018 b 1 1 1/1/2018 c 1 1 1/1/2018 d 2 2 2/1/2018 h 2 2 2/1/2018 i 2 2 2/1/2018 j 2 2 2/1/2018 k I have checked few option like using unpivot function, but unable to achieve my output. Thanks in

Multi Column Pivot SQL Server

拈花ヽ惹草 提交于 2019-12-11 15:56:52
问题 I have a problem that has deals with multi column pivoting in SQL Server 2008. I would like to explain by an example. Below is the result of joining 6 different tables using inner and left outer join ID Type Date Location Result Proc ProcDate ProcDetail ProcNotes -------------------------------------------------------------------------------------- 1 ABC 1/1/2010 OK AO Proc_A 1/1/2013 This is Detail Proc_A Notes 1 XYG 1/2/2011 Proc_A 1/1/2013 This is Detail Proc_A Notes 1 ABC 1/1/2010 OK AO

Unpivot dynamic table columns into key value rows

这一生的挚爱 提交于 2019-12-11 15:47:46
问题 The problem that I need to resolve is data transfer from one table with many dynamic fields into other structured key value table. The first table comes from a data export from another system, and has the following structure ( it can have any column name and data): [UserID],[FirstName],[LastName],[Email],[How was your day],[Would you like to receive weekly newsletter],[Confirm that you are 18+] ... The second table is where I want to put the data, and it has the following structure: [UserID