1

Here is my SQL Query

SELECT p.StudentID, ai.RollNo, p.FirstName, p.MiddleName, p.LastName, 
om.ExamID,  et.ExamName, om.SubjectID,
ISNULL(CONVERT(varchar(20),om.ObtainedMarksTheory), 'A') as 'ObtainedMarksTheory', 
ISNULL(CONVERT(varchar(20),om.ObtainedPracticalMarks),'A') as 'ObtainedPracticalMarks'
FROM Students.PersonalInfo p
INNER JOIN Students.AcademicCourse ac on p.StudentID = ac.StudentID
INNER JOIN Students.AcademicInfo ai on p.StudentID=ai.StudentID
LEFT OUTER JOIN Exam.ObtainedMarkEntry om on p.StudentID = om.StudentID
LEFT JOIN Exam.ExamType et   on om.ExamID = et.ExamID
WHERE ai.BatchID = '103' AND ai.SemesterID = '21' and ac.Section = '8'

This produce result as in a picture:

But I want result like this since those two students were absent in that exam

Similarly if another Exam does exists for any of three student and other are absent same procedure should repeat

3
  • 1
    Can you please explain the logic behind the output your trying to achieve? where are those "default" values coming from? Commented Jul 23, 2015 at 11:15
  • In this particular result set student are enrolled in exam. In above result student with rollno 1 has attended Exam with ExamID 28 but rest of the two have not attended it but the exam was compulsory. So that ExamID, ExamName, SubjectID field should populate to those students too who didn't attended. Rest of two columns with value A indicates absent. Commented Jul 23, 2015 at 11:34
  • Something wrong happened with the images. They too became NULL Commented Jul 23, 2015 at 11:42

2 Answers 2

1

Use IsNULL() Function see below example

Declare @variable varchar(MAX)
set @variable = NULL
select IsNULL(@variable,0) as A

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

I think I simply can't do this. because isnull replace null field with some known static value in this case 0 but In my case I don't know with what value I have to replace that null field. But what I know is; a field which contains value , that value should pass to null fields
@user42392, use ISNULL(ColumnContainedNULL, ColumnWhichReplaceNULLValue)
0

Let's have the following sample data (it is like your sample data, just inserted in one table):

DECLARE @DataSource TABLE
(
    [StudentID] TINYINT
   ,[RowNo] TINYINT
   ,[FirstName] VARCHAR(12)
   ,[MiddleName] VARCHAR(12)
   ,[LastName] VARCHAR(12)
   ,[ExamID] TINYINT
   ,[ExamName] VARCHAR(18)
   ,[SubjectID] TINYINT
   ,[ObtainedMarksTheory] VARCHAR(12)
   ,[ObtainedPracticalMarks] VARCHAR(12)
);

INSERT INTO @DataSource ([StudentID], [RowNo], [FirstName], [MiddleName], [LastName], [ExamID], [ExamName], [SubjectID], [ObtainedMarksTheory], [ObtainedPracticalMarks])
VALUES (101, 1, 'FN_A', 'MN_A', 'LN_A', NULL, NULL, NULL, 'A', 'A')
      ,(102, 2, 'FN_B', 'MN_B', 'LN_B', 28, 'First Tem2072', 97, '74.00', '56.00')
      ,(103, 3, 'FN_C', 'MN_C', 'LN_C', NULL, NULL, NULL, 'A', 'A');

SELECT *
FROM @DataSource;

enter image description here

So, I am supposing we have details only for one exam here and the question is how to replace the NULL values of ExamID, ExamName and SubjectID columns using the existing values.

The solution is to use the MAX function with OVER clause:

SELECT [StudentID]
      ,[RowNo]
      ,[FirstName]
      ,[MiddleName]
      ,[LastName]
      ,MAX([ExamID]) OVER() AS [ExamID]
      ,MAX([ExamName]) OVER() AS [ExamName]
      ,MAX([SubjectID]) OVER() AS [SubjectID]
      ,[ObtainedMarksTheory]
      ,[ObtainedPracticalMarks]
FROM @DataSource;

enter image description here

Note, that it is better to use the OVER clause as you are not going to add GROUP BY clauses in your initial query. Also, if you have details for more exams you can use the PARTITION BY clause (if you have some column(s) to distinguish the NULL rows for each exam).

2 Comments

Ok I will try this in my query. And yes I have to show more exam results too.
@user42392 OK, you need to add to your sample data a column or columns by which to distinguish the exams. Another solution will be to order the results by some columns. Could you edit your question and add such data/output?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.