Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Sql convert vertical to horizontal

Author  Topic 

micron009
Starting Member

2 Posts

Posted - 2012-07-30 : 23:29:48
i have a table like this one..


Table1

No StudentNo Name Date Time

1 101 Paula 2012/07/16 08:00:00AM
1 101 Paula 2012/07/16 12:00:00PM
1 101 Paula 2012/07/16 12:50:00PM
1 101 Paula 2012/07/16 06:00:00PM


and then i want to convert it into table2 like this..


Table2

No. EmpNo Name Date TimeIn Brk Out Brk In TimeOut

1 101 Paula 2012/07/16 08:00:00AM 12:00:00PM 12:50:00PM 06:00:00PM

Please help me please,,

Paula

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-07-30 : 23:51:44
how do you determine which date value should go to Brk In,Brk Out etc?

Is there a column to determine order?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2012-07-31 : 03:58:14
;with cte as
(select studentNo, Name, Date, Time, seq = row_number() over (partition by No, studentNo, Date order by Time from table1)
select No, StudentNo, Date, TimeIn = max(case when seq = 1 then time), BrkOut = max(case when seq = 2 then time), BrkIn = max(case when seq = 3 then time), TimeOut = max(case when seq = 4 then time)
group by No, StudentNo, Date

But as visakh16 suggests there will be a lot more to this.


==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

micron009
Starting Member

2 Posts

Posted - 2012-08-01 : 02:57:54
Hi visakh16 and nigelrivett,

Thanks guys for your suggestions. This are the DATA VALUEs and its equivalent time, which i need from TABLE1, then i find it difficult to insert this DATA VALUEs to TABLE2 as shown below which will be sorted by DATE.

---Data value ---
TimeIn = 06:00-11:59
BrkOut = 12:00-12:59
BrkIn = 12:00-12:59
TimeOut = 13:00-21:00



---Table1---

No----StudentNo----Name-----------Date/Time
1----- 101-------Paula-------2012/07/16 08:00:00
2----- 101-------Paula-------2012/07/16 12:15:00
3----- 101-------Paula-------2012/07/16 12:44:00
4----- 101-------Paula-------2012/07/16 18:00:00
5----- 133-------Latina------2012/08/16 08:30:00
6----- 133-------Latina------2012/08/16 12:25:00
7----- 133-------Latina------2012/08/16 12:49:00
8----- 133-------Latina------2012/08/16 18:30:00




I wish to come up with this result, hope you guys can help me. Thanks

---Table 2---

StudentNo-----Name---------Date----------TimeIn------BrkOut-------BrkIn------TimeOut
101--------Paula------2012/07/16------08:00:00----12:15:00----12:44:00----18:00:00
133-------Latina------2012/08/16------08:30:00----12:25:00----12:49:00----18:30:00



Paula
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-08-01 : 09:32:05
[code]
SELECT StudentNo,Name,DATEADD(dd,DATEDIFF(dd,0,[Date/Time]),0) AS [Date],
MAX(CASE WHEN Rn=1 THEN CONVERT(time,[Date\Time]) END) AS TimeIn,
MAX(CASE WHEN Rn=2 THEN CONVERT(time,[Date\Time]) END) AS BrkOut,
MAX(CASE WHEN Rn=3 THEN CONVERT(time,[Date\Time]) END) AS BrkIn,
MAX(CASE WHEN Rn=4 THEN CONVERT(time,[Date\Time]) END) AS TimeOut
FROM
(
SELECT ROW_NUMBER() OVER (PARTITION BY StudentNo,Name,DATEADD(dd,DATEDIFF(dd,0,[Date/Time]),0) ORDER BY [Date\time]) AS Seq,
*
FROM Table
)t
GROUP BY StudentNo,Name,DATEADD(dd,DATEDIFF(dd,0,[Date/Time]),0)
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -