unpivot

Build SQL query with dynamic columns

*爱你&永不变心* 提交于 2019-12-05 18:39:01
My tables go as follows: Patients table PatientId Name 1 James ... Visits table Date PatientID_FK Weight 1/1 1 220 2/1 1 210 ... How can I build a query that returns PatientId Name Visit1Date Visit1Weight Visit2Date Visit2Weight ... 1 James 1/1 220 2/1 210 2 ... How can we add more columns in this way? How to write that SELECT ? Please help. Some posts on StackExchange say it is impossible for a SQL statement to handle it. Is it really so? This type of data transformation will need to be done with both a pivot and the unpivot functions. Since your visits will be unknown, then you will want to

SQL query ; horizontal to vertical

牧云@^-^@ 提交于 2019-12-05 17:55:14
I'm stuck with a SQL query (SQL Server) that involves converting horizontal rows to vertical rows Below is my data No Flag_1 Flag_2 Flag_3 --- ---- ----- ----- A 1 2 3 B 4 1 6 After conversion , the table should be No FlagsName Flag_value -- ---- ---------- A Flag_1 1 A Flag_2 2 A Flag_3 3 B Flag_1 4 B Flag_2 1 B Flag_3 6 Any input on this would be helpful? I'm trying to play around ROW_NUMBER over partition. but it is not working somehow !!! Thanks !!! You can use a UNION ALL : select No, 'Flag_1' as FlagName, Flag_1 as Flag_Value from yourtable union all select No, 'Flag_2' as FlagName, Flag

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

微笑、不失礼 提交于 2019-12-05 08:18:39
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_First, CONVERT (varchar(10),MAX(EKBE . BLDAT) OVER ( PARTITION BY EKBE . EBELN, EKBE . EBELP, EKBE .

Unpivot T-sql Query

只谈情不闲聊 提交于 2019-12-04 22:45:58
following is the result of my query Year_Month.........cat1_per......cat2_per........cat3_per......cat4_per 2004_06...............0.892..........0.778............0.467..........0.871 2005_10...............0.790..........0.629............0.581..........0.978 but i want output of my query to be Category...........2004_06..............2005_10 cat1_per.............0.892..................0.790 cat2_per.............0.778..................0.629 cat3_per.............0.467..................0.581 cat4_per.............0.871..................0.978 what is the best way to do it? any help is appreciated. I

Pivoting rows into columns in SQL Server

假装没事ソ 提交于 2019-12-04 19:43:25
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 4 U I have looked at the PIVOT function. It may work. I am not too sure. I couldn't make sense of how

SQL 2008 Combining data from multiple rows with the same ID into one row

允我心安 提交于 2019-12-04 18:50:40
I have data in one table that contains several rows of data for the same CardNum. I would like to create a table where all the data for the same CardNum is displayed on the same row. My data is currently like this: PartID | CardNumber | RdrGrpID | TZID 0 412 31 1 0 412 34 1 0 567 38 1 0 567 33 5 0 567 71 3 This is how I would like the data to be: PartID | CardNumber | RdrGrpID_1 | TZID_1 | RdrGrpID_2 | TZID_2 | RdrGrpID_3 | TZID_3 0 412 31 1 34 1 0 567 38 1 33 5 71 3 Thank you in advance. To get this result, there are several ways that you can formulate the query. If you have a limited number

How to Pivot a Table?

佐手、 提交于 2019-12-04 16:28:49
My table has this structure subcode date rol1 rol2 rol3 rol4 rol5 rol6 upto rol60 -------------------------------------------------------------- mc1603 12/03/2011 p p a p p p a mc1604 12/03/2011 p p a p p p a mc1605 12/03/2011 p p a p p p a mc1606 12/03/2011 p p a p p p a here p=present a=absent this table will be change into rollno mc1603 mc1604 mc1605 mc1606 date ------------------------------------------------- rol1 p p p p 12/03/2011 rol2 p p p p 12/03/2011 rol3 p p a p 12/03/2011 Here is how you would do this with both an unpivot and then pivot : Static query: create table #temp ( subcode

Any .Net function or linq query to unpivot data

自作多情 提交于 2019-12-04 11:12:17
Is there any .net library available to unpivot excel data? I am currently using LinqToExcel framework to read data from spreadsheets, so not sure if there are dynamic linq queries available to perform the unpivot. Thanks for any suggestions. BTW, I am looking for a solution which could handle multiple columns. Example Original Table Product Location Customer1 Customer2 Customer3 A X 10 20 100 Destinaton Table Product Location Customer Demand A X Customer1 10 A X Customer2 20 A X Customer3 100 Try something like this: var customer1 = from c in excel.Worksheet() select new { Product = c["Product

How to unpivot in BigQuery?

老子叫甜甜 提交于 2019-12-04 08:00:44
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! 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 as Quantity FROM [table] ), ( SELECT Location, 'Large' as Size, Large as Quantity FROM [table] ) @Felipe, I

SQL Server: Dynamic pivot with headers to include column name and date

时光毁灭记忆、已成空白 提交于 2019-12-04 06:38:07
问题 I am trying to use dynamic pivot and need help on converting rows to columns The table looks like: ID expense revenue date 1 43 45 12-31-2012 1 32 32 01-01-2013 3 64 56 01-31-2013 4 31 32 02-31-2013 and I need for reporting purposes like ID expense12-31-2012 expense01-01-2013 expense01-31-2013 revenue12-31-2013 1 43 32 3 64 回答1: In order to get both the expense and revenue columns as headers with the date , I would recommend applying both the UNPIVOT and the PIVOT functions. The UNPIVOT will