pivot-without-aggregate

how to transform vertical fields in a table to horizontal result by SQL

血红的双手。 提交于 2019-12-24 10:03:27
问题 I have a table like this: create table t1 { person_id int, item_name varchar(30), item_value varchar(100) }; Suppose person_id+item_name is the composite key, now I have some data (5 records) in table t1 as below: person_id ====item_name ====== item_value 1 'NAME' 'john' 1 'GENDER' 'M' 1 'DOB' '1970/02/01' 1 'M_PHONE' '1234567890' 1 'ADDRESS' 'Some Addresses unknown' Now I want to use SQL (or combing store procedure/function or whatever) to query the above result (1 result set) become: NAME=

How to create a pivot query

亡梦爱人 提交于 2019-12-09 19:05:44
问题 Imagine I have this table: Column A | Column B | Column C ------------------------------ 111 X 10 111 Y 12 How can I query this table to show the results like these: Column A | X | Y ----------------------------------- 111 10 12 回答1: You can perform this via a PIVOT. You can use either a static PIVOT where you know the number of columns that you want to rotate or you can use a dynamic PIVOT Static Pivot (see SQL Fiddle with Demo) SELECT * FROM ( select * from t1 ) x pivot ( min(columnc) for

Select query by type wise

北城以北 提交于 2019-12-06 13:29:35
问题 Table1 Code, desc, type id 01 Rajan 1 01 Sajan 1 01 Vijayan 2 01 Suresh 3 01 Caresh 4 01 Sujesh 4 01 vikran 4 02 desk 1 02 card 2 02 villa 2 02 megash 2 02 supan 3 .... I want to view the table by type id wise Expected Output Code type-1 type-2 type-3 type-4 01 Rajan Vijayan suresh caresh 01 Sajan null null Sujan 01 null null null vikran 02 desk card supan null 02 null villa null null 02 null megash null null How to make a query for the above condition Need Query Help 回答1: So first off just

How to create a pivot query

血红的双手。 提交于 2019-12-04 14:28:52
Imagine I have this table: Column A | Column B | Column C ------------------------------ 111 X 10 111 Y 12 How can I query this table to show the results like these: Column A | X | Y ----------------------------------- 111 10 12 You can perform this via a PIVOT . You can use either a static PIVOT where you know the number of columns that you want to rotate or you can use a dynamic PIVOT Static Pivot (see SQL Fiddle with Demo ) SELECT * FROM ( select * from t1 ) x pivot ( min(columnc) for columnb in ([X], [Y]) ) p Dynamic Pivot (see SQL Fiddle with Demo ) DECLARE @cols AS NVARCHAR(MAX), @query

SQL Transpose Rows as Columns

拜拜、爱过 提交于 2019-11-26 20:01:48
I have an interesting conundrum which I believe can be solved in purely SQL. I have tables similar to the following: responses: user_id | question_id | body ---------------------------- 1 | 1 | Yes 2 | 1 | Yes 1 | 2 | Yes 2 | 2 | No 1 | 3 | No 2 | 3 | No questions: id | body ------------------------- 1 | Do you like apples? 2 | Do you like oranges? 3 | Do you like carrots? and I would like to get the following output user_id | Do you like apples? | Do you like oranges? | Do you like carrots? --------------------------------------------------------------------------- 1 | Yes | Yes | No 2 | Yes

SQL Transpose Rows as Columns

空扰寡人 提交于 2019-11-26 06:33:54
问题 I have an interesting conundrum which I believe can be solved in purely SQL. I have tables similar to the following: responses: user_id | question_id | body ---------------------------- 1 | 1 | Yes 2 | 1 | Yes 1 | 2 | Yes 2 | 2 | No 1 | 3 | No 2 | 3 | No questions: id | body ------------------------- 1 | Do you like apples? 2 | Do you like oranges? 3 | Do you like carrots? and I would like to get the following output user_id | Do you like apples? | Do you like oranges? | Do you like carrots?

TSQL Pivot without aggregate function

房东的猫 提交于 2019-11-26 00:09:00
问题 I have a table like this... CustomerID DBColumnName Data -------------------------------------- 1 FirstName Joe 1 MiddleName S 1 LastName Smith 1 Date 12/12/2009 2 FirstName Sam 2 MiddleName S 2 LastName Freddrick 2 Date 1/12/2009 3 FirstName Jaime 3 MiddleName S 3 LastName Carol 3 Date 12/1/2009 And I want this... Is this possible using PIVOT? CustomerID FirstName MiddleName LastName Date ---------------------------------------------------------------------- 1 Joe S Smith 12/12/2009 2 Sam S