473,804 Members | 2,277 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Strange JOIN query problem

I have to produce a query to obtain a single record, however, the
tables I work with have a very strange relationship:

Table: student
Fields:
id
first_name
last_name
email
school_type_id (type of school)
school_type_oth er
school_year_id (their year in school)
school_year_oth er
student_enrollm ent_status_id (if they're enrollment full time, part
time, whatever)
student_enrollm ent_status_othe r
unique_key

the "_other" fields contain text the student enters in lieu of not
entering anything that would produce IDs in the "_id" fields. Like
this:

Table: school_year
Fields
id school_year_nam e
1 freshman
2 sophomore
3 junior
4 senior

And instead of choosing any of the above, the student enters "King of
the World" in the "_other" text field and thus populating the
"school_year_ot her" field instead of "school_year_id " (which becomes 0
in this case).

So your student record could look like this:

id school_type_id school_type_oth er school_year_id
school_year_oth er
1 4 NULL 2
NULL

Or it could look like this:

id school_type_id school_type_oth er school_year_id
school_year_oth er
1 0 merchant marine 0
pfc

Or any combination of these six fields!

Based on what you see so far, how would you produce a unique query of
one row, knowing that you are grabbing the data blindly, of course,
what on earth do you do? If I do this:

1) SELECT .. FROM student s, school_year y WHERE s.school_year_i d =
y.id
(etc.)
Then you might be 0 records if s.school_year = 0

BUT

If I do this:

1) SELECT .. FROM student s, school_year y WHERE s.school_year_i d NOT
IN (SELECT id from school_year) AND s.school_year_o ther IS NOT NULL AND
s.school_year_o ther != ''
Then I get a Cartesian product!!

Basically, I really need help on this and quickly as I have a
presentation in a week and I can't for the life of me figure this query
out (NO DBA's available!)

Thanx
Phil

Jan 27 '06 #1
3 1358
<ph************ **@gmail.com> wrote in message
news:11******** *************@g 47g2000cwa.goog legroups.com...
. . . Based on what you see so far, how would you produce a unique query
of
one row, knowing that you are grabbing the data blindly, of course,
what on earth do you do?


Here's an example to handle the school year field:

SELECT IF(s.school_yea r_id = 0, s.school_year_o ther, y.school_year) AS
school_year
FROM student AS s
LEFT OUTER JOIN school_year AS y ON s.school_year_i d = y.id

The outer join ensures it gets the row from s even if there is no matching
row in y.
The IF() function in the select-list chooses the _other label if the _id is
0, otherwise returns the label from y.

Regards,
Bill K.
Jan 27 '06 #2
I'm sorry, but the query fails if school_year_id = 0. It still
produces an empty set, even though the record exists with
school_year_id = 0.

Phil

Bill Karwin wrote:
<ph************ **@gmail.com> wrote in message
news:11******** *************@g 47g2000cwa.goog legroups.com...
. . . Based on what you see so far, how would you produce a unique query
of
one row, knowing that you are grabbing the data blindly, of course,
what on earth do you do?


Here's an example to handle the school year field:

SELECT IF(s.school_yea r_id = 0, s.school_year_o ther, y.school_year) AS
school_year
FROM student AS s
LEFT OUTER JOIN school_year AS y ON s.school_year_i d = y.id

The outer join ensures it gets the row from s even if there is no matching
row in y.
The IF() function in the select-list chooses the _other label if the _id is
0, otherwise returns the label from y.

Regards,
Bill K.


Jan 27 '06 #3
Ok I got it to work! I had some extraneous JOINS in there by mistake,
THANX!!

Phil

Jan 27 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
2074
by: Dave | last post by:
Hi all, I've been trying to figure this out for the last day and a half and it has me stumped. I've got a web application that I wrote that keeps track of trading cards I own, and I'm moving it from an Access 2000 database to a SQL Server 2000 database. Everything worked perfectly in Access, but I'm having trouble getting data to display in SQL Server. For reference, here's the query. It's big and nasty, but I thought
0
2368
by: Soefara | last post by:
Dear Sirs, I am experiencing strange results when trying to optimize a LEFT JOIN on 3 tables using MySQL. Given 3 tables A, B, C such as the following: create table A ( uniqueId int not null default 0 auto_increment, a1 varchar(64) not null default '',
3
2541
by: Gary Besta | last post by:
I am trying to add a simple case statement to a stored procedure or user defined function. However when I try and save the function/procedure I get 2 syntax errors. Running the query in query analyser works fine and a result is given with no syntax errors. I believe its something to do with the spaces in the field names. Not my choice as its an existing system I have to work around. Any help greatly appreciated SQL Query
4
2197
by: Andrei Ivanov | last post by:
Hello, I have 2 tables: CREATE TABLE products ( id SERIAL PRIMARY KEY, name VARCHAR(255) NOT NULL ); CREATE TABLE products_daily_compacted_views ( product INTEGER NOT NULL REFERENCES products,
16
1578
by: Justin Koivisto | last post by:
I am trying to create a query to use as a report record source. Below is what I want to do (this was tested and works with a MySQL web script): SELECT contacts.id, contacts.email, contacts.first_name, contacts.last_name, contacts.middle_name, contacts.company,
2
1310
by: Andrew | last post by:
hi, here is my code : szQry = "SELECT SubCategory_ID , " & _ " Loc1.Description as Descrizione " & _ " FROM C_SubCategories " & _ " LEFT OUTER JOIN D_Localizations Loc1 " & _ " ON ( C_SubCategories.Description_ID = Loc1.Localization_ID AND Loc1.Language_ID = @LanguageID ) " & _ " WHERE Category_ID = @CategoryID " & _ " AND StateActive = 1 " & _
2
1484
by: veramente | last post by:
Hello all, I have the following query that has a problem i cannot resolve: SELECT puzzle_picking.*, tmagaztestate.mconto, tanagraficagen.araso, tmagaztestate.mdabol, tcausalimagaz.tdescr FROM tcausalimagaz RIGHT JOIN (tanagraficagen RIGHT JOIN (tmagazrighe LEFT JOIN tmagaztestate ON (tmagazrighe.mnubol = tmagaztestate.mnubol) AND (tmagazrighe.mspecie = tmagaztestate.mspecie) AND (tmagazrighe.manno = tmagaztestate.manno) AND...
8
2531
by: Richard | last post by:
Hello! I have this piece of SQL code: UPDATE a SET Field1 = c.Field1 FROM a INNER JOIN b ON a.GUID1 = b.GUID1 INNER JOIN c ON b.GUID2 = c.GUID2 WHERE c.Type = 1
4
1131
prn
by: prn | last post by:
Hi Folks, Sorry if I have the forum wrong, It's more a SQL question than a SQL Server question, but it is intended to run on MS SQL Server at least. :) I'm trying to construct a query and am having trouble wrapping my head around this so I'm hoping that someone can help me get it straight. Here's a stripped-down version of the problem. There are 3 tables involved. (Actually 4, but I'm going to bypass one of them.) We have a table of...
0
9714
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9594
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10599
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10090
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9173
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7635
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6863
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4308
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.