Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

sql 2005 pivot data

Posted on 2013-11-08
5
Medium Priority
?
296 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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
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 video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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

636 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