unpivot

PIVOT two columns and keep others as they are

六月ゝ 毕业季﹏ 提交于 2019-12-08 02:13:26
问题 I want to turn some of the rows into columns while keeping other rows as they are. ID name value RefId 1 Fname John 32145 2 LName Smith 32145 3 Fname Peter 34589 4 LName Mahlang 34589 Now what I want to achieve is to turn the Fname and Lname rows into columns with their matching value field. ID column doesn't really matter, I don't need it. Desired Output Fname Lname RefId John Smith 32145 Peter Mahlang 34589 Any help 回答1: Using conditional aggregation: select Fname = max(case when name =

How to replace a functional (many) OUTER APPLY (SELECT * FROM)

此生再无相见时 提交于 2019-12-07 17:43:33
问题 Applies to Microsoft SQL Server 2008 R2. The problem is If we have a few dozen Outer Apply (30) then they begin to work pretty slowly. In the middle of the Outer Apply I have something more complicated than a simple select, a view. Details I'm writing a sort of attributes assigned to tables (in the database). Generally, a few tables, holds a reference to a table of attributes (key, value). Pseudo structure looks like this: DECLARE @Lot TABLE ( LotId INT PRIMARY KEY IDENTITY, SomeText VARCHAR

Create a row for each cell that has data from multiple columns

↘锁芯ラ 提交于 2019-12-07 17:29:28
I'm dealing with data that has been generated from a survey that has a unique respondant ID as the first column and then has multiple columns relating to the choices of country that the respondent was looking at in relation to finding employees. So my table looks something like: RespondentID Andorra Austria Belgium Cyprus Denmark Finland France 2546078180 Andorra NULL NULL Cyprus NULL NULL NULL 2546077668 NULL NULL Belgium NULL NULL NULL NULL 2546077120 NULL NULL NULL NULL Denmark Finland NULL What I want to end up with is a table that lists the Respondent ID for each answer given. So on the

How to join row values to column names in a dynamic query

一曲冷凌霜 提交于 2019-12-07 15:22:30
问题 I am developing an application that allows configurable questions and answers. Currently there can be up to 20 answers, but possibly less. The structure I have is as follows: Questions +----+--------+--------------+-------------+ | ID | FormId | QuestionText | AnswerField | +----+--------+--------------+-------------+ | 1 | 1 | Name | Answer01 | | 2 | 1 | Address | Answer02 | | 3 | 1 | Phone | Answer03 | | 4 | 1 | Email | Answer04 | | 5 | 2 | First Name | Answer01 | | 6 | 2 | Surname |

SQL Server unpivot two columns

谁说我不能喝 提交于 2019-12-07 12:51:45
问题 I'm trying to pivot a table to get 3 columns my example table is like : CREATE TABLE tbl1 (A1 int, cA1 int,A2 int, cA2 int,A3 int, cA3 int) GO INSERT INTO tbl1 VALUES (60,2,30,3,10,5); GO I am using the query below to get tthe results from two columns: select A, value from tbl1 unpivot ( value for A in ([A1], [A2],[A3]) ) un1; The results are like : A | value --+------- A1|60 A2|30 A3|10 but I want to add and second column with and the results to be like : A | value1 | value2 --+--------+----

SQL Pivot with dynamic generated columns, aggregate function and columns without aggregation

旧时模样 提交于 2019-12-07 04:01:06
问题 I have got the following query: WITH preEKBE AS( SELECT EKPO . MANDT, EKPO . EBELN, EKPO . EBELP, DD07T.DDTEXT AS c_Meaning, EKBE . VGABE, EKBE . DMBTR, EKBE . MENGE, COUNT(VGABE) OVER(PARTITION BY EKBE . EBELN, EKBE . EBELP, ZEKKN) AS c_COUNT, CONVERT (varchar(10),MIN(EKBE . BLDAT) OVER ( PARTITION BY EKBE . EBELN, EKBE . EBELP, EKBE . VGABE),104) AS c_EBKE_BLDAT_First, CONVERT (varchar(10),MIN(EKBE . BUDAT) OVER ( PARTITION BY EKBE . EBELN, EKBE . EBELP, EKBE . VGABE),104) AS c_EKBE_BUDAT

Pivoting rows into columns in SQL Server

故事扮演 提交于 2019-12-06 12:47:38
问题 I have a set of data that looks like this: Before FirstName LastName Field1 Field2 Field3 ... Field27 --------- -------- ------ ------ ------ ------- Mark Smith A B C D John Baptist X T Y G Tom Dumm R B B U However, I'd like the data to look like this: After FirstName LastName Field Value --------- -------- ----- ----- Mark Smith 1 A Mark Smith 2 B Mark Smith 3 C Mark Smith 4 D John Baptist 1 X John Baptist 2 T John Baptist 3 Y John Baptist 4 G Tom Dumm 1 R Tom Dumm 2 B Tom Dumm 3 B Tom Dumm

PIVOT two columns and keep others as they are

假装没事ソ 提交于 2019-12-06 08:11:05
I want to turn some of the rows into columns while keeping other rows as they are. ID name value RefId 1 Fname John 32145 2 LName Smith 32145 3 Fname Peter 34589 4 LName Mahlang 34589 Now what I want to achieve is to turn the Fname and Lname rows into columns with their matching value field. ID column doesn't really matter, I don't need it. Desired Output Fname Lname RefId John Smith 32145 Peter Mahlang 34589 Any help Using conditional aggregation: select Fname = max(case when name = 'Fname' then value end) , Lname = max(case when name = 'Lname' then value end) , RefId from t group by RefId

SQL Server unpivot two columns

為{幸葍}努か 提交于 2019-12-06 03:48:37
I'm trying to pivot a table to get 3 columns my example table is like : CREATE TABLE tbl1 (A1 int, cA1 int,A2 int, cA2 int,A3 int, cA3 int) GO INSERT INTO tbl1 VALUES (60,2,30,3,10,5); GO I am using the query below to get tthe results from two columns: select A, value from tbl1 unpivot ( value for A in ([A1], [A2],[A3]) ) un1; The results are like : A | value --+------- A1|60 A2|30 A3|10 but I want to add and second column with and the results to be like : A | value1 | value2 --+--------+-------- A1| 60 | 2 A2| 30 | 3 A3| 10 | 5 Any Help?? I would use APPLY : select v.* from tbl1 t cross apply

How to unpivot in BigQuery?

本小妞迷上赌 提交于 2019-12-06 03:06:58
问题 Not sure what functions to call, but transpose is the closest thing I can think of. I have a table in BigQuery that is configured like this: but I want to query a table that is configured like this: What does the SQL code look like for creating this table? Thanks! 回答1: Use the UNION of tables (with ',' in BigQuery), plus some column aliasing: SELECT Location, Size, Quantity FROM ( SELECT Location, 'Small' as Size, Small as Quantity FROM [table] ), ( SELECT Location, 'Medium' as Size, Medium