Solved

sql 2005 pivot data

Posted on 2013-11-08
5
276 Views
Last Modified: 2013-11-14
id               date                        time
id1             11/8/2013                900
id1             11/8/2013              1000
id2             11/8/2013                910
id2             11/8/2013              1010

if I have data like the above and I want to rearrange it like this:
id               date                        time1          time2
id1             11/8/2013                900           1000
id2             11/8/2013                910           1010

What is the best sql to do that? Acceptable answer must be for sql server 2005 and sql only.
0
Comment
Question by:qube09
5 Comments
 
LVL 32

Expert Comment

by:bhess1
ID: 39634769
There are a lot of assumptions that need to be made to answer this question. And this sort of looks like instructional work, not real world work.  This would not need to be pivot data at all, and as structured really is not suited to be pivot data.  

HINT: Look at using MIN, MAX, and GROUP BY for your query.
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 39635113
Are there only ever two times per ID/date, or can there be just one or >2?
0
 

Author Comment

by:qube09
ID: 39635207
generally but could be four
0
 
LVL 40

Accepted Solution

by:
Sharath earned 250 total points
ID: 39635294
try like this.
;WITH CTE AS (SELECT id,[date],[time],
				     ROW_NUMBER() OVER (PARTITION BY id,[date] ORDER BY [time]) rn
				FROM your_table)
SELECT id,[date],
       MAX(CASE rn WHEN 1 THEN [time] END) AS time1,
	   MAX(CASE rn WHEN 2 THEN [time] END) AS time2,
	   MAX(CASE rn WHEN 3 THEN [time] END) AS time3,
	   MAX(CASE rn WHEN 4 THEN [time] END) AS time4
  FROM id,[date]

Open in new window

0
 
LVL 48

Assisted Solution

by:PortletPaul
PortletPaul earned 250 total points
ID: 39635630
nb: line 9 in the comment above should read "FROM CTE" and probably was going on with an extra line for "order by id, [date]"

Another vote for using row_number() as Sharath has, my only additional comment is that you don't have to use the CTE approach, a nested subquery containing the row_number() would work just as well.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…
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.

808 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