Last active
September 12, 2016 13:03
-
-
Save zarzen/593747c0c3fe2e0ef24654a2f0fb6811 to your computer and use it in GitHub Desktop.
Outer join equivalent
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT | |
distinct p1.cid, p2.cid, p1.pid | |
FROM | |
Purchase p1 | |
CROSS JOIN | |
Purchase p2 | |
WHERE | |
p1.cid <> p2.cid AND p1.cid < p2.cid and p1.pid = p2.pid | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT | |
sid, sname, COUNT(bid) | |
FROM | |
(SELECT | |
* | |
FROM | |
SAILORS | |
JOIN RESERVES USING (sid)) AS TEMP | |
GROUP BY sid , sname | |
UNION SELECT | |
s1.sid, s1.sname, COUNT(NULL) | |
FROM | |
SAILORS s1 | |
WHERE | |
NOT EXISTS( SELECT | |
s1.sid | |
FROM | |
RESERVES r2 | |
WHERE | |
s1.sid = r2.sid) | |
GROUP BY sid , sname; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT | |
p1.cid, p2.cid, p1.n + p2.n | |
FROM | |
(SELECT | |
cid, COUNT(Temp.pid) AS n | |
FROM | |
(SELECT DISTINCT | |
cid, pid | |
FROM | |
Purchase) AS Temp | |
GROUP BY Temp.cid) AS p1 | |
CROSS JOIN | |
(SELECT | |
cid, COUNT(Temp.pid) AS n | |
FROM | |
(SELECT DISTINCT | |
cid, pid | |
FROM | |
Purchase) AS Temp | |
GROUP BY Temp.cid) AS p2 ON p1.cid <> p2.cid AND p1.cid < p2.cid |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
http://stackoverflow.com/questions/15572333/left-outer-join-equivalent | |
http://stackoverflow.com/questions/18904778/sql-query-with-not-exists-not-working |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment