Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If this is an SQL Server database, then your query would be something along the lines of:</p> <pre><code>SELECT CODE , SUM(1) AS DAYS , SUN = SUM(CASE WHEN DATEPART(weekday, TimeStamp) = 0 THEN 1 ELSE 0 END) , MON = SUM(CASE WHEN DATEPART(weekday, TimeStamp) = 1 THEN 1 ELSE 0 END) , TUE = SUM(CASE WHEN DATEPART(weekday, TimeStamp) = 2 THEN 1 ELSE 0 END) , WED = SUM(CASE WHEN DATEPART(weekday, TimeStamp) = 3 THEN 1 ELSE 0 END) , THURS = SUM(CASE WHEN DATEPART(weekday, TimeStamp) = 4 THEN 1 ELSE 0 END) , FRI = SUM(CASE WHEN DATEPART(weekday, TimeStamp) = 5 THEN 1 ELSE 0 END) , SAT = SUM(CASE WHEN DATEPART(weekday, TimeStamp) = 6 THEN 1 ELSE 0 END) FROM Event GROUP BY code </code></pre> <p>Where <code>Timestamp</code> is the name of the column containing the date of the training and <code>Event</code> is the name of the table.</p> <p><strong>Update</strong></p> <p>The above query assumes that one record exists in the database for each student for each day they attend a class.</p> <p>However, if the database design is such that there is one record for the session that specifies a start date and a number of days and another table where attendees are registered for a given session, then the query becomes a bit different.</p> <p>Assuming the following rough table structure:</p> <pre><code>CREATE TABLE Sessions ( code INT NOT NULL IDENTITY(1, 1) , date DATETIME NOT NULL , DAYS INT NOT NULL , PRIMARY KEY (code) ) CREATE TABLE Attendees ( id INT NOT NULL IDENTITY(1, 1) , code INT NOT NULL , NAME NVARCHAR(100) PRIMARY KEY (id) , FOREIGN KEY (code) REFERENCES sessions (code) ) </code></pre> <p>We first create a view to calculate the start and end day of week (this makes ensuing views easier to understand):</p> <pre><code>CREATE VIEW SessionDOW AS SELECT CODE , DAYS , StartDOW = DATEPART(weekday, date) , EndDOW = DATEPART(weekday, date) + DAYS FROM Sessions GO </code></pre> <p>We then create a view to record 1 for each day of the week that the session is running. Note that this view takes into account a session that spans two weeks:</p> <pre><code>CREATE VIEW SessionDays AS SELECT CODE , DAYS , SUN = CASE WHEN (0 BETWEEN StartDOW AND EndDOW) OR (EndDow &gt;= 0 + 7) THEN 1 ELSE 0 END , MON = CASE WHEN (1 BETWEEN StartDOW AND EndDOW) OR (EndDow &gt;= 1 + 7) THEN 1 ELSE 0 END , TUE = CASE WHEN (2 BETWEEN StartDOW AND EndDOW) OR (EndDow &gt;= 2 + 7) THEN 1 ELSE 0 END , WED = CASE WHEN (3 BETWEEN StartDOW AND EndDOW) OR (EndDow &gt;= 3 + 7) THEN 1 ELSE 0 END , THURS = CASE WHEN (4 BETWEEN StartDOW AND EndDOW) OR (EndDow &gt;= 4 + 7) THEN 1 ELSE 0 END , FRI = CASE WHEN (5 BETWEEN StartDOW AND EndDOW) OR (EndDow &gt;= 5 + 7) THEN 1 ELSE 0 END , SAT = CASE WHEN (6 BETWEEN StartDOW AND EndDOW) OR (EndDow &gt;= 6 + 7) THEN 1 ELSE 0 END FROM SessionDOW GO </code></pre> <p>Finally, we join the sessiondays count with the students attending each session and add up the values:</p> <pre><code> SELECT e.CODE , MAX(DAYS) AS DAYS , SUN = SUM(SUN) , MON = SUM(MON) , TUE = SUM(TUE) , WED = SUM(WED) , THURS = SUM(THURS) , FRI = SUM(FRI) , SAT = SUM(SAT) FROM SessionDays e INNER JOIN Attendees a ON e.code = a.code GROUP BY e.code </code></pre>
    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.
 

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