Solved

Compound SQL Query

Posted on 2009-05-07
11
560 Views
Last Modified: 2012-05-06
I have constructed this SQL query:
SELECT DISTINCT a.uid, a.sid, a.fname, a.lname, b.class_code from student a, class b where b.grade_code = '2' and a.class = b.class_code and a.school_code = 'SES' order by a.class, a.lname, a.fname

My attempt is to pick up all students in grade 2 for school SES. The grade field is not contained in the student table, rather the class field links to class_code in the class table.

The attached files are (File1), content of table class for all grade 2 classes, (File2) all students who are in grade 2 classes, (File3), partial result.

File4 is the structure of table student. File 5 is the structure of table class.

Note that class 31 in in grade 3, not grade 2.

What's wrong?
File1.jpg
File2.jpg
File3.jpg
File4.jpg
File5.jpg
0
Comment
Question by:Richard Korts
  • 6
  • 3
  • 2
11 Comments
 
LVL 15

Expert Comment

by:DonKronos
ID: 24331276
SELECT DISTINCT a.uid, a.sid, a.fname, a.lname, b.class_code
from student a
inner join class b
on a.class = b.class_code and a.school_code = 'SES' and b.grade_code = '2'
order by a.class, a.lname, a.fname
0
 

Author Comment

by:Richard Korts
ID: 24331445
To DonKronos:

It still picks up students in class 31 (a 3rd grade class).

See attached file (File6)
File6.jpg
0
 
LVL 40

Expert Comment

by:Sharath
ID: 24331616

Can you run this and post the result.
select distinct class_code  

  from class 

 where grade_code = '2' and class_code in (select class from student where school_code = 'SES')

Open in new window

0
 

Author Comment

by:Richard Korts
ID: 24331663
To Sharath_123:

It produces the attached File7 (error).
File7.jpg
0
 
LVL 15

Expert Comment

by:DonKronos
ID: 24331669
Hmmm  ...  doesn't seem logical.

Look in the class table and see what the grade_code is for class_code 31.  I have a suspicion you will find grade_code = 2 for class_code = 31.

If that is not the case then I have to figure out how SQL server is joining rows that do not meet the specified criteria?
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:Richard Korts
ID: 24331720
To all:

I did what DonKronos suggested (I had already confirmed that before I posted the original question). See attached (File 8).

I have the sinking feeling that it has to do with the fact that the name "class" is used as a column name in the table student and as a table name. I say this because I revised the original DonKronos query to eliminate the aliases & it falid saying something to the effect that "class" was not unique.

I hope I don't have to change the column name, it must be in 30 or 40 php scripts. Or the table name (even worse)
File8.jpg
0
 
LVL 40

Expert Comment

by:Sharath
ID: 24331774

What i am trying to do is to know all the class_codes for school_code = 'SES' and grade_code = '2'
If the class code 31 has the grade_code = '2', then your query will return the records for that class code also.
select class_code  

  from class 

 where grade_code = '2' and class_code in (select class from student where school_code = 'SES')

 group by class_code
 

or
 

select count(class_code)

  from class 

 where grade_code = '2' and class_code in (21,22,31)

Open in new window

0
 
LVL 40

Accepted Solution

by:
Sharath earned 500 total points
ID: 24331780
pls run this
select class_code

  from class 

 where grade_code = '2' and class_code = 31

Open in new window

0
 

Author Comment

by:Richard Korts
ID: 24331826
To Sharath_123:

I ran this query:

SELECT * from class where school_code = 'SES' and grade_code = '2'.

It produces the arrached File9. This says to me that there are EXACTLY 2 rows in the table that meet this criteria and the row with class_code = 31 is NOT one of them.
File9.jpg
0
 

Author Comment

by:Richard Korts
ID: 24331863
To To Sharath_123:

I ran this query:

Select * from class where class_code = 31 and grade_code = '2". I got the attached. But note that the school_code is NOT 'SES'.

Ah, I think I see the solution. I have to further qualify b (class) that b.school_code = 'SES'. I'll try that.
File10.jpg
0
 

Author Closing Comment

by:Richard Korts
ID: 31579244
The real solution is as I indicated in my last message, I had to add the FURTHER qualification "... and b.school_code = 'SES' ......"

You were most helpful & you led me in the right direction, so you get the points.
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now