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
 Need the Result as explained

Author  Topic 

sachin.hingole
Starting Member

15 Posts

Posted - 2009-01-28 : 06:36:16
Hi everybody,

I have a table as shown below (SQL server 2005)

Table1
Id | FirstName | LastName
1 | FirstName1 | LastName1
2 | FirstName2 | LastName2
3 | FirstName3 | LastName3
4 | FirstName4 | LastName4

always I want to divide the records in 2 groups, Just by dividing row count by 2
and want to show table data as below

Table2
Id | FirstName | LastName | Id1 | FirstName1 | LastName1 |
1 | FirstName1 | LastName1 | 3 | FirstName3 | LastName3 |
2 | FirstName2 | LastName2 | 4 | FirstName4 | LastName4 |


Thanks in advance

Sachin Hingole

Lumbago
Norsk Yak Master

3271 Posts

Posted - 2009-01-28 : 06:40:02
This is a presentation issue...you should do it in the front end.

- Lumbago
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-28 : 06:55:27
[code]select t1.Id,t1.FirstName,t1.Lastname,
t2.Id AS Id1 ,t2.FirstName AS FirstName1,t2.LastName AS LastName1
from table t1
inner join table t2
on t1.Id=t2.Id-1
where t1.id %2>0[/code]
Go to Top of Page

Nageswar9
Aged Yak Warrior

600 Posts

Posted - 2009-01-29 : 03:46:36
Try this also,

declare @temp table (Id int, FirstName varchar(32), LastName varchar(32))
insert into @temp
select 1, 'FirstName1' ,'LastName1' union all
select 2, 'FirstName2' , 'LastName2' union all
select 3, 'FirstName3' , 'LastName3' union all
select 4, 'FirstName4' , 'LastName4'

select t.id,t.firstname,t.lastname,
t1.id as id1,(t1.firstname) firstname1,t1.lastname lastname1 from @temp t
inner join @temp t1 on t.id = t1.id-2
Go to Top of Page

sachin.hingole
Starting Member

15 Posts

Posted - 2009-02-03 : 04:18:33
Thanks for your help, its working fine as per my requirement but there is 1 problem if I have odd number of records for example 3 instead of 4 then I want 2 in first column and remaining in 2 column then how can achive it?

quote:
Originally posted by Nageswar9

Try this also,

declare @temp table (Id int, FirstName varchar(32), LastName varchar(32))
insert into @temp
select 1, 'FirstName1' ,'LastName1' union all
select 2, 'FirstName2' , 'LastName2' union all
select 3, 'FirstName3' , 'LastName3' union all
select 4, 'FirstName4' , 'LastName4'

select t.id,t.firstname,t.lastname,
t1.id as id1,(t1.firstname) firstname1,t1.lastname lastname1 from @temp t
inner join @temp t1 on t.id = t1.id-2




Sachin Hingole
Go to Top of Page

Nageswar9
Aged Yak Warrior

600 Posts

Posted - 2009-02-03 : 04:27:17
Try This Also

declare @temp table (Id int, FirstName varchar(32), LastName varchar(32))
insert into @temp
select 1, 'FirstName1' ,'LastName1' union all
select 2, 'FirstName2' , 'LastName2' union all
select 3, 'FirstName3' , 'LastName3' union all
select 4, 'FirstName4' , 'LastName4' union all
select 5, 'FirstName4' , 'LastName4' union all
select 6, 'FirstName4' , 'LastName4' union all
select 7, 'FirstName4' , 'LastName4'

declare @cnt int
select @cnt = count(*) from @temp

select t.id,t.firstname,t.lastname,
t1.id as id1,(t1.firstname) firstname1,t1.lastname lastname1 from @temp t
left join @temp t1 on t.id = t1.id - case when (@cnt%2) <> 0 then @cnt /2 +1 else @cnt / 2 end
where t.id <= case when (@cnt%2) <> 0 then @cnt /2 +1 else @cnt / 2 end


Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-02-03 : 09:18:32
are you using sql 2005?
Go to Top of Page
   

- Advertisement -