unpivot

transpose rows to columns in sql

时光总嘲笑我的痴心妄想 提交于 2019-12-04 05:30:53
问题 I have problem in getting the desired output with the SQL query. My sql data is as follows: TOTAL Charge PAYMNET A B C D E MonthYear ------- ----------- ----------- --------- -------- ---------- ------- ------- ---------- 661 157832.24 82967.80 700.00 10.70 58329.33 0.00 0.00 Oct-2013 612 95030.52 17824.28 850.00 66.10 53971.41 0.00 0.00 Nov-2013 584 90256.35 16732.91 700.00 66.10 52219.87 0.00 0.00 Dec-2013 511 72217.32 12336.12 285.00 53.17 42951.12 0.00 0.00 Jan-2014 I need the output as

Transpose rows and columns without aggregate [closed]

倾然丶 夕夏残阳落幕 提交于 2019-12-03 20:48:47
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center . Closed 7 years ago . I have the following dataset Date Field1 Col1 Col2 Col3 2012/07/02 Customer1 CL DS RT 2012/07/03 Customer1 DS RT 700 2012/07/04 Customer1 DS RT 700 2012/07/02 Customer2 CL DS RT 2012/07/03 Customer2 DS RT 1500 2012/07/04 Customer2 DS RT 1500 2012/07/02 Customer3 CL DS RT 2012/07/03 Customer3 DS RT 6000 2012/07/04

GROUP BY or COUNT Like Field Values - UNPIVOT?

北城以北 提交于 2019-12-03 09:54:40
I have a table with test fields, Example id | test1 | test2 | test3 | test4 | test5 +----------+----------+----------+----------+----------+----------+ 12345 | P | P | F | I | P So for each record I want to know how many Pass, Failed or Incomplete (P,F or I) Is there a way to GROUP BY value? Pseudo: SELECT ('P' IN (fields)) AS pass WHERE id = 12345 I have about 40 test fields that I need to somehow group together and I really don't want to write this super ugly, long query. Yes I know I should rewrite the table into two or three separate tables but this is another problem. Expected Results:

SQL server using unpivot function [closed]

非 Y 不嫁゛ 提交于 2019-12-02 23:31:48
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 6 years ago . How would you obtain the following output using SQL? Type: Value Type2 Value2 p_01 1 ofpk_01 1 p_02 2 ofpk_02 2 p_03 3 ofpk_03 3 Table structure create table test(p_01 int, p_02 int, p_03 int, ofpk_01 int, ofpk_02 int, ofpk_03 int) SQL fiddle link Is this possible with unpivot function? here is

Unpivot in Access SQL

梦想与她 提交于 2019-12-02 13:12:56
Hi guys I'm trying to use unpivot in SQL on MS Access and I found the following code online: SELECT CustomerID, Phone FROM ( SELECT CustomerID, Phone1, Phone2, Phone3 FROM dbo.CustomerPhones ) AS cp UNPIVOT ( Phone FOR Phones IN (Phone1, Phone2, Phone3) ) AS up; from this webpage: https://www.mssqltips.com/sqlservertip/3000/use-sql-servers-unpivot-operator-to-help-normalize-output/ However when I tried the exact same code on Access, it keeps saying the FROM clause has an error. I wonder if this is because the syntax for access is somehow different from that in SQL server? I would really

Dynamic Query in MySQL

我与影子孤独终老i 提交于 2019-12-02 11:19:49
I have a following table. /------------------------------------\ | LocID | Year | Birth | Death | Abc | |------------------------------------| | 1 | 2011 | 100 | 60 | 10 | |------------------------------------| | 1 | 2012 | 98 | 70 | 20 | |..... | \------------------------------------/ I need the output to be (Condition LocID = 1) /---------------------\ | Event | 2011 | 2012 | |---------------------| | Birth | 100 | 98 | |---------------------| | Death | 60 | 70 | |---------------------| | Abc | 10 | 20 | \---------------------/ The table may contain more fields based on various requirements.

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

三世轮回 提交于 2019-12-02 10:53:48
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 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 convert the expense and revenue columns into rows that you can append the date to. Once the date is added to

SQL: “Reverse” transpose a table

拜拜、爱过 提交于 2019-12-02 10:22:17
问题 I saw a lot of questions on transposing from the below table... scanid | region | volume ------------------------- 1 A 34.4 1 B 32.1 1 C 29.1 2 A 32.4 2 B 33.2 2 C 35.6 to this table. scanid | A_volume | B_volume | C_volume ---------------------------------------- 1 34.4 32.1 29.1 2 32.4 33.2 35.6 However, I need to do the inverse, and have trouble trying to wrap my head around this problem. Can anyone help? Thank you. 回答1: You could do this very simply with a UNION clause: Select Scan_ID, 'A

SQL: “Reverse” transpose a table

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-02 07:13:52
I saw a lot of questions on transposing from the below table... scanid | region | volume ------------------------- 1 A 34.4 1 B 32.1 1 C 29.1 2 A 32.4 2 B 33.2 2 C 35.6 to this table. scanid | A_volume | B_volume | C_volume ---------------------------------------- 1 34.4 32.1 29.1 2 32.4 33.2 35.6 However, I need to do the inverse, and have trouble trying to wrap my head around this problem. Can anyone help? Thank you. You could do this very simply with a UNION clause: Select Scan_ID, 'A' as Region, A_Volume as volume union all Select Scan_ID, 'B' as Region, B_Volume as volume union all Select

Expand a comma separated table in multiple rows

可紊 提交于 2019-12-02 03:02:19
I have some table with a key and multiple values associated to it in a comma separated way. Would like to change it to one row per value, repeating the key multiple times as follow. Would you know how to do this without any Excel macro ? pnuts Assuming your numbers are text format and abc is in A2, select ColumnB, DATA > Data Tools - Text to Columns, Delimited, Comma, Column data format Text (for all columns) then label the columns and create a Table via a PivotTable from your data with Multiple consolidation ranges as described here . Delete ColumnB and Filter the Value column to remove blank