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.
| Author |
Topic |
|
micron009
Starting Member
2 Posts |
Posted - 2012-07-30 : 23:29:48
|
| i have a table like this one..Table1No StudentNo Name Date Time 1 101 Paula 2012/07/16 08:00:00AM1 101 Paula 2012/07/16 12:00:00PM1 101 Paula 2012/07/16 12:50:00PM1 101 Paula 2012/07/16 06:00:00PMand then i want to convert it into table2 like this..Table2No. 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:00PMPlease 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 MVPhttp://visakhm.blogspot.com/ |
 |
|
|
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, DateBut 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. |
 |
|
|
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:59BrkOut = 12:00-12:59BrkIn = 12:00-12:59TimeOut = 13:00-21:00---Table1---No----StudentNo----Name-----------Date/Time1----- 101-------Paula-------2012/07/16 08:00:002----- 101-------Paula-------2012/07/16 12:15:003----- 101-------Paula-------2012/07/16 12:44:004----- 101-------Paula-------2012/07/16 18:00:005----- 133-------Latina------2012/08/16 08:30:006----- 133-------Latina------2012/08/16 12:25:007----- 133-------Latina------2012/08/16 12:49:008----- 133-------Latina------2012/08/16 18:30:00I 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:00Paula |
 |
|
|
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 TimeOutFROM(SELECT ROW_NUMBER() OVER (PARTITION BY StudentNo,Name,DATEADD(dd,DATEDIFF(dd,0,[Date/Time]),0) ORDER BY [Date\time]) AS Seq,*FROM Table)tGROUP BY StudentNo,Name,DATEADD(dd,DATEDIFF(dd,0,[Date/Time]),0)[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|