Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
|
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 19th, 2004, 10:49 AM
Authorized User
 
Join Date: Oct 2004
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
Default What is the Query for this

Hi,

my question is as follows:

Student Table:

Student Id Student Name
S1 Ram
S2 Kumar

Subject Table:
Subject Id Subject Name
I1 Maths
I2 Physics

Marks Table:
Subject Id Student Id Marks
I1 S1 100
I1 S2 50
I2 S1 Null
I2 S2 ---
I2 S1 45

Question is:
1. Query to display Student Name,Subject Name, Marks and if Marks is null indicate it “ABSENT” on display orderby Student Name and Subject Name.

2. Query to display Subject Name, Number of Students Passed or Failed considering pass marks as >=50 and considering other than this as Fail.

Expecting for reply.

A.D.Ramkumar
__________________
Ramkumar A D
 
Old December 19th, 2004, 03:32 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 625
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to jemacc
Default

May want to experiment with case, for example:



Code:
SELECT a.studentName,b.subjectname, c.marks,
   Type = 
  CASE 
    WHEN (c.marks) = Null THEN 'Absent'
    WHEN (c.marks) = '---'THEN 'Absent'
  END
FROM marks c JOIN students a ON c.studentID= a.StudentID
Jaime E. Maccou
 
Old December 19th, 2004, 04:30 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 184
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Jonax
Default

1.
Code:
-- I defined the field Marks as Integer, that's the reason for the CAST...
SELECT StudentName, SubjectName, ISNULL(CAST(Marks AS VARCHAR(6)),'Absent') AS Marks
FROM tbStudent INNER JOIN tbMarks
ON tbStudent.StudentId = tbMarks.StudentId
INNER JOIN tbSubject ON tbMarks.SubjectId = tbSubject.SubjectId
2.
Code:
SELECT SubjectName, SUM(Passed) AS Passed, SUM(Failed) AS Failed
FROM tbSubject INNER JOIN (
/*
This query (/derived table) get's the maximum mark achieved by a student in a given subject
and introduces the fields Passed and Failed...
*/
SELECT 
StudentId, 
SubjectId, 
CASE 
    WHEN ISNULL(MAX(Marks), 0) >= 50 THEN 1 
    ELSE 0
END
AS Passed, 
CASE 
    WHEN ISNULL(MAX(Marks), 0) < 50 THEN 1 
    ELSE 0
END
AS Failed

FROM tbMarks
GROUP BY StudentId, SubjectId

) AS tbDerived
ON tbSubject.SubjectId = tbDerived.SubjectId
GROUP BY tbSubject.SubjectName





Similar Threads
Thread Thread Starter Forum Replies Last Post
Output Query to txt file from SQL Query everest SQL Server 2005 4 November 22nd, 2007 01:49 AM
how to make a query from an existing query raport SQL Language 3 November 13th, 2006 08:59 PM
I solved insert query.now see this Update Query. [email protected] VB.NET 2002/2003 Basics 2 September 21st, 2006 12:48 AM
Syntax error in query. Incomplete query clause. dispickle ADO.NET 3 April 16th, 2004 01:04 PM
Error on Make-Table Query In Union Query rylemer Access 1 August 20th, 2003 07:42 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.