?
Solved

sql 2005 pivot data

Posted on 2013-11-08
5
Medium Priority
?
291 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 32

Expert Comment

by:Brendt Hess
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 93

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 41

Accepted Solution

by:
Sharath earned 1000 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 49

Assisted Solution

by:PortletPaul
PortletPaul earned 1000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

777 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