SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 how transpose data
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

bekeer020
Starting Member

USA
24 Posts

Posted - 09/14/2011 :  11:07:56  Show Profile  Reply with Quote
Hello all
I have table in one query like this:

Stu_ID Subject_Name
---------------------------
1 English
1 Math
1 France
1 Geog
2 France
2 Italy



now i want to show it like this format

Stu_ID Subject1 Subject2 Subject3 Subject4
-----------------------------------------------------------------
1 English Math France Geog
2 France Italy


i want to split data into 4 columns
can i do like this please
note: maximum columns is four

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 09/14/2011 :  11:21:36  Show Profile  Reply with Quote

SELECT Stu_ID,
[1] AS Subject1,
[2] AS Subject2,
[3] AS Subject3,
[4] AS Subject4
FROM (SELECT ROW_NUMBER() OVER (PARTITION BY Stu_ID ORDER BY Subject_name) AS Seq,* FROM Table)t
PIVOT(MAX(Subject_Name) FOR Seq IN ([1],[2],[3],[4]))p


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

bekeer020
Starting Member

USA
24 Posts

Posted - 09/15/2011 :  00:04:47  Show Profile  Reply with Quote
thank you visakhm for your solution but i have large view (from 7 tables) how i add this four columns
my view like this:
------------------
SELECT TOP (100) PERCENT dbo.Class_Registration.Stu_ID, dbo.Students.Full_Nmae, dbo.Classes.Semester_ID, dbo.Training_Product.Training_Product_ID,
dbo.Class_Registration.Class_ID, dbo.Classes.Course_ID, dbo.Courses.Subject_name, dbo.Classes.Center_ID

FROM dbo.Training_Product_Registration INNER JOIN
dbo.Class_Registration INNER JOIN
dbo.Classes ON dbo.Class_Registration.Class_ID = dbo.Classes.Class_ID INNER JOIN
dbo.Courses ON dbo.Classes.Course_ID = dbo.Courses.Course_ID INNER JOIN
dbo.Semester ON dbo.Classes.Semester_ID = dbo.Semester.Semester_ID INNER JOIN
dbo.Students ON dbo.Class_Registration.Stu_ID = dbo.Students.Stu_ID ON
dbo.Training_Product_Registration.Registration_ID = dbo.Class_Registration.Registration_ID INNER JOIN
dbo.Training_Product INNER JOIN
dbo.[Plan] ON dbo.Training_Product.Training_Product_ID = dbo.[Plan].Training_Product_ID ON
dbo.Training_Product_Registration.Plan_ID = dbo.[Plan].Plan_ID INNER JOIN
dbo.View_man ON dbo.Class_Registration.Stu_ID = dbo.View_man.Stu_ID
WHERE (dbo.Classes.Semester_ID = N'20101') AND (dbo.Classes.Center_ID = N'41QAM') AND (dbo.Training_Product.Training_Product_ID = 9)


------------------
note:


also in above query client_ID and Course_Arabic_Name in different table

how can add four columns (subject1,subject2,subject3,subject4)to distribute subject_names for each client if my data like in my query

Edited by - bekeer020 on 09/15/2011 01:09:20
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 09/15/2011 :  01:06:14  Show Profile  Reply with Quote
you want all these columns to appear along with transpose? can you show waht should be your required output with all the columns?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 09/15/2011 :  01:24:17  Show Profile  Reply with Quote
Can you please give requested output format?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

bekeer020
Starting Member

USA
24 Posts

Posted - 09/15/2011 :  01:33:11  Show Profile  Reply with Quote
see i will give you easy example suppose i have this three tables

Student
------
St_ID
St_Name


Data:

1 John
2 Smith
---------------------------------------------------------------

Courses
------
Cu_ID
Cu_Name

Data:

A1 Outlook
A2 Excel
A3 Flash
A4 Access
---------------------------------------------------------------


Class_Register
------
St_ID
CU_ID

Data:
1 A1
1 A2
1 A3
1 A4
2 A2
2 A2
---------------------------------------------------------------

also there is relation between this tables

I want output like this:


St_ID St_Name Subject1 Subject2 Subject3 Subject4
----------------------------------------------------------------------------
1 John Outlook Excel Flash Access
2 Smith Excel Flash

the data of Subject1..4 from Courses table depend on data in class_Register table

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 09/15/2011 :  01:41:54  Show Profile  Reply with Quote
See for the scenario I've given you solution already using PIVOT.
But regarding your actual view my question was whether you need to return any more additional columns other that id,name and 4 subjects as i see lot of other columns also in view definition.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

bekeer020
Starting Member

USA
24 Posts

Posted - 09/15/2011 :  02:01:12  Show Profile  Reply with Quote
can you give me your email i will send sample from my DB

Edited by - bekeer020 on 09/15/2011 02:09:20
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 09/15/2011 :  02:45:35  Show Profile  Reply with Quote
no need. please post your sample output here giving 5 or 10 rows from view

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

bekeer020
Starting Member

USA
24 Posts

Posted - 09/15/2011 :  03:28:20  Show Profile  Reply with Quote


see in the above picture this is my real view the out put three columns

client_id this student id
full_Arab_Name this is student name
no need to this column (Course_Eng_Name) ,I want Subject1 Subject2 Subject3 Subject4 for each Student(client_id)

Edited by - bekeer020 on 09/17/2011 14:55:42
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 09/15/2011 :  03:59:19  Show Profile  Reply with Quote
I cant see the picture. anyways i think it should be something like


SELECT Stu_ID,Full_Nmae,
MAX(CASE WHEN Seq=1 THEN Subject_name ELSE NULL END) AS Subject1,
MAX(CASE WHEN Seq=2 THEN Subject_name ELSE NULL END) AS Subject2,
MAX(CASE WHEN Seq=3 THEN Subject_name ELSE NULL END) AS Subject3,
MAX(CASE WHEN Seq=4 THEN Subject_name ELSE NULL END) AS Subject4
FROM
(
SELECT ROW_NUMBER() OVER (PARTITION BY dbo.Class_Registration.Stu_ID,dbo.Students.Full_Nmae ORDER BY dbo.Courses.Subject_name) AS Seq, dbo.Class_Registration.Stu_ID, dbo.Students.Full_Nmae, dbo.Classes.Semester_ID, dbo.Training_Product.Training_Product_ID, 
dbo.Class_Registration.Class_ID, dbo.Classes.Course_ID, dbo.Courses.Subject_name, dbo.Classes.Center_ID

FROM dbo.Training_Product_Registration INNER JOIN
dbo.Class_Registration INNER JOIN
dbo.Classes ON dbo.Class_Registration.Class_ID = dbo.Classes.Class_ID INNER JOIN
dbo.Courses ON dbo.Classes.Course_ID = dbo.Courses.Course_ID INNER JOIN
dbo.Semester ON dbo.Classes.Semester_ID = dbo.Semester.Semester_ID INNER JOIN
dbo.Students ON dbo.Class_Registration.Stu_ID = dbo.Students.Stu_ID ON 
dbo.Training_Product_Registration.Registration_ID = dbo.Class_Registration.Registration_ID INNER JOIN
dbo.Training_Product INNER JOIN
dbo.[Plan] ON dbo.Training_Product.Training_Product_ID = dbo.[Plan].Training_Product_ID ON 
dbo.Training_Product_Registration.Plan_ID = dbo.[Plan].Plan_ID INNER JOIN
dbo.View_man ON dbo.Class_Registration.Stu_ID = dbo.View_man.Stu_ID
WHERE (dbo.Classes.Semester_ID = N'20101') AND (dbo.Classes.Center_ID = N'41QAM') AND (dbo.Training_Product.Training_Product_ID = 9)

)t
GROUP BY Stu_ID,Full_Nmae


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

bekeer020
Starting Member

USA
24 Posts

Posted - 09/15/2011 :  08:53:11  Show Profile  Reply with Quote
not correct ..


This is my data

There is view "view15" edite it to my goal and give me the code


Edited by - bekeer020 on 09/17/2011 14:53:59
Go to Top of Page

bekeer020
Starting Member

USA
24 Posts

Posted - 09/15/2011 :  22:08:29  Show Profile  Reply with Quote

Can any one help me
----------------------
This is my data

There is view "view15" edite it to my goal and give me the code


Edited by - bekeer020 on 09/17/2011 14:53:35
Go to Top of Page

namman
Constraint Violating Yak Guru

USA
263 Posts

Posted - 09/15/2011 :  23:51:06  Show Profile  Reply with Quote

Student
------
St_ID
St_Name


Data:

1 John
2 Smith
---------------------------------------------------------------

Courses
------
Cu_ID
Cu_Name

Data:

A1 Outlook
A2 Excel
A3 Flash
A4 Access
---------------------------------------------------------------


Class_Register
------
St_ID
CU_ID

Data:
1 A1
1 A2
1 A3
1 A4
2 A2
2 A2
---------------------------------------------------------------

also there is relation between this tables

I want output like this:


St_ID St_Name Subject1 Subject2 Subject3 Subject4
----------------------------------------------------------------------------
1 John Outlook Excel Flash Access
2 Smith Excel Flash


With that example, you can easily solve it, then apply to your real problem.

Join the 3 tables (using cte), then using case, group by, row_number, max to display the desired result.

Go to Top of Page

bekeer020
Starting Member

USA
24 Posts

Posted - 09/17/2011 :  14:58:17  Show Profile  Reply with Quote
Ok I solved Thank you every one for help me
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 09/18/2011 :  03:13:30  Show Profile  Reply with Quote
quote:
Originally posted by bekeer020

Ok I solved Thank you every one for help me

what was change you did to make it work?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.14 seconds. Powered By: Snitz Forums 2000