Solved

stored procedure rows to columns

Posted on 2013-01-09
11
35 Views
Last Modified: 2016-01-28
i have a table looks like that (notes, actually i have more columns, but it is a simplified version)

1  A  10
1  B  3
2  A  9
2  C  40

How can i convert them to
     A    B    C
1  10   3
2  9         40
0
Comment
Question by:hongclub
  • 4
  • 2
  • 2
  • +1
11 Comments
 
LVL 73

Accepted Solution

by:
sdstuber earned 500 total points
ID: 38760041
SELECT   col1,
         MAX(CASE WHEN col2 = 'A' THEN col3 END) a,
         MAX(CASE WHEN col2 = 'B' THEN col3 END) b,
         MAX(CASE WHEN col2 = 'C' THEN col3 END) c
    FROM yourtable
GROUP BY col1
ORDER BY col1;
0
 
LVL 12

Expert Comment

by:Jared_S
ID: 38760477
Depending on the amount of data you have, you may want to use a pivot command.

The syntax will be similar to:

select * from (
select col1, col2, col3 from mytable)
a
pivot (max(col3) for col2 in ([A],[B],[C])) as b

Open in new window

0
 

Author Comment

by:hongclub
ID: 38760986
now i twist my data a little bit better and put them into a temp table

How can i make this result set (#result):
A    B     C        dD          dE            dF              dG
test test  test  01/05/2006
test test  test              01/06/2006
test test  test                           01/08/2006
test test  test                                       01/09/2006
test2 test2 test2 06/05/2013
test2 test2 test2             06/06/2013
test2 test2 test2                          06/08/2013
test2 test2 test2                                          06/09/2013  


to

A     B     C                 dD                    dE                     dF                     dG
test  test  test               01/05/2006      01/06/2006      01/08/2006      01/09/2006
test2 test2 test2        06/05/2013      06/06/2013      06/08/2013      06/09/2013


(notes: dD, dE .... are dynamic, but consider it as statis first to make it easier)
0
 

Author Comment

by:hongclub
ID: 38760991
you think i should do a left join the table itself?
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 73

Expert Comment

by:sdstuber
ID: 38761034
>>> (notes: dD, dE .... are dynamic, but consider it as statis first to make it easier)

you can't have dynamic columns in SQL.

every column must be known at the time the statement is parsed, not after it executes
0
 
LVL 73

Assisted Solution

by:sdstuber
sdstuber earned 500 total points
ID: 38761058
no, a left join won't really help

but, using your data above, same idea as before, except it's easier because your data is already nulled and spread as needed to make the aggregation work

SELECT   a,b,c,
         MAX(dd) dd,
         MAX(de) de,
         MAX(df) df,
         max(dg) dg
    FROM yourtable
GROUP BY a,b,c
ORDER BY a,b,c;
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 41428570
I've requested that this question be deleted for the following reason:

Not enough information to confirm an answer.
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 41428562
both of the queries I posted above produces the results requested from the data provided

ID: 38760041 -- for the original data and results
ID: 38761058  -- for the extended data and new results
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 41428571
The dynamic column part is what might be needed, hence my suggestion. But on the other hand, you answered the original question plus addition ... I'll change my suggestion.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
query help 18 57
SQL Server R2 Stored procedure performance 8 42
MSSQL 2014 Query Synthax 8 46
SQL Server CASE .. WHEN .. IN statement - Syntax issue 4 49
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

929 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

10 Experts available now in Live!

Get 1:1 Help Now