473,770 Members | 7,229 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_tblB L FOREIGN KEY (FileNumber) REFERENCES
tblFILE(FileNum ber)
)

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(Co ntainerID),

CONSTRAINT FK_tblBL FOREIGN KEY (BL_Identity) REFERENCES
tblBL(BL_Identi ty),
constraint PK_tblBL_tblCON TAINER 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_tblH BL FOREIGN KEY (FileNumber) REFERENCES
tblFILE(FileNum ber)

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

)
CREATE table tblCONTAINER_HB L (
ContainerID bigint not null ,
hbl_Identity bigint not null ,

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

CONSTRAINT FK_tblHBL FOREIGN KEY (hbl_Identity) REFERENCES
tblHBL(hbl_Iden tity),
constraint PK_tblHBL_tblCO NTAINER 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_b l_view as (
Select f1.FileNumber, f1.Status, f1.CustomerID, bl.BL_Identity, bl.BL,
hbl.HBL_Identit y, hbl.HBL,
chbl.ContainerI D, c1.ContainerNum ber 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_HB L chbl ON (hbl.HBL_Identi ty =
chbl.HBL_ID) left outer join
tblCONTAINER c1 ON (chbl.Container ID = c1.ContainerIde ntity) )

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 2650
Rowan (ph********@yah oo.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_HB L (ContainerID, hbl_Identity) values(33, 400)
Insert tblCONTAINER_HB L (ContainerID, hbl_Identity) values(34, 400)
Insert tblCONTAINER_HB L (ContainerID, hbl_Identity) values(35, 400)
Insert tblCONTAINER_HB L (ContainerID, hbl_Identity) values(36, 430)
Jul 20 '05 #3
Rowan (ph********@yah oo.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********@yah oo.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.Fil eNumber, f2.FileNumber),
Status = coalesce(f1.sta tus, f2.status),
bl.bl_Identity, bl.bl, hbl.hbl_Identit y, hbl.hbl,
ContainerID = coalesce(cbl.Co ntainerID, chbl.ContainerI D),
coalesce(c1.Con tainerNumber, c2.ContainerNum ber)
FROM tblFILE f1
LEFT JOIN (tblHBL hbl
JOIN tblCONTAINER_HB L ch ON hbl.hbl_Identit y = 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********@yah oo.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.Fil eNumber, f2.FileNumber),
Status = coalesce(f1.sta tus, f2.status),
bl.bl_Identity, bl.bl, hbl.hbl_Identit y, hbl.hbl,
ContainerID = coalesce(cbl.Co ntainerID, chbl.ContainerI D),
coalesce(c1.Con tainerNumber, c2.ContainerNum ber)
FROM tblFILE f1
LEFT JOIN (tblHBL hbl
JOIN tblCONTAINER_HB L ch ON hbl.hbl_Identit y = 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
2072
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 four two. I found out that I have to make a dictionary like this: List = { 1:"one", 2:"two" and so on )
2
1949
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 that needs to "left" join to table1A, table1B, and table1C which is corrently done with the following: select table1.x, table1a.y, table1b.z, table1c.q from table1 left join table1a on table1.ID = table1a.ID left join table1b on table1.ID =...
1
1412
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 contains records on individuals. A number of the fields in this take a numeric entry which corresponds to an entry in another table (used to populate drop down menus in the application being developed). The tables which are used for the dropdowns...
14
611
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 PartsJournal ON PartLocations.LocationID = PartsJournal.LocationID GROUP BY PartLocations.LocationName, PartsJournal.PartsLotNumber, PartsJournal.PartNumber, PartsJournal.LocationID HAVING (((Sum(PartsJournal.Quantity))>0) AND...
8
19600
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 couple of tables in my database using INNER JOINS and the WHERE clause to specify the required constraints. However, I also want to read two fields from a *single* record from a table called 'Locations' and then apply one of these field's values...
1
4178
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 couple of tables in my database using INNER JOINS and the WHERE clause to specify the required constraints. However, I also want to read two fields from a *single* record from a table called 'Locations' and then apply one of these field's values...
27
2312
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: http://www.johnjsal.devisland.net/switches.jpg Here's the situation: Each of the four rows in the diagram is considered a single 'panel'. Each panel has eight 'switches', which are composed of two columns each, and these columns have a total of 20...
1
3115
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 tra.tra_id, tra.per_id, tra.fir_id, tra.tra_dcn, tra.tra_startdate::date AS tra_startdate, tra.tra_enddate::date AS tra_enddate, tra.tra_highprice, tra.tra_lowprice, tra.tra_shares, tra.tra_marketvalue, tra.tra_commonsharesheld, tra.tra_directsharesheld,...
9
5759
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 call it Express for simplicity). I have, to try to simplify things, put the exact same DB on two systems, one running MSDE and one running Express. Both have 2 Ghz processors (one Intel, one AMD), both have a decent amount of RAM (Intel system...
3
2388
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. Here is the Query
0
9592
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
9425
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
10230
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
10058
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10004
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9870
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...
1
7416
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
6678
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();...
2
3576
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.