The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Databases
> MySQL Help
|
Weird join - PART II
Discuss Weird join - PART II in the MySQL Help forum on Dev Shed. Weird join - PART II MySQL Help forum discussing administration, SQL syntax, and other MySQL-related topics. MySQL is an open-source relational database management system (RDBMS).
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

October 15th, 2012, 01:49 PM
|
|
Contributing User
|
|
Join Date: Oct 2012
Posts: 35
Time spent in forums: 4 h 15 m 8 sec
Reputation Power: 1
|
|
|
Weird join - PART II
I posted before on this and thanks to you guys it works great.
I forgot to mention however that I need SQL to return results even if no record exists in xferleads.
More help would be appreciated!
...and yes I know this is a weird db layout.
Code:
SELECT
customers.customer
, customers.ac0 AS ac
, customers.xfer_no2
, customers.state
, customers.carrier
, customers.lead_type
, customers.criteria
, customers.pri
, MAX(xferleads.timestamp) AS maxTimeStamp
FROM customers
JOIN xferleads ON xferleads.customer = customers.id
WHERE customers.ac0 IS NOT NULL AND pri <> 'NONE' AND customers.ac0 > 1
GROUP BY
customers.customer
, customers.ac0
, customers.xfer_no2
, customers.state
, customers.carrier
, customers.lead_type
, customers.criteria
, customers.pri
UNION ALL SELECT
customers.customer
, customers.ac1
, customers.xfer_no2
, customers.state
, customers.carrier
, customers.lead_type
, customers.criteria
, customers.pri
, MAX(xferleads.timestamp) AS maxTimeStamp
FROM customers
JOIN xferleads ON xferleads.customer = customers.id
WHERE customers.ac1 IS NOT NULL AND pri <> 'NONE' AND customers.ac1 > 1
GROUP BY
customers.customer
, customers.ac0
, customers.xfer_no2
, customers.state
, customers.carrier
, customers.lead_type
, customers.criteria
, customers.pri
UNION ALL SELECT
customers.customer
, customers.ac2
, customers.xfer_no2
, customers.state
, customers.carrier
, customers.lead_type
, customers.criteria
, customers.pri
, MAX(xferleads.timestamp) AS maxTimeStamp
FROM customers
JOIN xferleads ON xferleads.customer = customers.id
WHERE customers.ac2 IS NOT NULL AND pri <> 'NONE' AND customers.ac2 > 1
GROUP BY
customers.customer
, customers.ac0
, customers.xfer_no2
, customers.state
, customers.carrier
, customers.lead_type
, customers.criteria
, customers.pri
UNION ALL SELECT
customers.customer
, customers.ac3
, customers.xfer_no2
, customers.state
, customers.carrier
, customers.lead_type
, customers.criteria
, customers.pri
, MAX(xferleads.timestamp) AS maxTimeStamp
FROM customers
JOIN xferleads ON xferleads.customer = customers.id
WHERE customers.ac3 IS NOT NULL AND pri <> 'NONE' AND customers.ac3 > 1
GROUP BY
customers.customer
, customers.ac0
, customers.xfer_no2
, customers.state
, customers.carrier
, customers.lead_type
, customers.criteria
, customers.pri
UNION ALL SELECT
customers.customer
, customers.ac4
, customers.xfer_no2
, customers.state
, customers.carrier
, customers.lead_type
, customers.criteria
, customers.pri
, MAX(xferleads.timestamp) AS maxTimeStamp
FROM customers
JOIN xferleads ON xferleads.customer = customers.id
WHERE customers.ac4 IS NOT NULL AND pri <> 'NONE' AND customers.ac4 > 1
GROUP BY
customers.customer
, customers.ac0
, customers.xfer_no2
, customers.state
, customers.carrier
, customers.lead_type
, customers.criteria
, customers.pri
ORDER BY ac ASC, pri DESC, maxTimeStamp ASC
|

October 15th, 2012, 04:09 PM
|
|
|
|
Left Join
|

October 15th, 2012, 04:19 PM
|
|
Contributing User
|
|
Join Date: Oct 2012
Posts: 35
Time spent in forums: 4 h 15 m 8 sec
Reputation Power: 1
|
|
|
duh...
thanks man!
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|