Skip to content

Instantly share code, notes, and snippets.

@mwilc0x
Created April 23, 2012 21:52
Show Gist options
  • Select an option

  • Save mwilc0x/2474154 to your computer and use it in GitHub Desktop.

Select an option

Save mwilc0x/2474154 to your computer and use it in GitHub Desktop.
Merging two SQL queries into one
I want to divide the correct count by total count (where the domain names match)
/* Get the total count */
SELECT DISTINCT(domain_name), COUNT(*) AS count
FROM recommendations
WHERE rec_date LIKE '2007%'
GROUP BY domain_name
ORDER BY count DESC;
/* Get the correct count */
SELECT DISTINCT(domain_name), COUNT(*) AS count
FROM recommendations
WHERE rec_date LIKE '2007%' AND direction = 'positive' AND adj_ret1d > 0
OR rec_date LIKE '2007%' AND direction = 'negative' AND adj_ret1d < 0
GROUP BY domain_name
ORDER BY count DESC;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment