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

problem with join

Hello,

I am having a problem with a join. Either I am just not seeing the
obvious, it isn't possible, or I need to use a different approach. I
have an application with a vsflexgrid that needs to display the
following:

filenumber, BL, Container_BL, BL_HBL, HBL, Container_HBL

The tables look like:

CREATE TABLE tblFILE (
FileNumber int not null Primary Key,
status char(1) not null
)

CREATE table tblBL (
bl_Identity bigint not null primary key identity,
bl varchar(20) not null,
FileNumber BIGint not null,

CONSTRAINT FK_tblFILE_tblBL FOREIGN KEY (FileNumber) REFERENCES
tblFILE(FileNumber)
)

CREATE TABLE tblCONTAINER (
ContainerID bigint not null primary key identity,
ContainerNumber varchar(20) not null
)
CREATE table tblCONTAINER_BL (
ContainerID bigint not null ,
BL_Identity bigint not null ,

CONSTRAINT FK_tblCONTAINER FOREIGN KEY (ContainerID) REFERENCES
tblCONTAINER(ContainerID),

CONSTRAINT FK_tblBL FOREIGN KEY (BL_Identity) REFERENCES
tblBL(BL_Identity),
constraint PK_tblBL_tblCONTAINER primary key (ContainerID,
BL_Identity)
)
CREATE TABLE tblHBL (
hbl_Identity bigint not null primary key identity,
hbl varchar(20) not null,
FileNumber BIGint not null,
bl_identity bigint,

CONSTRAINT FK_tblFILE_tblHBL FOREIGN KEY (FileNumber) REFERENCES
tblFILE(FileNumber)

CONSTRAINT FK_tblBL FOREIGN KEY (bl_identity) REFERENCES
tblBL(bl_identity)

)
CREATE table tblCONTAINER_HBL (
ContainerID bigint not null ,
hbl_Identity bigint not null ,

CONSTRAINT FK_tblCONTAINER FOREIGN KEY (ContainerID) REFERENCES
tblCONTAINER(ContainerID),

CONSTRAINT FK_tblHBL FOREIGN KEY (hbl_Identity) REFERENCES
tblHBL(hbl_Identity),
constraint PK_tblHBL_tblCONTAINER primary key (ContainerID,
hbl_Identity)
)

To explain this a little bit...a file has relations with zero to many
BLs, a BL has zero to many containers. A file also has zero to many
HBLs, an HBL has zero to many Containers. Also, a BL has zero to many
HBLs. An HBL will eventually always have a BL but because of the
business process, many months may go by without knowing what the BL is
so the stable relationship for the HBL is with the file. But when the
HBL has a relationship with a BL, it needs to be clear.

So my problem, I tried to make a view that shows each relationship but
I can't get it to show correctly.

CREATE VIEW fileselecthbl_bl_view as (
Select f1.FileNumber, f1.Status, f1.CustomerID, bl.BL_Identity, bl.BL,
hbl.HBL_Identity, hbl.HBL,
chbl.ContainerID, c1.ContainerNumber from tblFile f1 left OUTER JOIN
tblHBL hbl
ON (f1.FileNumber = hbl.FileNumber) full OUTER JOIN tblBL bl ON
(bl.BL_Identity = hbl.BL_ID)
LEFT OUTER JOIN tblCONTAINER_HBL chbl ON (hbl.HBL_Identity =
chbl.HBL_ID) left outer join
tblCONTAINER c1 ON (chbl.ContainerID = c1.ContainerIdentity) )

But this doesn't give me what I need. This gives me 40 rows with a
filenumber and six with null in the filenumber. What I need is for the
hbl AND the bl to be joined to the initial tblFILE.
---This is vb code showing where it will be used.
With vsfgOpenedFiles
.TextMatrix(0, 0) = "File Number"
.TextMatrix(0, 1) = "File Status"
.TextMatrix(0, 2) = "BL"
.TextMatrix(0, 3) = "Container by BL"
.TextMatrix(0, 4) = "HBL"
.TextMatrix(0, 5) = "Container by HBL"
.AutoSize 0, 5
End With
----

I need to have rows that show FileNumber, Status, Bl, Container by BL,
HBL if it has a relationship with BL, Container by HBL.

If a BL does not have a relationship with an HBL then the row needs to
show FileNumber, Status, BL, Container by BL, Null, Null

If an HBL does not have a relationship with BL then it needs to be a
line with FileNumber, Status, Null, Null, HBL, Container by HBL.

Jeez, I am sorry this is so long. I don't know how else to explain my
problem. I am going to stop. I would appreciate anybody's ideas.

--rowan
Jul 20 '05 #1
6 2634
Rowan (ph********@yahoo.com) writes:
I am having a problem with a join. Either I am just not seeing the
obvious, it isn't possible, or I need to use a different approach. I
have an application with a vsflexgrid that needs to display the
following:


Thanks for the tables and the description. Alas, you did not include
any sample data in form of INSERT statements and the expected results
from the sample. Therefore I find it difficult to understand what you
are looking for.
--
Erland Sommarskog, SQL Server MVP, so****@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #2
Sorry, I didn't include any sample data. I think this should do it.

Insert tblFILE (FileNumber, Status) Values(10111, 'O')
Insert tblFILE (FileNumber, Status) Values(10222, 'O')
Insert tblFile (FileNumber, Status) values(10333, 'X')
Insert tblFILe (FileNumber, Status) Values(10444, 'O')

Insert tblBL (bl_Identity, bl, FileNumber) values(200, 'bl1', 10222)
Insert tblBL (bl_Identity, bl, FileNumber) values(210, 'bl2', 10111)
Insert tblBL (bl_Identity, bl, FileNumber) values(220, 'bl2', 10111)

Insert tblCONTAINER (ContainerID, ContainerNumber) Values(30,
C10000000)
Insert tblCONTAINER (ContainerID, ContainerNumber) Values(31,
C11111111)
Insert tblCONTAINER (ContainerID, ContainerNumber) Values(32,
C12222222)
Insert tblCONTAINER (ContainerID, ContainerNumber) Values(33,
C13333333)
Insert tblCONTAINER (ContainerID, ContainerNumber) Values(34,
C14444444)
Insert tblCONTAINER (ContainerID, ContainerNumber) Values(35,
C15555555)
Insert tblCONTAINER (ContainerID, ContainerNumber) Values(36,
C16666666)

Insert tblCONTAINER_BL (ContainerID, BL_Identity) Values(30, 200)
Insert tblCONTAINER_BL (ContainerID, BL_Identity) values(31, 200)
Insert tblCONTAINER_BL (ContainerID, BL_Identity) values(32, 220)

Insert tblHBL (hbl_Identity, hbl, FileNumber, bl_Identity) values(400,
'hbl1', 10222, NULL)
Insert tblHBL (hbl_Identity, hbl, FileNumber, bl_Identity) values(410,
'hbl2', 10111, 210)
Insert tblHBL (hbl_Identity, hbl, FileNumber, bl_Identity) values(420,
'hbl3', 10444, NULL)
Insert tblHBL (hbl_Identity, hbl, FileNumber, bl_Identity) values(430,
'hbl4', 10111, 210)

Insert tblCONTAINER_HBL (ContainerID, hbl_Identity) values(33, 400)
Insert tblCONTAINER_HBL (ContainerID, hbl_Identity) values(34, 400)
Insert tblCONTAINER_HBL (ContainerID, hbl_Identity) values(35, 400)
Insert tblCONTAINER_HBL (ContainerID, hbl_Identity) values(36, 430)
Jul 20 '05 #3
Rowan (ph********@yahoo.com) writes:
Sorry, I didn't include any sample data. I think this should do it.


Thsnks for the sample data, but I still don't know what the desired output
is. The output I got from the query you posted (where I had to change
some names to make it compile, and remove CustomerID) was:

10111 O 210 bl2 410 hbl2 NULL NULL
10111 O 210 bl2 430 hbl4 36 C16666666
10222 O NULL NULL 400 hbl1 33 C13333333
10222 O NULL NULL 400 hbl1 34 C14444444
10222 O NULL NULL 400 hbl1 35 C15555555
10333 X NULL NULL NULL NULL NULL NULL
10444 O NULL NULL 420 hbl3 NULL NULL
NULL NULL 200 bl1 NULL NULL NULL NULL
NULL NULL 220 bl2 NULL NULL NULL NULL
--
Erland Sommarskog, SQL Server MVP, so****@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #4
Oops I left something out. For the last two lines I need more than
the filenumber. I also need the BL Container information.

NULL NULL 200 bl1 NULL NULL NULL NULL
NULL NULL 220 bl2 NULL NULL NULL NULL
So, what I would need for this line is:

10222 O 200 bl1 NULL NULL 30 C10000000
10222 O 200 bl1 NULL NULL 31 C11111111
10111 O 220 bl3 NULL NULL 32 C12222222

Do I need to create two views?
Jul 20 '05 #5
Rowan (ph********@yahoo.com) writes:
Oops I left something out. For the last two lines I need more than
the filenumber. I also need the BL Container information.

NULL NULL 200 bl1 NULL NULL NULL NULL
NULL NULL 220 bl2 NULL NULL NULL NULL
So, what I would need for this line is:

10222 O 200 bl1 NULL NULL 30 C10000000
10222 O 200 bl1 NULL NULL 31 C11111111
10111 O 220 bl3 NULL NULL 32 C12222222

Do I need to create two views?


Thanks for the data! I believe I now get hinch about your data model.
Here is a query that appears to correspond to your initial narrative,
and indeed gives the above rows:

SELECT FileNumber = coalesce(f1.FileNumber, f2.FileNumber),
Status = coalesce(f1.status, f2.status),
bl.bl_Identity, bl.bl, hbl.hbl_Identity, hbl.hbl,
ContainerID = coalesce(cbl.ContainerID, chbl.ContainerID),
coalesce(c1.ContainerNumber, c2.ContainerNumber)
FROM tblFILE f1
LEFT JOIN (tblHBL hbl
JOIN tblCONTAINER_HBL ch ON hbl.hbl_Identity = ch.hbl_Identity
JOIN tblCONTAINER c1 ON ch.ContainerID = c1.ContainerID)
ON f1.FileNumber = hbl.FileNumber
FULL JOIN (tblBL bl
JOIN tblCONTAINER_BL cbl ON bl.bl_Identity = cbl.BL_Identity
JOIN tblCONTAINER c2 ON cbl.ContainerID = c2.ContainerID
JOIN tblFILE f2 ON bl.FileNumber = f2.FileNumber)
ON bl.bl_Identity = hbl.bl_identity

The key here is that JOIN is an operator just like plus. The HBL should
be inner joined to the container table, because once you have an HBL,
you have the rest. So you join tblFILE with the tbale (HBL JOIN ch JOIN c1).
Same applies for the FULL JOIN stuff.

Not that the parantheses specifies *logical* evaluation order. The
optmizer may apply all sorts of shortcuts, as long as the result is
the the one specified by the expression.
--
Erland Sommarskog, SQL Server MVP, so****@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #6
Ohhhh, thank you! This is so helpful. Plus it gives me a model to
look at and understand for future reference. Thank you very much.
Erland Sommarskog <so****@algonet.se> wrote in message news:<Xn**********************@127.0.0.1>...
Rowan (ph********@yahoo.com) writes:
Oops I left something out. For the last two lines I need more than
the filenumber. I also need the BL Container information.

NULL NULL 200 bl1 NULL NULL NULL NULL
NULL NULL 220 bl2 NULL NULL NULL NULL
So, what I would need for this line is:

10222 O 200 bl1 NULL NULL 30 C10000000
10222 O 200 bl1 NULL NULL 31 C11111111
10111 O 220 bl3 NULL NULL 32 C12222222

Do I need to create two views?


Thanks for the data! I believe I now get hinch about your data model.
Here is a query that appears to correspond to your initial narrative,
and indeed gives the above rows:

SELECT FileNumber = coalesce(f1.FileNumber, f2.FileNumber),
Status = coalesce(f1.status, f2.status),
bl.bl_Identity, bl.bl, hbl.hbl_Identity, hbl.hbl,
ContainerID = coalesce(cbl.ContainerID, chbl.ContainerID),
coalesce(c1.ContainerNumber, c2.ContainerNumber)
FROM tblFILE f1
LEFT JOIN (tblHBL hbl
JOIN tblCONTAINER_HBL ch ON hbl.hbl_Identity = ch.hbl_Identity
JOIN tblCONTAINER c1 ON ch.ContainerID = c1.ContainerID)
ON f1.FileNumber = hbl.FileNumber
FULL JOIN (tblBL bl
JOIN tblCONTAINER_BL cbl ON bl.bl_Identity = cbl.BL_Identity
JOIN tblCONTAINER c2 ON cbl.ContainerID = c2.ContainerID
JOIN tblFILE f2 ON bl.FileNumber = f2.FileNumber)
ON bl.bl_Identity = hbl.bl_identity

The key here is that JOIN is an operator just like plus. The HBL should
be inner joined to the container table, because once you have an HBL,
you have the rest. So you join tblFILE with the tbale (HBL JOIN ch JOIN c1).
Same applies for the FULL JOIN stuff.

Not that the parantheses specifies *logical* evaluation order. The
optmizer may apply all sorts of shortcuts, as long as the result is
the the one specified by the expression.

Jul 20 '05 #7

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

Similar topics

16
by: Ling Lee | last post by:
Hello. I'm trying to write a small program that lets you put in a number as an integer and then it tells you the textuel representation of the number. Like if your input is 42, it will say...
2
by: Bruce Duncan | last post by:
I'm a bit new to MySQL (know MS SQL well...and that may be the problem...getting the syntax confused) and I'm having a join problem...can anyone offer some help? Here's my problem: I have table1...
1
by: Keith | last post by:
I have created a view to test some of the data in my database. I am relatively new to SQL so may have caused this problem by doing something wrong. I have a table called SYS_Individual which...
14
by: signaturefactory | last post by:
I am trying the following query in and oleDbCommand: SELECT PartLocations.LocationName, Sum(PartsJournal.Quantity) AS SumOfQuantity, PartsJournal.PartsLotNumber FROM PartLocations INNER JOIN...
8
by: Andrew McNab | last post by:
Hi folks, I have a problem with an MS Access SQL query which is being used in an Access Report, and am wondering if anyone can help. Basically, my query (shown below) gets some records from a...
1
by: Andrew McNab | last post by:
Hi folks, I have a problem with an MS Access SQL query which is being used in an Access Report, and am wondering if anyone can help. Basically, my query (shown below) gets some records from a...
27
by: John Salerno | last post by:
Ok, here's a problem I've sort of assigned to myself for fun, but it's turning out to be quite a pain to wrap my mind around. It's from a puzzle game. It will help if you look at this image: ...
1
by: imranpariyani | last post by:
Hi i have a severe performance problem with one of my views which has 6 to 8 joins .. any help will be appreciated.. the view is: CREATE OR REPLACE VIEW thsn.trade_view AS SELECT...
9
by: HC | last post by:
Hello, all, I started out thinking my problems were elsewhere but as I have worked through this I have isolated my problem, currently, as a difference between MSDE and SQL Express 2005 (I'll just...
3
by: Anila | last post by:
Hi Friends, My problem with Inner join is ... first i joined two tables and i got the result. after that iam trying to join one more table its giving syn tax error in JOIN condition. ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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...
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.