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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Query

Author  Topic 

ms65g
Constraint Violating Yak Guru

497 Posts

Posted - 2009-08-07 : 19:49:20
Hi all,

I would like to learn more in T-SQL Querying to this reason I want you to help me to create more solutions.

I have one table with structure and instances under:

CREATE TABLE Course
(
S_No int,
course nvarchar(25)
)

INSERT Course
SELECT 1,'math 1' UNION ALL
SELECT 1,'phisisc 1' UNION ALL
SELECT 2,'math 1' UNION ALL
SELECT 3,'culture' UNION ALL
SELECT 3,'history' UNION ALL
SELECT 3,'art'


S_No course
----------- -------------------------
1 math 1
1 phisisc 1
2 math 1
3 culture
3 history
3 art



Then I want to get the result similar to:


s_no course 1 course 2 course 3
----------- ------------------------- ------------------------- ------------
1 math 1 phisisc 1 NULL
2 math 1 NULL NULL
3 art culture history

-- #1
if object_id('temp') is not null drop table temp
SELECT S_no,
course,
row=ROW_NUMBER() OVER(PARTITION BY S_no ORDER BY course)
INTO temp
FROM Course

SELECT s_no,
[course 1]=(SELECT course FROM temp t
WHERE t.s_no=s.s_no AND t.row=1),
[course 2]=(SELECT course FROM temp t
WHERE t.s_no=s.s_no AND t.row=2),
[course 3]=(SELECT course FROM temp t
WHERE t.s_no=s.s_no AND t.row=3)
FROM (SELECT DISTINCT s_no
FROM temp) s


-- #2
if object_id('temp') is not null drop table temp
SELECT S_no,
course,
row=ROW_NUMBER() OVER(PARTITION BY S_no ORDER BY course)
INTO temp
FROM Course

SELECT [Student Number]=(CASE WHEN D1.S_no IS NOT NULL THEN D1.S_no
ELSE
CASE WHEN D2.S_no IS NOT NULL THEN D2.S_no
ELSE D3.S_no
END
END),
D1.course,
D2.course,
D3.course
FROM
(
SELECT S_no,
course
FROM temp
WHERE row=1
) D1
FULL OUTER JOIN
(
SELECT S_no,
course
FROM temp
where row=2
)D2 ON D1.s_no=D2.S_no
FULL OUTER JOIN
(
SELECT S_no,
course
FROM temp
WHERE row=3
)D3 ON D3.s_no=D2.S_no



Please post other correct queries.
And can be the columns in a query variable?

Vinnie881
Master Smack Fu Yak Hacker

1231 Posts

Posted - 2009-08-07 : 20:12:17
drop table #Course
CREATE TABLE #Course
(
S_No int,
course nvarchar(25)
)

INSERT into #Course
SELECT 1,'math 1' UNION ALL
SELECT 1,'phisisc 1' UNION ALL
SELECT 2,'math 1' UNION ALL
SELECT 3,'culture' UNION ALL
SELECT 3,'history' UNION ALL
SELECT 3,'art'


SELECT S_No, isnull([1],'') as Course1,isnull([2],'') as Course2,isnull([3],'') as Course3
FROM
(Select row_Number() over (partition by S_No order by course) as RowID,*
from #Course) p
PIVOT
(Max(Course)
FOR RowID IN
([1],[2],[3])
) AS pvt


Success is 10% Intelligence, 70% Determination, and 22% Stupidity.
\_/ _/ _/\_/ _/\_/ _/ _/- 881
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-08-08 : 02:17:44
or
http://sqlblogcasts.com/blogs/madhivanan/archive/2008/08/27/dynamic-pivot-in-sql-server-2005.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-08-08 : 07:27:37
try this one

select s_no,max(case when rid = 1 then course end) as course1,
max(case when rid = 2 then course end) as course2,
max(case when rid = 3 then course end) as course3
from (Select row_Number() over (partition by S_No order by course) as RID,*
from #Course) p
group by s_no
Go to Top of Page

ms65g
Constraint Violating Yak Guru

497 Posts

Posted - 2009-08-08 : 07:52:32
Thanks a lot.

Vinnie881: your method is major/master.

madhivanan: your link is useful.

bklr: good attempt but your query has a warning.

Warning: Null value is eliminated by an aggregate or other SET operation.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-08-08 : 09:02:45
The warning is just a warning, because you are trying to take the MAX value of a potential NULL value.


N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page
   

- Advertisement -