| 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)Table1Id | FirstName | LastName1 | FirstName1 | LastName12 | FirstName2 | LastName23 | FirstName3 | LastName34 | FirstName4 | LastName4always I want to divide the records in 2 groups, Just by dividing row count by 2and want to show table data as belowTable2Id | FirstName | LastName | Id1 | FirstName1 | LastName1 | 1 | FirstName1 | LastName1 | 3 | FirstName3 | LastName3 | 2 | FirstName2 | LastName2 | 4 | FirstName4 | LastName4 | Thanks in advanceSachin 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 |
 |
|
|
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 t1inner join table t2on t1.Id=t2.Id-1where t1.id %2>0[/code] |
 |
|
|
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 @tempselect 1, 'FirstName1' ,'LastName1' union allselect 2, 'FirstName2' , 'LastName2' union allselect 3, 'FirstName3' , 'LastName3' union allselect 4, 'FirstName4' , 'LastName4'select t.id,t.firstname,t.lastname,t1.id as id1,(t1.firstname) firstname1,t1.lastname lastname1 from @temp tinner join @temp t1 on t.id = t1.id-2 |
 |
|
|
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 @tempselect 1, 'FirstName1' ,'LastName1' union allselect 2, 'FirstName2' , 'LastName2' union allselect 3, 'FirstName3' , 'LastName3' union allselect 4, 'FirstName4' , 'LastName4'select t.id,t.firstname,t.lastname,t1.id as id1,(t1.firstname) firstname1,t1.lastname lastname1 from @temp tinner join @temp t1 on t.id = t1.id-2
Sachin Hingole |
 |
|
|
Nageswar9
Aged Yak Warrior
600 Posts |
Posted - 2009-02-03 : 04:27:17
|
Try This Alsodeclare @temp table (Id int, FirstName varchar(32), LastName varchar(32))insert into @tempselect 1, 'FirstName1' ,'LastName1' union allselect 2, 'FirstName2' , 'LastName2' union allselect 3, 'FirstName3' , 'LastName3' union allselect 4, 'FirstName4' , 'LastName4' union allselect 5, 'FirstName4' , 'LastName4' union allselect 6, 'FirstName4' , 'LastName4' union allselect 7, 'FirstName4' , 'LastName4'declare @cnt intselect @cnt = count(*) from @tempselect t.id,t.firstname,t.lastname,t1.id as id1,(t1.firstname) firstname1,t1.lastname lastname1 from @temp tleft join @temp t1 on t.id = t1.id - case when (@cnt%2) <> 0 then @cnt /2 +1 else @cnt / 2 endwhere t.id <= case when (@cnt%2) <> 0 then @cnt /2 +1 else @cnt / 2 end |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-02-03 : 09:18:32
|
| are you using sql 2005? |
 |
|
|
|
|
|