Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
SergioM
Posting Yak Master
170 Posts |
Posted - 2013-07-29 : 16:07:32
|
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 tttiiddFROM `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' -SergioThis is on MySQL |
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-07-29 : 16:20:12
|
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.comLooking 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. |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2013-07-29 : 16:20:52
|
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 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2013-07-29 : 17:07:26
|
[code]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 tttiiddFROM dbo.Tracking AS tLEFT JOIN dbo.Vendor AS v ON v.id = t.vendorIDLEFT JOIN ( SELECT pkgTracking, MAX(dateUpdated) AS dateUpdated FROM dbo.Activity GROUP BY pkgTracking ) AS a ON a.pkgTracking = t.pkgTrackingWHERE t.pkgStatus BETWEEN '0' AND '3';[/code] Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA |
|
|
SergioM
Posting Yak Master
170 Posts |
Posted - 2013-07-30 : 14:06:33
|
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.comLooking 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 forumquote: 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.sqlquote: 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 |
|
|
|
|
|
|
|