Note that there are some explanatory texts on larger screens.

plurals
  1. POMySQL Using UNION or OUTER LEFT JOIN
    primarykey
    data
    text
    <p>I have been struggling with what seems a simple enough query. I have three tables, Accounts, Contacts &amp; Activities. Everytime an Activity is logged it logs the AccountID, Contact Subject, TimeDate.</p> <p>I query the Activities table to show me all the activity count from the previous week for each AccountID.</p> <p>I use:</p> <pre><code>select accounts.account as Account, count(distinct activities.contactid) as Users, from accounts, activities where activities.accountid=accounts.accountid AND completeddate &gt;= curdate() - INTERVAL DAYOFWEEK(curdate())+6 DAY AND completeddate &lt; curdate() - INTERVAL DAYOFWEEK(curdate())-1 DAY group by accounts.account asc; </code></pre> <p>The result is something like This: </p> <pre><code>Account Users ACME Ltd 4 Warner Bros 6 RBS 9 </code></pre> <p>etc..</p> <p>The activities table has baout 20 million rows and this runs in about 20 seconds.</p> <p>However, I want a comprehensive list. I want to combine the results with a list of AccountID's that havent had any activity for that month. </p> <pre><code>Account Users ACME Ltd 4 Warner Bros 6 RBS 9 Microsoft 0 or NULL </code></pre> <p>etc...</p> <p>i have tried to UNION like this:</p> <pre><code>select Account, '' from Accounts UNION select accounts.account as Account, count(distinct activities.contactid) as Users from accounts, activities where activities.accountid=accounts.accountid AND completeddate &gt;= curdate() - INTERVAL DAYOFWEEK(curdate())+6 DAY AND completeddate &lt; curdate() - INTERVAL DAYOFWEEK(curdate())-1 DAY group by accounts.account asc; </code></pre> <p>From what i understand about UNION is that it should return a unique list (without duplicates). But what i get is a list of aprox 1400 accounts with when I only have approx 900 Accounts.</p> <p>I have tried LEFT OUTER JOIN but this just seemed to run forever ( i killed it after 2 hours)</p> <p>Does anyone have any suggestions on what I can try?</p> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload