473,414 Members | 1,643 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

5-table SELECT giving me problems

New to the forum, and a relatively new with SQL, but until now I've been able to get everything to work just fine. I've spent hours trying to get this select statement to work right, and I've never quite gotten there -- it's probably an easy fix, and I'm just too close to the problem or too new at SQL to find it. This is on SQL Server 2005. Any help would be tremendously appreciated.

I am trying to do a join including 5 tables: one each for customers, equipment, equipment categories, equipment configurations, and configuration items. Here is my current code:

[PHP]SELECT
eq.id, eq.partnum, eq.description, eq.cost,
cat.name, v.name,
i.qty,
cf.name, cf.usr_id
FROM Equipment eq
INNER JOIN Equipment_Categories cat on cat.id = eq.cat_id
INNER JOIN Vendors v on v.id = eq.cust_id
INNER JOIN Equipment_Configurations cf on cf.id = 5
LEFT JOIN Equipment_Configurations_Items i on i.eq_id = eq.id
where i.cf_id = cf.id OR i.qty IS NULL[/PHP]

Our staff is able to create a "configuration" that lists what equipment we need from which vendor, and how many of each item. Each vendor can have multiple pieces of equipment which belong to any existing "category." The SELECT statement above is used to populate a page where they can edit the quantity of each item, but it only parly works.

I successfully retrieve the configuration, complete with name, equipment, vendors, and quantity for all equipment for a given configuration (here, configuration 5). I also retrieve all equipment that has NO quantity in ANY configuration. So if configuration A includes 9 of equipment X, conf. B includes 1 eq. Y, and no configuration has any of eq. Z, when I run this SELECT query to edit configuration A I am only returned 9 x equipment X, 0 x equipment Z -- equipment Y is not in the result set, so I can't have the user specify that they want to add any of that equipment to their configuration.

I've tried different kinds of joins to no avail. Of course, I can select all equipment and include its category and vendor, as well as the configuration name with this simple query:

[PHP]select eq.*, cf.name, v.name, cat.name from equipment eq
inner join equipment_configurations cf on cf.id = 5
inner join vendors v on v.id = eq.vendor_id
inner join equipment_categories cat on cat.id = eq.cat_id[/PHP]

The problem comes when I try to include the quantity of each item currently associated with that configuration. My brain's fried, and nothing I have tried so far works.

Is that all as clear as mud? Hopefully it makes sense to more than my own confused mind. I can give any other information needed to help get this fixed.

TIA.
-Ben
May 11 '07 #1
3 1647
Sorry, in my attempt to clean up my SQL and make it more coherent for others, I made a mistake. The second join in my first select actually should be this:

[PHP]INNER JOIN Vendors v on v.id = eq.vendor_id[/PHP]
May 11 '07 #2
Rimsky
3
I am trying to do a join including 5 tables: one each for customers, equipment, equipment categories, equipment configurations, and configuration items. Here is my current code:

[PHP]SELECT
eq.id, eq.partnum, eq.description, eq.cost,
cat.name, v.name,
i.qty,
cf.name, cf.usr_id
FROM Equipment eq
INNER JOIN Equipment_Categories cat on cat.id = eq.cat_id
INNER JOIN Vendors v on v.id = eq.cust_id
INNER JOIN Equipment_Configurations cf on cf.id = 5
LEFT JOIN Equipment_Configurations_Items i on i.eq_id = eq.id
where i.cf_id = cf.id OR i.qty IS NULL[/PHP]

Is that all as clear as mud? Hopefully it makes sense to more than my own confused mind. I can give any other information needed to help get this fixed.

TIA.
-Ben
Ben,

Try to use LEFT OUTER JOIN\'s
May 14 '07 #3
Thanks Rimsky. LEFT OUTER JOIN didn't help me, but it did get me thinking and helped me come up with the solution. Turns out all I had to do was remove
where i.cf_id = cf.id OR i.qty IS NULL
and change the last join to
LEFT JOIN Equipment_Configurations_Items i on i.eq_id = eq.id AND i.cf_id = cf.id
Thanks again for getting me thinking right.

-Ben
May 14 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Amitava Bardhan | last post by:
Request.servervariables("Logon_USer") is working perfectly well for one asp page returning the NT userid as expected but returning an empty string for another asp page.Both the asp pages are in the...
1
by: Gaurav | last post by:
when i use the requiredfieldvalidator control on my web form and run the application i get the following message: Unable to find script library '/aspnet_client/system_web/1_1_4322/webuivalidatio...
17
by: Leif | last post by:
Hello All, Last week (11 Nov 05) with the Subject header: "XHTML 1.0 Strict validation problem", I tried to describe a validation problem with text added to my source files in XHTML. I was...
0
by: Alain Hogue | last post by:
When I try to use "%" with the LIKE command it return nothing if they are an acute charater in the search string. For example if I have a field named "Title" containing words like "Hébert, Gagné,...
2
by: Karl | last post by:
Hi all, I'm reasonably new to csharp so you have to forgive me asking what may be a stupid question... As I said, I'm new to CSharp but in VB I was able to click anywhere on a DataGridView...
0
by: tslorick | last post by:
Hi, I'm having trouble getting this ASP code to work. I want to retrieve the record just added, and found the scope_identity() syntax, but I get the following error: Microsoft JET Database...
1
by: =?Utf-8?B?d2VzdGNvYXN0Y29kZQ==?= | last post by:
void CreateBatches(string currDir, DataTable hvTable) { string id; DirectoryInfo di = new DirectoryInfo(currDir); StreamWriter writer = new StreamWriter(currDir + "\\Index.dat", false); DataRow...
2
by: Lebbsy | last post by:
I have a datasheet form that on opening I want to select only the records whose Decision field value is "Recommend". I have the following SQL statement so far but it gives me the "cannot execute...
46
by: benwizzle | last post by:
Im using this code to make it so one can select a row from a gridview by clicking on it anywhere. protected void gvSideList_RowDataBound(object sender, GridViewRowEventArgs e) { if...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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,...
0
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...

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.