unpivot

Pivot query to return multiple repeating groups?

会有一股神秘感。 提交于 2019-12-23 20:23:30
问题 I'm trying to get a result set (which will be inserted into a table) that has multiple repeating groups. Here's a script that shows a very simplified version of the data I'm starting out with: CREATE TABLE #Aggregate( StoreKey int , NumberOfDaysBack int , ThisYearGrossTransactions int , ThisYearGrossPrice money , LastYearGrossTransactions int , LastYearGrossPrice money ) GO INSERT #Aggregate VALUES (10134, 7, 198, 71324.3600, 248, 95889.6089) INSERT #Aggregate VALUES (10131, 7, 9, 1299.8300,

SQL Server , restrict UNPIVOT to order columns automatically

拟墨画扇 提交于 2019-12-23 19:46:51
问题 I have a table with data in one row: Account | OrderID | OrderName | Priority | Fasting |AssignedTo |ResultsTo |Location ---------------------------------------------------------------------------------------------------------------------------- 12345 | REQ123456 | Lipid Panel (1.2) |Routine | Yes |Fast, Brook |Nurse Group |Fisher Rd, Woodbridge, NV Now I want to UNPIVOT the data to show the user in the following form: GROUPCOL | LABEL | VALUE -------------------------------------------------

Oracle 11 SQL : Split 1 row into x rows and insert a new column

巧了我就是萌 提交于 2019-12-23 12:34:32
问题 I asked Oracle 11 SQL : Is there a way to split 1 row into x rows -- this question is very close to that but has a small twist ... Customer asked to Split 1 row from the Oracle DB SQL into 6 rows. Let's say, originally the SQL (complex sql with multiple joins , etc) is pulling in 9 columns: select A, B, C, D, E, F, G, H, I from X, Y, Z . . . (but quite complex query) 1) A, B, C, D, E, F, G, H, I. Now, customer is asking for every row returning above pattern, the new output should be like

SQL Server unpivot multiple columns

假如想象 提交于 2019-12-23 06:58:39
问题 I'm trying to pivot a table around it's many columns to get to 3 columns (pivot, column name, value) so for example: name | age | gender ------+-------+--------- John | 20 | M Jill | 21 | F would become: name | column | value -----+--------+------- John | age | 20 John | gender | M Jill | age | 21 Jill | gender | F I've googled quite a bit but haven't found a similar situation - especially since the pivot seems to be done in the opposite direction as what I'm trying to accomplish. 回答1: The

Unpivoting multiple columns

给你一囗甜甜゛ 提交于 2019-12-23 01:55:07
问题 I have a table in SQL Server 2014 called anotes with the following data and I want to add this data into another table named final as ID Notes NoteDate With text1, text2, text3, text4 going into the Notes column in the final table and Notedate1,notedate2,notedate3,notedate4 going into Notedate column. I tried unpivoting the data with notes first as: select createdid, temp from (select createdid,text1,text2,text3,text4 from anotes) p unpivot (temp for note in(text1,text2,text3,text4)) as unpvt

How to UnPivot for multiple columns SQLServer

倾然丶 夕夏残阳落幕 提交于 2019-12-22 11:06:29
问题 In my application, I have used store product description values as follows: ID BILLNO CUS_NAME DATE TOT_BAL S1 S2 S3 S4 D1 D2 D3 D4 Q1 Q2 Q3 Q4 U1 U2 U3 U4 T1 T2 T3 T4 TOTAL CUSCODE ------------------------------------------------------------------------------------------------------------------------------------------------------------------- 29 1 XXX Apr-03-2017 1932 1 2 3 NULL AAA BBB CCC NULL 6 30 6 NULL 80 35 67 NULL 480 1050 402 0 1932 DF 40 2 YYYY Apr-04-2017 6454 1 2 3 NULL AAA DDD

Build SQL query with dynamic columns

倖福魔咒の 提交于 2019-12-22 10:28:26
问题 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? 回答1: This type of data transformation will need to be

SQL query ; horizontal to vertical

回眸只為那壹抹淺笑 提交于 2019-12-22 08:57:22
问题 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 !!! 回答1: You can use a UNION ALL : select

Unpivot T-sql Query

旧城冷巷雨未停 提交于 2019-12-22 01:02:28
问题 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

How to Pivot a Table?

时光怂恿深爱的人放手 提交于 2019-12-21 22:52:07
问题 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 回答1: