Showing posts with label score. Show all posts
Showing posts with label score. Show all posts

Wednesday, March 21, 2012

formatting in sql server

hi

below is my query

select * from guest.tbl1

i m getting result like this

id score

1 50
2 100

3 150

4 200

but i want this output like this

1 50 2 100

3 150 4 200

how to do this?

thanx

Where you want to display this record?In a web page or in the SQL server o/p?|||in sql server o/p|||

Code Snippet

CREATE TABLE #TMP1(ID INT IDENTITY (1,1),COL1 INT,COL2 INT)
CREATE TABLE #TMP2(ID INT ,COL3 INT,COL4 INT)

INSERT INTO #TMP1
(
COL1
,COL2
)
SELECT id
,val
FROM tbl1

INSERT INTO #TMP2(ID,COL3
,COL4
)
SELECT ID
,COL1
,COL2 FROM #TMP1 WHERE ID%2=1

SELECT * FROM #TMP1
SELECT * FROM #TMP2
SELECT T2.COL3
,T2.COL4
,T1.COL1
,T1.COL2 FROM #TMP1 T1 JOIN #TMP2 T2 ON T1.ID = T2.ID + 1
GO
DROP TABLE #TMP1
DROP TABLE #TMP2

|||

Afraid to believe the ID value , It may have gaps rite?

Code Snippet

Create Table #data (

[id] int ,

[score] Varchar(100)

);

Insert Into #data Values('1','50');

Insert Into #data Values('2','100');

Insert Into #data Values('5','150');

Insert Into #data Values('6','200');

Insert Into #data Values('9','200');

Insert Into #data Values('10','200');

If you use SQL Server 2005,

Code Snippet

;WITH CTE

as

(

Select

*,

Row_Number() Over(Order By ID) RID

From

#data

)

Select up.id,up.score,dwn.id,dwn.score from CTE up

left outer join CTE dwn on up.Rid = dwn.Rid-1

Where

up. RID % 2 = 1

If you use SQL Server 2000,

Code Snippet

Declare @.Table Table(

[id] int ,

[score] Varchar(100) ,

[RID] int identity(1,1)

)

Insert Into @.Table

Select Id,Score From #Data Order By 1;

Select up.id,up.score,dwn.id,dwn.score from @.Table up

left outer join @.Table dwn on up.Rid = dwn.Rid-1

Where

up. RID % 2 = 1