Click here to Skip to main content
15,906,645 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
insert into attn values('12334','12-02-2013',09.10)
insert into attn values('12334','12-02-2013',10.10)
insert into attn values('12334','12-02-2013',11.10)

insert into attn values('32334','12-02-2013',09.11)
insert into attn values('32334','12-02-2013',11.20)
insert into attn values('32334','12-02-2013',14.30)
insert into attn values('32334','12-02-2013',17.10)

insert into attn values('32430','12-02-2013',10.11)


I want in this Format

Output :
Staff_no      ADate            InTime      OutTime
'12334'     '12-02-2013'        09.10      10.10
'12334'     '12-02-2013'        11.10      -

'32334'     '12-02-2013'        09.11      11.20
'32334'     '12-02-2013'        14.30      17.10

'32430'     '12-02-2013'        10.11      -
Posted
Updated 6-Mar-13 2:28am
v2
Comments
Prakash Thirumoorthy 25-Feb-13 2:19am    
do u have any flag in your table for identify that in-time or out-time?
Sergey Alexandrovich Kryukov 11-Mar-13 13:51pm    
Please stop posting non-answers as "solution". It can give you abuse reports which eventually may lead to cancellation of your CodeProject membership.
Comment on any posts, reply to available comments, or use "Improve question" (above).
Also, keep in mind that members only get notifications on the post sent in reply to there posts.
—SA

hi,

see the below query


SQL
create table temp (Staff_no numeric(10,0), ADate varchar(50), tme varchar(50), flag varchar(1))


insert into temp values('12334','12-02-2013',09.10,'I')
insert into temp values('12334','12-02-2013',10.10,'O')
insert into temp values('12334','12-02-2013',11.10,'I')

insert into temp values('32334','12-02-2013',09.11,'I')
insert into temp values('32334','12-02-2013',11.20,'O')
insert into temp values('32334','12-02-2013',14.30,'I')
insert into temp values('32334','12-02-2013',17.10,'O')

insert into temp values('32430','12-02-2013',10.11,'I')

Select Staff_no,ADate, isnull(case when flag='I' then tme end,'') 'Intime' ,isnull(case when flag='O' then tme end,'') 'OutTime' from temp
 
Share this answer
 
Comments
Member 1233140 25-Feb-13 2:45am    
I am not having the flag varchar(1))
Prakash Thirumoorthy 25-Feb-13 3:26am    
then how do you identify that in time or out time?
Let me know if it is not working.
SQL
create table attn(id varchar(10), Dt datetime, InOut varchar(10));
insert into attn values('12334','12-02-2013',09.10)
insert into attn values('12334','12-02-2013',10.10)
insert into attn values('12334','12-02-2013',11.10)
 
insert into attn values('32334','12-02-2013',09.11)
insert into attn values('32334','12-02-2013',11.20)
insert into attn values('32334','12-02-2013',14.30)
insert into attn values('32334','12-02-2013',17.10) 
insert into attn values('32430','12-02-2013',10.11)
GO
Select Id, Dt, isnull(case when Status='I' then InOut end,'') 'Intime' ,isnull(case when Status='O' then InOut end,'') 'OutTime' from 
(
select * from (select row_number() over(order by id) as sl, id, Dt, Inout, 'I' as Status from attn) as t
where  (sl % 2 = 1)
union all
select * from (select row_number() over(order by id) as sl, id, Dt, Inout, 'O' as Status FROM attn) as t2 where (sl % 2 = 0)
) 
AS T


Here I assume that Ist row is for in and 2nd for out. Query is going to complecated. You can think redesign your table though query will as simple as possible.
 
Share this answer
 
Comments
Davidduraisamy 6-Mar-13 7:01am    
This is not coming correctly..
Hi ,

to convert Rows into Columns You need to use PIVOT .

please go through the below link

http://blog.sqlauthority.com/2008/06/07/sql-server-pivot-and-unpivot-table-examples[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900