Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>After several hours of research and rethinking I determined how to do this. Basically, we need to be selecting each field (calendar.datefield, the count for location 1, the count for locaiton 2, the count for location 3...)</p> <p>To get the count I did a LEFT OUTER JOIN of the subquery that is right joined to the calendar. This makes the subquery return the count for each individual day (grouped by each calendar day) and this gets left outer joined (as mentioned a few seconds ago) to the calendar.datefield which is then filtered by the where clause between dates a and b.</p> <p>Then rinse and repeat that left outer join of each subquery. I am not an SQL master and I never went to school for this so it may not be optimized or perfect but it works and works fairly quickly. I am just glad to have figured it out.</p> <pre><code> select calendar.datefield, (ifnull(tOHS.Count,0)+ifnull(tOMSS.Count,0)+ifnull(tOMSN.Count,0)) as 'Total', ifnull(tOHS.Count,0) as 'OHS', ifnull(tOMSS.Count,0) as 'OMS-S', ifnull(tOMSN.Count,0) as 'OMS-N' from calendar LEFT OUTER JOIN ( Select workorders.school, Count(workorders.dateSubmitted) As 'Count', FROM_UNIXTIME(workorders.dateSubmitted, '%Y-%m-%d') as 'Date' From workorders Right Join calendar On (FROM_UNIXTIME(workorders.dateSubmitted, '%Y-%m-%d') = calendar.datefield) Where School = 'OHS' Group By calendar.datefield,workorders.school ) tOHS on calendar.datefield = tOHS.Date LEFT OUTER JOIN ( Select workorders.school, Count(workorders.dateSubmitted) As 'Count', FROM_UNIXTIME(workorders.dateSubmitted, '%Y-%m-%d') as 'Date' From workorders Right Join calendar On (FROM_UNIXTIME(workorders.dateSubmitted, '%Y-%m-%d') = calendar.datefield) Where School = 'OMS-S' Group By calendar.datefield,workorders.school ) tOMSS on calendar.datefield = tOMSS.Date LEFT OUTER JOIN ( Select workorders.school, Count(workorders.dateSubmitted) As 'Count', FROM_UNIXTIME(workorders.dateSubmitted, '%Y-%m-%d') as 'Date' From workorders Right Join calendar On (FROM_UNIXTIME(workorders.dateSubmitted, '%Y-%m-%d') = calendar.datefield) Where School = 'OMS-N' Group By calendar.datefield,workorders.school ) tOMSN on calendar.datefield = tOMSN.Date WHERE calendar.datefield Between '2011-08-30' And date_format(now(), '%Y-%m-%d') </code></pre> <p>This returns dates from range a to b and gives total for 3 buildings and each building. I have 4 buildings but it is easy to add as many as you want by copy and pasting each LEFT OUTER JOIN down to the on clause and changing the variables then adding a new select for that count.</p> <p>Wow this stuff can get confusing. hope it helps someone someday :-)</p> <p>Bil</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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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