niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Databases » Oracle
'SELECT MAX' subquery problem
Post new topic   Reply to topic Page 1 of 1 [3 Posts] View previous topic :: View next topic
Author Message
Andyza
*nix forums beginner


Joined: 17 Feb 2005
Posts: 5

PostPosted: Thu Feb 17, 2005 9:56 am    Post subject: 'SELECT MAX' subquery problem Reply with quote

My Oracle 9i db table structure is:

Members (sysid(PK), classid, personalid, firstname, surname,
dateofbirth, deleted)

Education (qualificationid(PK), personalid(FK), qualificationtype,
qualificationtitle, yearobtained)

Terminations (terminationid(PK), sysid(FK), contractstatus, statusdate)

I'm trying to select all the members who are assigned to a particular
class (12345) and who have not been removed from that class (I keep a
record of students that are removed from each class). The query below
returns 0 results when there should be at least 3 (I set up the test
data so that there would be a number of 'removed' and 'active'
students).

SELECT m.sysid, m.classid, m.personalid, m.firstname, m.surname,
m.dateofbirth, e.qualificationtype, e.qualificationtitle,
e.yearobtained, t.contractstatus,
TO_CHAR (t.statusdate, 'YYYY/MM/DD') AS statusdate
FROM members m,
education e,
terminations t
WHERE (m.deleted = 'No')
AND ( (m.personalid = e.personalid(+))
AND (m.sysid = t.sysid(+))
AND (t.contractstatus <> 'Removed')
AND (m.classid = '12345')
AND (t.statusdate = (SELECT MAX (t.statusdate)
FROM terminations t
WHERE t.sysid = m.sysid))
)
GROUP BY m.sysid, m.classid, m.personalid, m.firstname, m.surname,
m.dateofbirth, e.qualificationtype,
e.qualificationtitle, e.yearobtained,
t.contractstatus, t.statusdate
ORDER BY LOWER(m.surname) ASC, LOWER(m.firstname) ASC

The 'terminations' table does not contain any records for a student
until he/she is 'removed' from the class or 'reassigned' to the class.
All students are initially automatically assigned to the class and are
members of just this one class.

I think the problem is with the 'SELECT MAX' subquery, but what?
Back to top
Rob Abrahams
*nix forums beginner


Joined: 12 Feb 2005
Posts: 2

PostPosted: Thu Feb 17, 2005 7:57 pm    Post subject: Re: 'SELECT MAX' subquery problem Reply with quote

When using a outerjoin, then all referenced fields in the where-clause has
to have (+). I think the t.statusdate will give you now no rows and perhaps
the t.contractstatus aswell.

greetings, Rob

"Andyza" <andyza@webmail.co.za> schreef in bericht
news:1108637812.430640.153190@g14g2000cwa.googlegroups.com...
Quote:
My Oracle 9i db table structure is:

Members (sysid(PK), classid, personalid, firstname, surname,
dateofbirth, deleted)

Education (qualificationid(PK), personalid(FK), qualificationtype,
qualificationtitle, yearobtained)

Terminations (terminationid(PK), sysid(FK), contractstatus, statusdate)

I'm trying to select all the members who are assigned to a particular
class (12345) and who have not been removed from that class (I keep a
record of students that are removed from each class). The query below
returns 0 results when there should be at least 3 (I set up the test
data so that there would be a number of 'removed' and 'active'
students).

SELECT m.sysid, m.classid, m.personalid, m.firstname, m.surname,
m.dateofbirth, e.qualificationtype, e.qualificationtitle,
e.yearobtained, t.contractstatus,
TO_CHAR (t.statusdate, 'YYYY/MM/DD') AS statusdate
FROM members m,
education e,
terminations t
WHERE (m.deleted = 'No')
AND ( (m.personalid = e.personalid(+))
AND (m.sysid = t.sysid(+))
AND (t.contractstatus <> 'Removed')
AND (m.classid = '12345')
AND (t.statusdate = (SELECT MAX (t.statusdate)
FROM terminations t
WHERE t.sysid = m.sysid))
)
GROUP BY m.sysid, m.classid, m.personalid, m.firstname, m.surname,
m.dateofbirth, e.qualificationtype,
e.qualificationtitle, e.yearobtained,
t.contractstatus, t.statusdate
ORDER BY LOWER(m.surname) ASC, LOWER(m.firstname) ASC

The 'terminations' table does not contain any records for a student
until he/she is 'removed' from the class or 'reassigned' to the class.
All students are initially automatically assigned to the class and are
members of just this one class.

I think the problem is with the 'SELECT MAX' subquery, but what?
Back to top
rogergorden@gmail.com
*nix forums beginner


Joined: 21 Jun 2005
Posts: 14

PostPosted: Mon Feb 28, 2005 2:31 pm    Post subject: Re: 'SELECT MAX' subquery problem Reply with quote

Just a thought:

Oracle9i supports ANSI standard join so feel free to use LEFT OUTER or
RIGHT OUTER or FULL in your query syntax.

Roger Gorden
Senior DBA
Viewpoint Corp
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [3 Posts] View previous topic :: View next topic
The time now is Thu Jan 08, 2009 5:12 am | All times are GMT
navigation Forum index » Databases » Oracle
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts Unknown in header problem -SOLVED- Light Speed Postfix 0 Thu Jul 03, 2008 10:40 am
No new posts problem with sending mail nuxia Postfix 0 Mon Apr 21, 2008 3:58 am
No new posts Postfix 2.3.8 Virtual problem Blotto Postfix 0 Fri Apr 04, 2008 6:11 am
No new posts Select statement Shamna Sybase 0 Mon Sep 17, 2007 6:03 am
No new posts Postfix sending problem for local domain remote email monkey_magix Postfix 0 Mon Sep 10, 2007 10:17 am

Problem Mortgage | Flights | Libros Electronicos | Capital One Credit Cards | Loans
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.2528s ][ Queries: 16 (0.1793s) ][ GZIP on - Debug on ]