Author |
Topic |
shriroshanraj
Starting Member
30 Posts |
Posted - 2013-07-16 : 07:51:04
|
DECLARE @AssignmentList varchar(3000),@Sql varchar(max)SET @AssignmentList =STUFF((SELECT DISTINCT ',[' + assignmentname + ']' FROM assignment where coursesectionid='b78a6efe-ac77-4e49-806a-fc2fad71068b' FOR XML PATH('')),1,1,'')SET @Sql ='select *from(select p.firstname +p.lastname as StudentName,a.assignmentname,cast((asg.pointsawarded+''/''+a.pointspossible )as int )as Marksfrom person2 pinner join coursesectionroster cson cs.personid = p.personid inner join assignment aon a.coursesectionid = cs.coursesectionidleft join assignmentsubmission asgon a.assignmentid=asg.assignmentidwhere cs.coursesectionid=''b78a6efe-ac77-4e49-806a-fc2fad71068b''and cs.courserole=2)tPIVOT(SUM(Marks) FOR assignmentname IN(' + @AssignmentList + '))p'execute(@sql) |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2013-07-16 : 08:05:33
|
You are using integer division. Try thisDECLARE @AssignmentList VARCHAR(3000), @Sql VARCHAR(MAX);SET @AssignmentList = STUFF((SELECT DISTINCT ',' + QUOTENAME(AssignmentName) FROM dbo.Assignment WHERE CourseSectionID = 'b78a6efe-ac77-4e49-806a-fc2fad71068b' ORDER BY ',' + QUOTENAME(AssignmentName) FOR XML PATH('')), 1, 1, '');SET @Sql = 'SELECT * from ( SELECT p.FirstName + p.LastName AS StudentName, a.AssignmentName, CAST(100E * asg.PointsAwarded / a.PointsPossible) AS INT) AS Marks FROM dbo.Person2 AS p INNER JOIN dbo.CourseSectionRoster AS cs ON cs.PersonID = p.PersonID INNER JOIN dbo.Assignment AS a ON a.CourseSectionID = cs.CourseSectionID LEFT JOIN dbo.AssignmentSubmission AS asg ON asg.AssignmentID = a.AssignmentID WHERE cs.CoursesectionID = ''b78a6efe-ac77-4e49-806a-fc2fad71068b'' AND cs.CourseRole = 2 ) AS t PIVOT ( SUM(Marks) FOR AssignmentName IN (' + @AssignmentList + ') ) AS p';EXEC (@Sql); N 56°04'39.26"E 12°55'05.63" |
|
|
shriroshanraj
Starting Member
30 Posts |
Posted - 2013-07-16 : 08:15:00
|
it is not integer division. I need to display slash between two integers. the output should be 10/20 |
|
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-07-16 : 08:26:02
|
quote: Originally posted by shriroshanraj it is not integer division. I need to display slash between two integers. the output should be 10/20
If you want to display the slash, then the data type cannot be INT when you compute Marks column. Make it VARCHAR or another character type. But then, you cannot add them up in the PIVOT clause. |
|
|
shriroshanraj
Starting Member
30 Posts |
Posted - 2013-07-18 : 02:02:16
|
if i need slash there, how do i change the query |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-07-18 : 02:06:41
|
[code]DECLARE @AssignmentList varchar(3000),@Sql varchar(max)SET @AssignmentList =STUFF((SELECT DISTINCT ',[' + assignmentname + ']' FROM assignment where coursesectionid='b78a6efe-ac77-4e49-806a-fc2fad71068b' FOR XML PATH('')),1,1,'')SET @Sql ='select *from(select p.firstname +p.lastname as StudentName,a.assignmentname,cast((asg.pointsawarded+''/''+a.pointspossible )as varchar(20))as Marksfrom person2 pinner join coursesectionroster cson cs.personid = p.personid inner join assignment aon a.coursesectionid = cs.coursesectionidleft join assignmentsubmission asgon a.assignmentid=asg.assignmentidwhere cs.coursesectionid=''b78a6efe-ac77-4e49-806a-fc2fad71068b''and cs.courserole=2)tPIVOT(SUM(Marks) FOR assignmentname IN(' + @AssignmentList + '))p'execute(@sql)[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
shriroshanraj
Starting Member
30 Posts |
Posted - 2013-07-18 : 02:11:03
|
i am getting error Operand data type varchar is invalid for sum operator. |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2013-07-18 : 02:12:57
|
[code]DECLARE @AssignmentList VARCHAR(3000), @Sql VARCHAR(MAX);SET @AssignmentList = STUFF((SELECT DISTINCT ',' + QUOTENAME(AssignmentName) FROM dbo.Assignment WHERE CourseSectionID = 'b78a6efe-ac77-4e49-806a-fc2fad71068b' ORDER BY ',' + QUOTENAME(AssignmentName) FOR XML PATH('')), 1, 1, '');SET @Sql = 'SELECT * from ( SELECT p.FirstName + p.LastName AS StudentName, a.AssignmentName, CAST(asg.PointsAwarded AS VARCHAR(12)) + ''/'' + CAST(a.PointsPossible AS VARCHAR(12)) AS Marks FROM dbo.Person2 AS p INNER JOIN dbo.CourseSectionRoster AS cs ON cs.PersonID = p.PersonID INNER JOIN dbo.Assignment AS a ON a.CourseSectionID = cs.CourseSectionID LEFT JOIN dbo.AssignmentSubmission AS asg ON asg.AssignmentID = a.AssignmentID WHERE cs.CoursesectionID = ''b78a6efe-ac77-4e49-806a-fc2fad71068b'' AND cs.CourseRole = 2 ) AS t PIVOT ( MAX(Marks) FOR AssignmentName IN (' + @AssignmentList + ') ) AS p';EXEC (@Sql);[/code] N 56°04'39.26"E 12°55'05.63" |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-07-18 : 02:17:29
|
quote: Originally posted by shriroshanraj i am getting error Operand data type varchar is invalid for sum operator.
Oh Yes..You need to do cast before concatenation. Just didnt realize that when I posted.Please use latest posted suggestion by Swepeso------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
shriroshanraj
Starting Member
30 Posts |
Posted - 2013-07-18 : 02:21:04
|
only PointsAwarded is displaying , but i need to display PointsAwarded / PointsPossible under assignmentname |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-07-18 : 02:30:45
|
quote: Originally posted by shriroshanraj only PointsAwarded is displaying , but i need to display PointsAwarded / PointsPossible under assignmentname
Does PointsPossible have valid values?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
shriroshanraj
Starting Member
30 Posts |
Posted - 2013-07-18 : 02:35:19
|
s PointsPossible has valid values |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-07-18 : 02:57:18
|
Run this query first and post back resultSELECT TOP 10 asg.PointsAwarded,a.PointsPossible , CAST(asg.PointsAwarded AS VARCHAR(12)) + ''/'' + CAST(a.PointsPossible AS VARCHAR(12)) AS Marks FROM dbo.Person2 AS p INNER JOIN dbo.CourseSectionRoster AS cs ON cs.PersonID = p.PersonID INNER JOIN dbo.Assignment AS a ON a.CourseSectionID = cs.CourseSectionID LEFT JOIN dbo.AssignmentSubmission AS asg ON asg.AssignmentID = a.AssignmentID WHERE cs.CoursesectionID = 'b78a6efe-ac77-4e49-806a-fc2fad71068b' AND cs.CourseRole = 2 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
shriroshanraj
Starting Member
30 Posts |
Posted - 2013-07-18 : 03:05:58
|
i changed like this, bcoz it shows errorCAST(asg.PointsAwarded AS VARCHAR(12)) + '/' + CAST(a.PointsPossible AS VARCHAR(12)) AS Marksmarks displays null value.in my db PointsAwarded has null value. PointsPossible has value. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-07-18 : 03:10:16
|
quote: Originally posted by shriroshanraj i changed like this, bcoz it shows errorCAST(asg.PointsAwarded AS VARCHAR(12)) + '/' + CAST(a.PointsPossible AS VARCHAR(12)) AS Marksmarks displays null value.in my db PointsAwarded has null value. PointsPossible has value.
COALESCE(CAST(asg.PointsAwarded AS VARCHAR(12)) + '/','') + CAST(a.PointsPossible AS VARCHAR(12)) AS Marks ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
shriroshanraj
Starting Member
30 Posts |
Posted - 2013-07-18 : 03:13:19
|
i changed like this ISNULL(CAST(asg.PointsAwarded AS VARCHAR(12)) ,0) +'/' + CAST(a.PointsPossible AS VARCHAR(12)) AS MarksMarks displays 0/12 |
|
|
shriroshanraj
Starting Member
30 Posts |
Posted - 2013-07-18 : 03:14:57
|
If i use this COALESCE(CAST(asg.PointsAwarded AS VARCHAR(12)) + '/','') + CAST(a.PointsPossible AS VARCHAR(12)) AS Marksi get only PointsPossible value. |
|
|
shriroshanraj
Starting Member
30 Posts |
Posted - 2013-07-18 : 03:20:14
|
from the below query , i need to display the total number of pointswarded/total number of pointspossible under totalpoints.how to get the totalpoints. the output should beStudentName Totalpoints network testing createnew newtest Raj Kumar 0/105 0/20 0/30 0/30 0/25 DECLARE @AssignmentList VARCHAR(3000), @Sql VARCHAR(MAX);SET @AssignmentList = STUFF((SELECT DISTINCT ',' + QUOTENAME(AssignmentName) FROM dbo.Assignment WHERE CourseSectionID = 'b78a6efe-ac77-4e49-806a-fc2fad71068b' ORDER BY ',' + QUOTENAME(AssignmentName) FOR XML PATH('')), 1, 1, '');SET @Sql = 'SELECT * from ( SELECT p.FirstName + p.LastName AS StudentName, a.AssignmentName, ISNULL(CAST(asg.PointsAwarded AS VARCHAR(12)) ,0) +''/'' + CAST(a.PointsPossible AS VARCHAR(12)) AS Marks FROM dbo.Person2 AS p INNER JOIN dbo.CourseSectionRoster AS cs ON cs.PersonID = p.PersonID INNER JOIN dbo.Assignment AS a ON a.CourseSectionID = cs.CourseSectionID LEFT JOIN dbo.AssignmentSubmission AS asg ON asg.AssignmentID = a.AssignmentID WHERE cs.CoursesectionID = ''b78a6efe-ac77-4e49-806a-fc2fad71068b'' AND cs.CourseRole = 2 ) AS t PIVOT ( MAX(Marks) FOR AssignmentName IN (' + @AssignmentList + ') ) AS p';EXEC (@Sql); |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-07-18 : 04:31:06
|
[code]DECLARE @AssignmentList VARCHAR(3000),@Sql VARCHAR(MAX);SET @AssignmentList = STUFF((SELECT DISTINCT ',' + QUOTENAME(AssignmentName) FROM dbo.Assignment WHERE CourseSectionID = 'b78a6efe-ac77-4e49-806a-fc2fad71068b' ORDER BY ',' + QUOTENAME(AssignmentName) FOR XML PATH('')), 1, 1, '');SET @Sql = 'SELECT *from (SELECT p.FirstName + p.LastName AS StudentName,a.AssignmentName,ISNULL(CAST(asg.PointsAwarded AS VARCHAR(12)) ,0) +''/'' + CAST(a.PointsPossible AS VARCHAR(12)) AS Marks,ISNULL(CAST(SUM(asg.PointsAwarded) OVER (PARTITION BY p.FirstName,p.LastName) AS VARCHAR(12)) ,0) +''/'' + CAST(SUM(a.PointsPossible) OVER (PARTITION BY p.FirstName,p.LastName) AS VARCHAR(12)) AS TotalMarksFROM dbo.Person2 AS pINNER JOIN dbo.CourseSectionRoster AS cs ON cs.PersonID = p.PersonID INNER JOIN dbo.Assignment AS a ON a.CourseSectionID = cs.CourseSectionIDLEFT JOIN dbo.AssignmentSubmission AS asg ON asg.AssignmentID = a.AssignmentIDWHERE cs.CoursesectionID = ''b78a6efe-ac77-4e49-806a-fc2fad71068b''AND cs.CourseRole = 2) AS tPIVOT (MAX(Marks)FOR AssignmentName IN (' + @AssignmentList + ')) AS p';EXEC (@Sql);[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
|