Join Problems - Getting Cartesian Product (cross join) and Too Many Results

Hi,

I am trying to write a stored proc that returns data from several tables but I am getting a Cartesian product (cross join) and I am having problems getting rid of it.  The problem occurs when I add both my Passengers table and my bags tables.  For each transaction, there can be mulitple passengers, multiple bags and each bag can have multiple charges/descriptions (ex. a bag can be both over weight and over size). I don't care which bag belongs to which passenger as long as long as both passengers are not charged for both bags.

The relationships are:
tbTransactions
tbTransactions.TransactionID_tbBagHandling.TransactionID
tbTransactions.TransactionID_tbPassengers.TransactionID
tbPaymentTypes.PaymentTypeID_tbTransactions.PaymentTypeID

tbBagHandling
tbBagHandling.BagID_tbBagAttributes.BagID
tbTransactions.TransactionID_tbBagHandling.TransactionID

tbBagAttributes
tbAttributes.AttributeID_tbBagAttributes.AttributeID
tbBagHandling.BagID_tbBagAttributes.BagID

tbAttributes
tbAttributes.AttributeID_tbBagAttributes.AttributeID


As you can see from the results below, when I join in both my passenger table and my bags tables, I get a result for every combination of bag/passenger.  I don't want to know which bag belongs to which passenger but I do need to make sure that each bag/attribute combination is only assigned to one passenger (seq).

Does anyone have an suggestions on how I can solve this?  If I need to put in more info, just let me know.

AJ




The results I want to get are (I left out a lot of fields):
 
Transaction ID	PNR	Seq	Bag Tag	Charge Description
1	748L8Z	1	1234567890	Size (63 - 80 in)
1	748L8Z	2	9876543210	Weight (71-99 lbs)
 
 
The results I am getting:
Transaction ID	PNR	Seq	Bag Tag	Charge Description
1	748L8Z	1	1234567890	Size (63 - 80 in)
1	748L8Z	2	1234567890	Size (63 - 80 in)
1	748L8Z	1	9876543210	Weight (71-99 lbs)
1	748L8Z	2	9876543210	Weight (71-99 lbs)

Open in new window

AJ0424Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Nathan RileyFounderCommented:
What syntax are you using now?  I assume your using all inner joins and no outer?
0
AJ0424Author Commented:
I am sorry...I forgot to actually post the query!  It is:

select 
	t.TransactionID as 'TransactionID',
	p.PNR,
	p.Sequence,
	bh.BagTag as 'BagTag',
	a.Description as 'Bag Charge Description',
	a.Charge as 'Line Amount',
	t.ChargeTotal as 'Charge Total',
	t.TipAmount as 'Tip Amount',
	'Transaction Total' = (t.ChargeTotal + t.TipAmount),
	pt.Description as 'Payment Type',
	'Card Info' = (t.CardLastFour + ' ' + t.CardCheckSum),
	t.AuthorizationCode as 'Authorization Code',
	t.RefundAuthorization as 'Refund Authorization',
	t.ChargeTimestamp as 'Charge Date',
	t.RefundTimestamp as 'Refund Date'
	
from tbTransactions t
inner join tbBagHandling bh on bh.TransactionID = t.TransactionID
inner join tbPaymentTypes pt on pt.PaymentTypeID = t.PaymentTypeID
inner join tbPassengers p on p.TransactionID = t.TransactionID
inner join tbBagAttributes ba on ba.BagID = bh.BagID
inner join tbAttributes a on a.AttributeID = ba.AttributeID

Open in new window

0
Nathan RileyFounderCommented:
Give this a try:
select 
        t.TransactionID as 'TransactionID',
        p.PNR,
        p.Sequence,
        bh.BagTag as 'BagTag',
        a.Description as 'Bag Charge Description',
        a.Charge as 'Line Amount',
        t.ChargeTotal as 'Charge Total',
        t.TipAmount as 'Tip Amount',
        'Transaction Total' = (t.ChargeTotal + t.TipAmount),
        pt.Description as 'Payment Type',
        'Card Info' = (t.CardLastFour + ' ' + t.CardCheckSum),
        t.AuthorizationCode as 'Authorization Code',
        t.RefundAuthorization as 'Refund Authorization',
        t.ChargeTimestamp as 'Charge Date',
        t.RefundTimestamp as 'Refund Date'
        
from tbTransactions t
left outer join tbBagHandling bh on bh.TransactionID = t.TransactionID
left outer join tbPaymentTypes pt on pt.PaymentTypeID = t.PaymentTypeID
left outer join tbPassengers p on p.TransactionID = t.TransactionID
left outer join tbBagAttributes ba on ba.BagID = bh.BagID
left outer join tbAttributes a on a.AttributeID = ba.AttributeID

Open in new window

0
i2mentalCommented:
This is going to happen because you are trying to display unrelated data in the same results. Your passengers are linked to a transaction and your bags are linked to a transaction. Unless you've got a way in your schema to also link a bag to a passenger, then that's what you're going to get.

If you can join between passengers and bags instead, I think you'll come out with the result you're looking for.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AJ0424Author Commented:
i2mental,

That is what I  was thinking but I am new enough that I was hoping there was another way around it that I was missing.  I guess I will have to go down that route.  Thanks.

Gallitin,
Unfortunately, that did not work but thank you for the help.

AJ
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.