Solved

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

Posted on 2014-09-23
8
211 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 34

Accepted Solution

by:
PatHartman earned 500 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 69

Expert Comment

by:ScottPletcher
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 34

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
 
LVL 69

Expert Comment

by:ScottPletcher
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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 34

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 69

Expert Comment

by:ScottPletcher
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 69

Expert Comment

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

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

New My Cloud Pro Series - organize everything!

With space to keep virtually everything, the My Cloud Pro Series offers your team the network storage to edit, save and share production files from anywhere with an internet connection. Compatible with both Mac and PC, you're able to protect your content regardless of OS.

Question has a verified solution.

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

Suggested Solutions

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Phishing is at the top of most security top 10 efforts you should be pursuing in 2016 and beyond. If you don't have phishing incorporated into your Security Awareness Program yet, now is the time. Phishers, and the scams they use, are only going to …
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

919 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now