Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>For showing ALL Divisions, that will need to be first in the list, then LEFT JOINed to the count results. You'll need to COALESCE() the value so if null, will return the zero instead. As for the last 3 months, you'll just need to add a WHERE clause for the date range.</p> <p>Edit... per comment. I've taken it one step further and for each division, I've created it as a Cross-Tab to show the division with 3 columns showing the counts of each respective month in a single row... So, the resulting column names would be the same, but would reflect the actual month data content... Actually adjusted the WHERE clause going back only 2 months... 2 months back plus current equals 3 months total.</p> <pre><code> select d.DivisionName, SUM( case when PreQuery.ByMonth = DATENAME(Month, DateAdd( month, -2, GetDate()) then PreQuery.DistinctQuizes else 0 end ) as TwoMonthsAgoCount, SUM( case when PreQuery.ByMonth = DATENAME(Month, DateAdd( month, -1, GetDate()) then PreQuery.DistinctQuizes else 0 end ) as OneMonthAgoCount, SUM( case when PreQuery.ByMonth = DATENAME(Month, GetDate()) then PreQuery.DistinctQuizes else 0 end ) as CurrentMonthCount from Divisions d left join ( select count( distinct UQ.QuizID ) DistinctQuizes, DATENAME(Month, UQ.DateTimeComplete) ByMonth, d2.DivisionName from UserQuiz UQ JOIN Quiz Q on UQ.QuizID = Q.QuizID JOIN Employee E on UQ.UserName = E.UserName JOIN Divisions D2 on E.DivisionCode = D2.SapCode where UQ.DateTimeComplete between DateAdd( month, -2, GetDate()) and GetDate() group by d2.DivisionName, DATENAME(Month, UQ.DateTimeComplete) ) PreQuery ON d.DivisionName = PreQuery.DivisionName GROUP BY d.DivisionName </code></pre> <p>By using "GetDate()", this returns whatever the current date is on the computer. This would be the ENDING date. The first date would be basic date arithmetic... Use the DateAdd function, add an interval based on months of -2 (negative to go BACKWARDS), based on whatever the current date is.</p>
    singulars
    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.
 

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