Solved

sql view or query to reformat data

Posted on 2014-01-01
5
187 Views
Last Modified: 2014-01-10
Hello Experts,

I am banging my head on what seems to be a simple query.  I need to massage the data into a different presentation.  I need to take the values of the input table and create column names for the output table.

Here's what the input table looks like:
      
ID      TYPE      Value
45      invoice      12345
45      check      555444
45      date              12/5/2013
46      invoice      987654
46      check      555666
46      date              12/3/2013

i need the output to look like this:
output                  
ID      invoice      check      date
45      12345      555444      12/5/2013
46      987654      555666      12/3/2013

thanks!!!
0
Comment
Question by:vinny3333
5 Comments
 
LVL 21

Expert Comment

by:Dale Burrell
ID: 39750370
You need to pivot the data, sorry don't have time to do it, but here is a reference http://technet.microsoft.com/en-us/library/ms177410(v=sql.105).aspx
0
 
LVL 24

Accepted Solution

by:
chaau earned 250 total points
ID: 39750403
This is how you do it with PIVOT:
SELECT ID, 
[invoice], [check], [date]
FROM
(SELECT ID, Type, Value 
    FROM Table1) AS SourceTable
PIVOT
(
MAX(Value)
FOR Type IN ([invoice], [check], [date])
) AS PivotTable;

Open in new window

0
 
LVL 31

Assisted Solution

by:awking00
awking00 earned 250 total points
ID: 39752139
If there are a limited number of values for type  (e.g. 3 as in your example) -
select id,
max(case when type = 'invoice' then value end) as invoice,
max(case when type = 'check' then value end) as check_rcvd,
max(case when type = 'date' then value end) as somedate
from input_table
group by id;
0
 

Author Comment

by:vinny3333
ID: 39754866
Thx for the education!!  Both solutions will work for my needs.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.

744 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

14 Experts available now in Live!

Get 1:1 Help Now