tags:

views:

42

answers:

1

how can i get students grades using these 2 tables?

CREATE TABLE [dbo].[Grade](
 [ID] [int] NULL,
 [From] [int] NULL,
 [To] [int] NULL,
 [Grade] [nvarchar](50) NULL
) ON [PRIMARY]

CREATE TABLE [dbo].[Students]( [ID] [int] NULL, [Name] nvarchar NULL, [Score] [int] NULL ) ON [PRIMARY]

+1  A: 

Ok, lets take a guess that the Students table has a column (probably an identity column) called 'ID' which is the students' unique identifier. Let's also assume that the Grade.ID field is a foreign key reference to the Student table (I know it doesn't say that, but it's probably a good idea.)

In that case, try this (untested code):

SELECT Student.Name, Grade.Grade, Grade.From, Grade.To
FROM Student, Grade
WHERE Student.ID = Grade.ID
ORDER BY Student.ID, Grade.To

That ought to give you a list of students, with their grades in ID order. It will repeat the student name for each grade record however (assuming no null values, of course.)

If you want one student instance with a list of grades, that's significantly harder.

(Btw, the more and better information in the initial question, the better the answers you're likely to get.)


Ok - second shot, now I understand the table structure:

SELECT Students.Name,  (select Grades.GradeName from Grades where Students.Score <= Grades.ScoreTo AND Students.Score > Grades.ScoreFrom) AS Grade
FROM Students

I changed the names a little to make them more obvious and distinct (having the same name for a table and a field on that table is confusing.)

This should give you the results you're looking for. BUT - you must ensure that your data is clean - if you have overlapping ranges, you will get a multiple sub-query return (which will fail.) You can overcome this with a 'SELECT TOP 1' restriction, but it's messy.

Ragster
thanx a lot dear.but its not like that. there is not foreign key.i added 2 tables now.check them and give me a solution
dotnet lover
1 0 39 F2 40 54 S3 55 64 C4 65 74 B5 75 100 AID Name Score2 student1 963 student2 904 student3 165 student4 926 student5 56these are da tables
dotnet lover
Ok, so is there a relationship between the tables? If not, I'm not understanding your question.
Ragster
CREATE TABLE [dbo].[Grade]( [ID] [int] NOT NULL, [From] [int] NOT NULL, [To] [int] NOT NULL, [Grade] [nvarchar](50) NOT NULL, CONSTRAINT [PK_Grade] PRIMARY KEY CLUSTERED ( [ID] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]this is da first table
dotnet lover
CREATE TABLE [dbo].[Students]( [ID] [int] NOT NULL, [Name] [nvarchar](50) NOT NULL, [Score] [int] NOT NULL, CONSTRAINT [PK_Students] PRIMARY KEY CLUSTERED ( [ID] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]this is da second table
dotnet lover
Ok, so two tables, with no obvious relationship - what information is it that you want? (I'm really trying to be helpful here, but I'm not seeing what it is you're after.)
Ragster
i want to get student names with there GradeID From To Grade1 0 39 F2 40 54 S3 55 64 C4 65 74 B5 75 100 A
dotnet lover
in Grade table has a marks ranges. lets say STUDENT1 get 25 marksand Grade table says that from 0 to 39 marks is Fso i want to get da result "STUDENT1 F" like wise for all student table
dotnet lover
Ahh .. ok, so the Grade table holds a grade ID, with a (presumably non-overlapping score range. You want the students' score to be interpreted as a grade. I think I see what you want now.
Ragster
thats rite.that is which i wanted
dotnet lover
Updated the answer. Note the name changes (I did it for clarity for myself.) And note the caveats on the solution.This is a correlated sub-query, and it has risks if the data isn't clean.
Ragster
And I'd also suggest a design change to make it safer. Rather than a from-to - I think it would be better to have a minimum mark system - so you have if your mark is 75 or greater, 'A', otherwise if greater than 65 then 'B', etc. This this can be done:select Students.Name, (select top 1 Grades.GradeName from Grades where Students.Score < Grades.ScoreTo order by Grades.ScoreTo Desc) AS Gradefrom Students
Ragster
Wooooow its working thanx Ragster thanx a lot :)
dotnet lover