Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Sir, Here is my sql:-


SQL
SELECT   
CONVERT(varchar, COUNT(CASE WHEN studentatt.status = 'P' THEN 1 END) +  ISNULL(MAX(specialAttendance.AttObt), 0) +
ISNULL(dbo.udf_ExtraAttendance(Student.StudentID,CONVERT(DATETIME, '01/Apr/2014', 102),
CONVERT(DATETIME, getdate(), 102)), 0))  as [Present],
CONVERT (varchar, COUNT(StudentAtt.Status) + ISNULL(MAX(specialAttendance.AttMax), 0)) as [Total]
FROM   Student
LEFT OUTER JOIN  specialAttendance  ON Student.StudentID = specialAttendance.StudentID
LEFT OUTER JOIN StudentAtt ON StudentAtt.StudentID = Student.StudentID
WHERE     (StudentAtt.Dated BETWEEN CONVERT(DATETIME, '01/Apr/2014', 102)   AND CONVERT(DATETIME, getdate(), 102))
AND (teach_type = 'L' OR teach_type = 'T'  or teach_type = 'P') AND (StudentAtt.SubjectID IN (2))
GROUP BY  Student.Roll_No, Student.Student, Student.StudentID, Student.CourseID,  Student.BatchID,student.classesid,
specialAttendance.AttObt,  dbo.udf_ExtraAttendance(Student.StudentID, CONVERT(DATETIME, '01/Apr/2014', 102),
CONVERT(DATETIME, getdate(), 102)), Student.Sem         HAVING (Student.CourseID = 1492)   And
(Student.Sem = 5) AND (Student.classesid = 17)


query result is like:-

[Present] -- [Total]
9 -- 20
5 -- 20
6 -- 20
7 -- 20


i need the result like:-

[Present(20)]
9
5
6
7

how can i do it sir.
Posted
Comments
Jörgen Andersson 30-Aug-14 5:57am    
This is a job for the presentation layer, not the query.

1 solution

It is technically possible, although I doubt if it's good practice:

- Get the result for Total into a variable:
SQL
DECLARE @total INT = (SELECT ... FROM ...)

- Create a temporary table using dynamic SQL. Make sure to create a global temp table (using the ## prefix), or it will not be available for the rest of the script
SQL
DECLARE @sql VARCHAR(MAX)

SET @sql = 'CREATE TABLE ##temptable (Present[' + @total + '] INT NOT NULL) ON [PRIMARY]'

EXECUTE (@sql);
GO

- Run your original query and put the results into the temp table
SQL
SELECT .... INTO ##temptable FROM ..........

- Select your results from the temptable:
SQL
SELECT * FROM ##temptable

| Present[20] |
|      9      |
|      5      |
|      6      |
|      7      |
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900