473,385 Members | 1,912 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,385 software developers and data experts.

Complex Query Woe

blyxx86
256 100+
Hey there, I'm running into a slight problem today...

I have a few things to show...

I'm running into duplicate values being shown in my query, but I cannot use a "Select DISTINCT" as it removes the records...

It's a complex query, so I'm including 3 parts.. an image of the joins, an example of the duplicate values and the SQL.
Expand|Select|Wrap|Line Numbers
  1. SELECT tblRequest.CustomerTracking, tblRequest.ReqDate, tblLocations.LocationCode, tblInvoice.TrackingNumber, [Prefix] & tblModel_1.ModelName AS ShippedModel, tblInvoiceDetail.Serial, [StdQuantity]*tblInvoiceDetail.Quantity AS ShipQty, tblInvoice.InvoiceNote, tblRequestType.RequestType, tblInvoice.InvoiceDate
  2. FROM (((tblModelDetail INNER JOIN tblModel AS tblModel_1 ON tblModelDetail.IncludesModelID = tblModel_1.ModelID) INNER JOIN (tblModel INNER JOIN tblProduct ON tblModel.ModelID = tblProduct.ModelID) ON tblModelDetail.ModelID = tblModel.ModelID) INNER JOIN tblProduct AS tblProduct_1 ON (tblProduct_1.CustomerID = tblProduct.CustomerID) AND (tblModel_1.ModelID = tblProduct_1.ModelID)) INNER JOIN ((tblLocations RIGHT JOIN (tblInvoice INNER JOIN tblCustomer ON tblInvoice.CustomerID = tblCustomer.CustomerID) ON tblLocations.LocationID = tblInvoice.LocationID) INNER JOIN (tblInvoiceDetail LEFT JOIN (tblRequestType RIGHT JOIN (qry_Qtys RIGHT JOIN (tblRequest LEFT JOIN tblRequestDetail ON tblRequest.RequestID = tblRequestDetail.RequestID) ON qry_Qtys.RequestID = tblRequestDetail.RequestID) ON tblRequestType.RequestTypeID = tblRequest.RequestTypeID) ON tblInvoiceDetail.RequestID = tblRequest.RequestID) ON tblInvoice.InvoiceID = tblInvoiceDetail.InvoiceID) ON tblProduct.ProductID = tblInvoiceDetail.ProductID;
  3.  




Shown here are the duplicate values... However only one ROS-LS2208, ROS-255270120 and ROS-207164501 should be shown. I know the problem lies within that "loop" of tblmodel/tblmodeldetail/tblproduct. However that loop needs to be there for reporting purposes... It shows the parts of "KITS" as being shipped individually. I know.. it's confusing, but perhaps there is another way to accomplish this? Any thoughts?

Maybe you can help me figure out a way to restructure the query?
Nov 10 '07 #1
3 1747
ADezii
8,834 Expert 8TB
In my humble opinion, as long as you have the 'Circular Relationships' existing between tblProduct, tblProduct_1, tblModel_1, tblModelDetail, and tblModel, you will be encountering a host of problems. What I am seeing is analogous to Circular References in programming jargon and I feel as though you will need a major restructuring of the Joins. I would suggest, however, that you wait and see what other Moderators/Experts suggest before you initiate any kind of changes. Just for curiosity, why the duplication on tblModel and tblProduct as far as the Relationships go?
Nov 10 '07 #2
blyxx86
256 100+
I use the circular reference to find the quantities of units shipped from "KITS"... a KIT includes those additional units. It is for the inventory management side. If the inventory was kept by full KIT models. Also, when shipments are made, but only partial orders are shipped then it keeps track of it for me.

So, in order to have the models listed properly I need to have the ModelDetails look up the models again and then the product table needs to match the customer again otherwise I have similar models being shown for all the customers.

I understand the circular lookup isn't the best scenario, but I believe it is necessary for what I am doing. Unless someone else has another way of accomplishing what I am doing...
Nov 10 '07 #3
blyxx86
256 100+
I may have got something working...

Expand|Select|Wrap|Line Numbers
  1. SELECT DISTINCT tblRequest.CustomerTracking, tblRequest.ReqDate, tblLocations.LocationCode, tblInvoice.TrackingNumber, tblInvoiceDetail.Serial, [StdQuantity]*tblInvoiceDetail.Quantity AS ShipQty, tblInvoice.InvoiceNote, tblRequestType.RequestType, tblInvoice.InvoiceDate, qry_Qtys.InvoicedQty, qry_Qtys.RequestedQty, [Prefix] & tblModel_1.ModelName AS Model, tblModel.ModelName, tblInvoiceDetail.InvoiceDetailID, tblProduct.ProductID, tblModel.ModelID, tblModelDetail.ModelDetailID, tblModel_1.ModelID, tblProduct_1.ProductID, tblInvoice.InvoiceID, qry_Qtys.RequestID
  2. FROM (tblLocations RIGHT JOIN (tblInvoice INNER JOIN tblCustomer ON tblInvoice.CustomerID = tblCustomer.CustomerID) ON tblLocations.LocationID = tblInvoice.LocationID) INNER JOIN (tblProduct AS tblProduct_1 INNER JOIN (tblModel AS tblModel_1 INNER JOIN (((tblModel INNER JOIN tblModelDetail ON tblModel.ModelID = tblModelDetail.ModelID) INNER JOIN tblProduct ON tblModel.ModelID = tblProduct.ModelID) INNER JOIN (tblInvoiceDetail LEFT JOIN (tblRequestType RIGHT JOIN (qry_Qtys RIGHT JOIN (tblRequest LEFT JOIN tblRequestDetail ON tblRequest.RequestID = tblRequestDetail.RequestID) ON qry_Qtys.RequestID = tblRequestDetail.RequestID) ON tblRequestType.RequestTypeID = tblRequest.RequestTypeID) ON tblInvoiceDetail.RequestID = tblRequest.RequestID) ON tblProduct.ProductID = tblInvoiceDetail.ProductID) ON tblModel_1.ModelID = tblModelDetail.IncludesModelID) ON (tblModel_1.ModelID = tblProduct_1.ModelID) AND (tblProduct_1.CustomerID = tblProduct.CustomerID)) ON tblInvoice.InvoiceID = tblInvoiceDetail.InvoiceID;
  3.  


New field was included (InvoiceDetailID)... It let me use SELECT DISTINCT and still show the records...



While what I did worked, I'm sure there is a better way to accomplish it... But please let me know what you think...

I ran into a small problem when a QTY 0 was being used... However that wouldn't make sense in any real world situation (Qty 0 shipped??? Why not just not include it on the shipping records...) It was always shown as backordered, but for some reason a duplicate record was thrown in when the qty was 0. Weird. Changing it to 1 or deleting it fixed that. I also added a validation rule to make all input numbers >0.
Nov 10 '07 #4

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

Similar topics

4
by: Starbuck | last post by:
OK, first let me say that I am no DB person. But I have a user here who keeps getting this error whenever she does, whatever it is she does, with databases... A google search takes me to...
2
by: Mikel | last post by:
I am trying to get around the problem "The expression you have entered is too complex" for a select query. (The example below is not the expression that is giving me headaches.) So I am thinking...
4
by: ED | last post by:
I am attempting to to write a query that has a numerous nested IIf statements. The problem that I am having is that it is to long of a query to be built in design mode and when I build it in sql...
8
by: Matt | last post by:
Hi all, Thank you for taking the time. I have a database with 45 tables on it. 44 tables are linked to a main table through a one to one relationship. My question is, is there no way i can...
2
by: Ben de Vette | last post by:
Hi, I'm using the querybuilder when updating a record in a table (Access). However, I get a "Query is too complex" message. The Primary key is autonumbered. Why is it making such a complex...
1
by: arun | last post by:
Query is too complex -------------------------------------------------------------------------------- Hi, I was trying to solve this problem since last two days but couldn't find any solution. ...
1
by: Randy Volkart | last post by:
I'm trying to fix a glitch in a complex access database, and have a fairly complex problem... unless there's some obscure easy fix I don't know being fairly new with Access. Basically, the area...
19
by: kawaks40 | last post by:
Hi everyone :) I just recently started using access/sql. and right away I ran into this problem "SQL expression too complex" I google'd a lot on what it means, and the only workaround I've...
3
by: Eric Davidson | last post by:
DB2 9.5 I keep geting the message. SQL0101N The statement is too long or too complex. SQLSTATE=54001 When one of my sql statements takes over 60 seconds to compile the sql statement. Is...
0
crystal2005
by: crystal2005 | last post by:
Hi, I am having trouble with some complex SQL queries. I’ve got winestore database, taken from Web Database Application with PHP and MySQL book. And some question about queries as the following ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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...

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.