Just to make sure there was no "human error" I copy / pasted your code, I received the exact same results.
Main Topics
Browse All TopicsHello Experts,
Below is a SELECT statement I am using from within VBA (Excel 2003) to populate a spreadsheet from a Firebird SQL v1.5 DB. Spreadsheet population works well, BUT...
I have three tables: JOBS, INVOICE, and SHIPMASTER
All tables are linked by the field: JOBNO
Eight fields are being pulled in the order they are needed on the spreadsheet.
JOBS has only one record for each JOBNO
INVOICE and SHIPMASTER may have multiple records for each JOBNO
str_Select = _
"SELECT J.CUSTNAME, J.JOBNO, I.INVUNINO, J.CRETDATE, J.NEEDDATE, I.INVDATE, S.SHIPDATE, J.JOBSTATS FROM JOBS J " & _
"LEFT OUTER JOIN INVOICE I on J.JOBNO = I.JOBNO " & _
"INNER JOIN SHIPMASTER S on I.JOBNO = S.JOBNO " & _
"WHERE J.CRETDATE >= '01.01.2007' " & _
"ORDER BY CRETDATE" & ";"
In the results, I get: (No matter how I arrange the JOINS")
CUSTNAME JOBNO INVUNINO CRETDATE NEEDDATE INVDATE SHIPDATE JOBSTATS
J & C INDUSTRIES 10277 21641 1/4/2007 2/15/2007 1/26/2007 1/26/2007 CLOSED
J & C INDUSTRIES 10277 21662 1/4/2007 2/15/2007 1/30/2007 1/26/2007 CLOSED
J & C INDUSTRIES 10277 21737 1/4/2007 2/15/2007 2/15/2007 1/26/2007 CLOSED
J & C INDUSTRIES 10277 21641 1/4/2007 2/15/2007 1/26/2007 1/30/2007 CLOSED
J & C INDUSTRIES 10277 21662 1/4/2007 2/15/2007 1/30/2007 1/30/2007 CLOSED
J & C INDUSTRIES 10277 21737 1/4/2007 2/15/2007 2/15/2007 1/30/2007 CLOSED
J & C INDUSTRIES 10277 21641 1/4/2007 2/15/2007 1/26/2007 2/15/2007 CLOSED
J & C INDUSTRIES 10277 21662 1/4/2007 2/15/2007 1/30/2007 2/15/2007 CLOSED
J & C INDUSTRIES 10277 21737 1/4/2007 2/15/2007 2/15/2007 2/15/2007 CLOSED
THIS is what the results should look like: (There is One record in JOBS, Three records in INVOICE, and Three records in SHIPMASTER)
J & C INDUSTRIES 10277 21641 1/4/2007 2/15/2007 1/26/2007 1/27/2007 CLOSED
J & C INDUSTRIES 10277 21662 1/4/2007 2/15/2007 1/30/2007 1/30/2007 CLOSED
J & C INDUSTRIES 10277 21737 1/4/2007 2/15/2007 2/15/2007 2/15/2007 CLOSED
What is wrong with that SELECT statement? Should I be using a UNION, if so how would I implement that here?
TIA!
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
>There is One record in JOBS, Three records in INVOICE, and Three records in SHIPMASTER
then it is, by default, normal that you get 9 rows, unless you have 1 more condition to join the 3 rows of invoice to the 3 rows of shipmaster.
I assume there ust be an additional condition to join the 2 tables INVOICE and SHIPMASTER than only jobno, so that 1 rows of INVOICE get's tied to 1 rows of SHIPMASTER.
if you can add that condition, it should reduce the output to indeed 3 rows.
As angelIII has said, it looks like the 9 rows is correct, unless you simply want to suppress the duplicate information in which case add distinct to the query
"SELECT distinct J.CUSTNAME, J.JOBNO, I.INVUNINO, J.CRETDATE, J.NEEDDATE, I.INVDATE, S.SHIPDATE, J.JOBSTATS FROM (JOBS J " & _
"LEFT OUTER JOIN INVOICE I on J.JOBNO = I.JOBNO) " & _
"LEFT OUTER JOIN SHIPMASTER S on J.JOBNO = S.JOBNO " & _
"WHERE J.CRETDATE >= '01.01.2007' " & _
"ORDER BY CRETDATE" & ";"
AngelIII: I'm not sure what you mean by "condition". Are you suggesting to another JOIN, linking INVOICE to SHIPMASTER? Or an addition to WHERE portion of the SELECT statement? Could you elaborate please?
NickUpson: I tried adding the "distinct" to the SELECT statement and adjusted the JOIN to make both INVOICE and SHIPMASTER link to JOBS on the JOBNO field. But, the same results were returned.
BTW: I am releasing the recordset resources within the same SUB with the following code. So I am recreating the recordset fresh upon re-opening the workbook.
'close recordset
rs_Data.Close
'erase recordset
Set rs_Data = Nothing
'release sheest
Set dbSheet = Nothing
'close connection
cnn1.Close
'erase connection
Set cnn1 = Nothing
>AngelIII: I'm not sure what you mean by "condition".
another "condition" looking like " aka INVOICE.somefield = SHIPMASTER.somefield "
with "somefield" being a field where you can match the 3 rows together respectively.
>Are you suggesting to another JOIN, linking INVOICE to SHIPMASTER?
>Or an addition to WHERE portion of the SELECT statement? Could you elaborate please?
I suggest the condition to be added on the
JOIN .. ON <condition> AND <additionalcondition here>
AngelIII: I will check for another field that I can link between INVOICE and SHIPMASTER.
NickUpson: I'm sorry, I thought I had provided adequate information, but I will get the specific info together and post it.
Both: I need to leave the office for awhile, will address when I get back, thank you for your assistance and patience, thus far.
Thank you all!
AngelIII: You were right on the mark, the below SELECT statement appears to give me exactly the information I need. I have to do a little more manual verification, but it looks good so far.
str_Select = _
"SELECT DISTINCT J.CUSTNAME, J.JOBNO, I.INVUNINO, J.CRETDATE, J.NEEDDATE, I.INVDATE, S.SHIPDATE, J.JOBSTATS FROM (JOBS J " & _
"LEFT OUTER JOIN INVOICE I on J.JOBNO = I.JOBNO) " & _
"LEFT OUTER JOIN SHIPMASTER S on I.JOBNO = S.JOBNO AND I.SUBNO = S.SHIPNO " & _
"WHERE J.CRETDATE >= '01.01.2007' " & _
"ORDER BY CRETDATE" & ";"
Business Accounts
Answer for Membership
by: roryaPosted on 2008-01-14 at 16:33:40ID: 20658894
Does this work:
str_Select = _
"SELECT J.CUSTNAME, J.JOBNO, I.INVUNINO, J.CRETDATE, J.NEEDDATE, I.INVDATE, S.SHIPDATE, J.JOBSTATS FROM (JOBS J " & _
"LEFT OUTER JOIN INVOICE I on J.JOBNO = I.JOBNO) " & _
"LEFT OUTER JOIN SHIPMASTER S on J.JOBNO = S.JOBNO " & _
"WHERE J.CRETDATE >= '01.01.2007' " & _
"ORDER BY CRETDATE" & ";"