SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Eliminating a Cartesian product
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

SergioM
Posting Yak Master

137 Posts

Posted - 07/29/2013 :  16:07:32  Show Profile  Reply with Quote
I have a SQL statement with two left outer joins which connects 3 tables. Vendors, Tracking & Activity. For whatever reason, even though each is a one-to-many relationship, I am able to join 2 tables (from Vendors to Tracking) without an issue. when I then join Activity, I get a Cartesian product.

I suspected that 'DISTINCT' would help, but it doesn't. Any ideas?

SELECT DISTINCT CASE 
    WHEN `vendor`.`companyname` IS NULL then 'No Company Assigned'
    ELSE `vendor`.`companyname`
END AS companyNameSQL, `tracking`.`pkgTracking`, CASE 
    WHEN `tracking`.`pkgStatus` = '0' then 'No Data'
    WHEN `tracking`.`pkgStatus` = '1' then 'Initiated'
    WHEN `tracking`.`pkgStatus` = '2' then 'In Transit'
    WHEN `tracking`.`pkgStatus` = '3' then 'Delivery Exception'
END AS Status, `activity`.`dateUpdated` , `tracking`.`id`  as tttiidd
FROM `tracking`
LEFT OUTER JOIN `vendor`
ON `tracking`.`vendorID` = `vendor`.`id`
LEFT OUTER JOIN `activity`
ON `activity`.`pkgTracking` = `tracking`.`pkgTracking`

WHERE  `tracking`.`pkgStatus` !='4' AND `tracking`.`pkgStatus` !='5'


-Sergio
This is on MySQL

Edited by - SergioM on 07/29/2013 16:08:27

James K
Flowing Fount of Yak Knowledge

3323 Posts

Posted - 07/29/2013 :  16:20:12  Show Profile  Reply with Quote
This forum is for Microsoft SQL Server, so there may be very few MySQL experts here. You might get faster and more accurate answers at a MySQL forum or other generalized forums such as dbforums.com

Looking from T-SQL perspective, I don't see anything wrong with the query unless you were not joining on all columns that you should when you join the activity table.
Go to Top of Page

Lamprey
Flowing Fount of Yak Knowledge

4354 Posts

Posted - 07/29/2013 :  16:20:52  Show Profile  Reply with Quote
Maybe you need to add another column to your join or that's just how the data is. Hard to tell without any knowledge of your system.

http://www.sqlservercentral.com/articles/Best+Practices/61537/
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

Sweden
29910 Posts

Posted - 07/29/2013 :  17:07:26  Show Profile  Visit SwePeso's Homepage  Reply with Quote
SELECT		COALESCE(v.CompanyName, 'No Company Assigned') AS companyNameSQL,
		t.pkgTracking,
		CASE 
			WHEN t.pkgStatus = '0' then 'No Data'
			WHEN t.pkgStatus = '1' then 'Initiated'
			WHEN t.pkgStatus = '2' then 'In Transit'
			WHEN t.pkgStatus = '3' then 'Delivery Exception'
		END AS [Status],
		a.dateUpdated,
		t.id AS tttiidd
FROM		dbo.Tracking AS t
LEFT JOIN	dbo.Vendor AS v ON v.id = t.vendorID
LEFT JOIN	(
			SELECT		pkgTracking,
					MAX(dateUpdated) AS dateUpdated
			FROM		dbo.Activity
			GROUP BY	pkgTracking
		) AS a ON a.pkgTracking = t.pkgTracking
WHERE		t.pkgStatus BETWEEN '0' AND '3';



Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Go to Top of Page

SergioM
Posting Yak Master

137 Posts

Posted - 07/30/2013 :  14:06:33  Show Profile  Reply with Quote
quote:
Originally posted by James K

This forum is for Microsoft SQL Server, so there may be very few MySQL experts here. You might get faster and more accurate answers at a MySQL forum or other generalized forums such as dbforums.com

Looking from T-SQL perspective, I don't see anything wrong with the query unless you were not joining on all columns that you should when you join the activity table.


Thanks. I've also posted it to the MySQL forum

quote:
Originally posted by Lamprey

Maybe you need to add another column to your join or that's just how the data is. Hard to tell without any knowledge of your system.

Hmm, it always seemed pushy to me to add a sql dump, but if that's the norm, I'm happy to do it. I've stripped it down to just the one tracking number & the 4 activity rows which result in the cartesian product.

http://u2xs.com/tmp/shipStatus.sql

quote:
Originally posted by SwePeso

I can't seem to get it to work in MySQL. I've removed the dbo references and changed them to MySQL friendly format, but I must be missing something.

-Sergio

Edited by - SergioM on 07/30/2013 14:08:47
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000