Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

sql query from multiple columns in a row to multiple row per columns

Posted on 2014-09-23
8
Medium Priority
?
220 Views
Last Modified: 2014-09-23
table1 columns,
thedate ,system_id, hour,call type1, call type2, call_type3 from table1
group by thedate, system_id, hour

example values
01/12/14, 1,  1,  25, 13, 1
01/12/14, 1,  1,  20, 11, 0
01/12/14  1,  2, 30,23,15
..
..

From the table above is it possible to create query that will output:

01/12/14 1, 1,  'call type1',  45
01/12/14  1,1,  'call type2',  24
01/12/14   1,1, 'call type3', 1
..
..
0
Comment
Question by:Shen
  • 4
  • 4
8 Comments
 
LVL 40

Accepted Solution

by:
PatHartman earned 2000 total points
ID: 40339207
You can't do it with one query but you can do it with one query per column you want to pivot (three in this case) plus a union query to pull them all together into a single recordset.

TheUnionQuery -
Select thedate ,system_id, hour, "Call type 1" AS CallType, [call type1] From YourTable
Union Select thedate ,system_id, hour, "Call type 2" AS CallType, [call type2] From YourTable
Union Select thedate ,system_id, hour, "Call type 3" AS CallType, [call type3] From YourTable;
0
 
LVL 70

Expert Comment

by:Scott Pletcher
ID: 40339285
SELECT
    thedate, system_id, hour,
    CASE whichCallType
        WHEN 1 THEN 'call type1'
        WHEN 2 THEN 'call type2'
        WHEN 3 THEN 'call type3' END AS [call type label],
    CASE whichCallType
        WHEN 1 THEN [call type1]
        WHEN 2 THEN [call type2]
        WHEN 3 THEN [call type3] END AS [call type]
FROM (
    SELECT
        thedate, system_id, hour,
        SUM([call type1]) AS [call type1], SUM([call type2]) AS [call type2], SUM([call type3]) AS [call type3]
    FROM table1
    GROUP BY thedate, system_id, hour
) AS derived
CROSS JOIN (
    SELECT 1 AS whichCallType UNION ALL
    SELECT 2 UNION ALL
    SELECT 3
) AS whichCallTypes
ORDER BY thedate, system_id, hour
0
 
LVL 40

Expert Comment

by:PatHartman
ID: 40339348
Scott,
My T-SQL is a little spotty.  Does that query write three rows for every "one" row it reads?

Amended suggestion to include summing.

TheUnionQuery -
 Select thedate ,system_id, hour, "Call type 1" AS CallType, Sum([call type1]) As SumCallType From YourTable
Group By thedate ,system_id, hour, "Call type 1"
 Union Select thedate ,system_id, hour, "Call type 2" AS CallType, Sum([call type2]) As SumCallType From YourTable
thedate ,system_id, hour, "Call type 2"
 Union Select thedate ,system_id, hour, "Call type 3" AS CallType, Sum([call type3]) As SumCallType From YourTable
thedate ,system_id, hour, "Call type 3";
0
Prepare for an Exciting Career in Cybersecurity

Help prevent cyber-threats and provide solutions to safeguard our global digital economy. Earn your MS in Cybersecurity. WGU’s MSCSIA degree program curriculum features two internationally recognized certifications from the EC-Council at no additional time or cost.

 
LVL 70

Expert Comment

by:Scott Pletcher
ID: 40339372
@Pat:
Yes.  The CROSS JOIN will join every row in the first table (our table input, in this case) and join it to every row in the second table (numbers 1, 2 and 3 in this case).  Thus, 3 rows are output for every row in the main input table.
0
 
LVL 40

Expert Comment

by:PatHartman
ID: 40339396
Thanks,
Access SQL hasn't changed in over 20 years (except to add support for the abomination data types) so I forget what is possible when you step out into the real world.  I still like my curmudgeon approach though since it works in Jet/ACE as well as SQL Server.
0
 
LVL 70

Expert Comment

by:Scott Pletcher
ID: 40339414
You could still do that in Access.  Just do an INNER JOIN ON 1 = 1, which would have the same affect as a CROSS JOIN.
0
 
LVL 70

Expert Comment

by:Scott Pletcher
ID: 40339435
Huh?  That query doesn't even sum up the values as originally requested.
0
 
LVL 40

Expert Comment

by:PatHartman
ID: 40340119
It looks like I flunked cut and paste.  Here it is - one last time.

TheUnionQuery -
Select thedate ,system_id, hour, "Call type 1" AS CallType, Sum([call type1]) As SumCallType
From YourTable
Group By thedate ,system_id, hour, "Call type 1"

Union Select thedate ,system_id, hour, "Call type 2" AS CallType, Sum([call type2]) As SumCallType
From YourTable
Group By thedate ,system_id, hour, "Call type 2"

Union Select thedate ,system_id, hour, "Call type 3" AS CallType, Sum([call type3]) As SumCallType
From YourTable
Group By thedate ,system_id, hour, "Call type 3";
0

Featured Post

Get Certified for a Job in Cybersecurity

Want an exciting career in an emerging field? Earn your MS in Cybersecurity and get certified in ethical hacking or computer forensic investigation. WGU’s MSCSIA degree program was designed to meet the most recent U.S. Department of Homeland Security (DHS) and NSA guidelines.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Windows Explorer lets you open cabinet (cab) files like any other folder. In VBA you can easily handle normal files and folders, but opening and indeed creating cabinet files takes a lot more - and that's you'll find here.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

972 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question