unpivot

t-sql most efficient row to column? crosstab for xml path, pivot

烂漫一生 提交于 2019-12-24 16:32:31
问题 I am looking for the most performant way to turn rows into columns. I have a requirement to output the contents of the db (not actual schema below, but concept is similar) in both fixed width and delimited formats. The below FOR XML PATH query gives me the result I want, but when dealing with anything other than small amounts of data, can take awhile. select orderid ,REPLACE(( SELECT ' ' + CAST(ProductId as varchar) FROM _details d WHERE d.OrderId = o.OrderId ORDER BY d.OrderId,d.DetailId FOR

How to achieve the below goal in obiee dashboard

拟墨画扇 提交于 2019-12-24 15:46:12
问题 I have a table like below: id name role1 role2 role3 ------------------------- 1 John y n y 2 Pete n y y 3 Den n y y 4 Mat y n n After I filter the table by using role1='Y' , I lost Pete and Den. How can I use the analysis to build a table like below: Count (Y) Role1 3 Role2 2 Role3 3 I tried everything. Please help Thanks 回答1: If your database is Oracle 11g or later you can use the unpivot clause SELECT usr_role, COUNT(*) role_count FROM (SELECT * FROM table_name UNPIVOT (hasRole FOR usr

SQL query to get the resultset in two columns only

。_饼干妹妹 提交于 2019-12-24 14:34:59
问题 I have this table: id fName lName Address PostCode ContactNumber ----------------------------------------------------- 1 Tom Daley London EC1 4EQ 075825485665 2 Jessica Ennis Sheffield SF2 3ER 075668956665 3 Joe Bloggs Glasgow G3 2AZ 075659565666 I want a query to give me the results like this: id | label 1 | Tom 1 | Daley 1 | London 1 | EC1 4EQ 1 | 075825485665 2 | Jessica 2 | Ennis 2 | Sheffied and so on so forth. Any suggestions please on how to do this. 回答1: You can use the UNPIVOT

Dynamic Unpivot and split Columns SQL Server 2012

馋奶兔 提交于 2019-12-24 12:41:07
问题 I have a table as MarketOutput that has 20 Columns [Region] [LOB] [GWP 2013] [GWP 2014] [LR 2013] [LR 2014] ------------------------------------------------------------- North Workers 38902.50 37,972,404 89 82 I would like to dynamically change column to rows. Region and LOB are fixed columns, [GWP 2013], [GWP 2014], [LR 2013], [LR 2014] are dynamic columns. For next year they will be [GWP 2015] , [LR 2015] I want to unpivot the column and split the [GWP 2014] as two column [GWP], [2014]. The

Convert multiple columns row values into multiple columns

冷暖自知 提交于 2019-12-24 11:36:02
问题 I've tried all possibilities around Pivot, Unpivot crosstab - you name it. Struggle is that i've over worked myself with this. I am hoping it wi going to be a simple solution just that I cant see it. I'm so sorry for not providing in DDL. But essentially I have a table that looks like this. DeptName Metric1_Name Metric1_Value Metric2_Name Metric2_Value Metric3_Name Metric3_Value Metric4_Name Metric4_Value ABC Sales Per Hour 200 Wins Per Hour 10 Leads per Hour 2 Losses per Hour 1 ABC Sales per

SSRS 2005 find name of column with max value

て烟熏妆下的殇ゞ 提交于 2019-12-24 08:58:10
问题 The column on the far right is what I'm trying to add to my report. Is this possible to do without modifying the query to use something like Unpivot? Step X Step W Step A Step B Step Y Last Step --------------------------------------------------------------------- 1/21/2013 1/24/2013 1/3/2013 1/5/2013 1/7/2013 Step W This is a step in the right direction, but it appears to only work in SSRS 2008: http://www.bigator.com/2012/04/26/spothighlight-minimum-and-maximum-values-in-each-row-in-matrix

Unpivot table in SQL Server

微笑、不失礼 提交于 2019-12-24 03:06:09
问题 I have a table with following columns: SeqNo, Date, Code, Val1, Val2, Val3,.. Val20 I need to get this representation (I assume I should unpivot table part from Val1 to Val20): SeqNo, Date, Code, Val where all Val1 ..Val20 columns go to Val column. And moreover I need to change Date column values: For "Val1" value in "Date" shouldn't be changed. For "Val2" the "Date" value should be decreased by 1 day. For "Val3" decrease by 2 days, etc. 回答1: You can do the pivot manually with a cross join

How to transpose columns to rows in sql server

北城以北 提交于 2019-12-24 02:18:17
问题 I'm trying to take a raw data set that adds columns for new data and convert it to a more traditional table structure. The idea is to have the script pull the column name (the date) and put that into a new column and then stack each dates data values on top of each other. Example Store 1/1/2013 2/1/2013 XYZ INC $1000 $2000 To Store Date Value XYZ INC 1/1/2013 $1000 XYZ INC 2/1/2013 $2000 thanks 回答1: There are a few different ways that you can get the result that you want. You can use a SELECT

How to transpose columns to rows in sql server

拈花ヽ惹草 提交于 2019-12-24 02:18:07
问题 I'm trying to take a raw data set that adds columns for new data and convert it to a more traditional table structure. The idea is to have the script pull the column name (the date) and put that into a new column and then stack each dates data values on top of each other. Example Store 1/1/2013 2/1/2013 XYZ INC $1000 $2000 To Store Date Value XYZ INC 1/1/2013 $1000 XYZ INC 2/1/2013 $2000 thanks 回答1: There are a few different ways that you can get the result that you want. You can use a SELECT

Turn SQLite columns to rows

帅比萌擦擦* 提交于 2019-12-24 01:22:44
问题 I am trying to convert a query which returns a single row with multiple columns to multiple rows with one or multiple columns. As an example my query looks like the following visualized: SELECT _id, value1, value2, value3, value4 FROM table WHERE _id IS 1; RESULT: _id value1 value2 value3 value4 ---------------------------------------- 1 10 11 12 13 I would like to have my results look like the following: name value -------------- value1 10 value2 11 value3 12 value4 13 but I can also work