Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Not mentioning the RDBMS, lets say MS SQL</p> <pre><code>SELECT TopicID, COUNT(1) TotalCount FROM TopicComments WHERE CommentDate BETWEEN @StartDate AND @EndDate GROUP BY TopicID ORDER BY TotalCount DESC </code></pre> <p>So a <strong>full example</strong> would look something like</p> <pre><code>DECLARE @TopicComments TABLE( TopicID INT, UserID INT, CommentDate DATETIME ) INSERT INTO @TopicComments SELECT 1, 1, '14 Feb 2010' INSERT INTO @TopicComments SELECT 1, 1, '14 Feb 2010' INSERT INTO @TopicComments SELECT 1, 2, '14 Feb 2010' INSERT INTO @TopicComments SELECT 1, 2, '14 Feb 2010' INSERT INTO @TopicComments SELECT 2, 1, '14 Feb 2010' INSERT INTO @TopicComments SELECT 2, 1, '14 Feb 2010' INSERT INTO @TopicComments SELECT 2, 2, '05 Feb 2010' DECLARE @StartDate DATETIME, @EndDate DATETIME SELECT @StartDate = '10 Feb 2010', @EndDate = '14 Feb 2010' SELECT TopicID, COUNT(1) TotalCount FROM @TopicComments WHERE CommentDate BETWEEN @StartDate AND @EndDate GROUP BY TopicID ORDER BY TotalCount DESC </code></pre> <p>Results</p> <pre><code>TopicID TotalCount ----------- ----------- 1 4 2 2 </code></pre> <p>To select the <em>TOP 1</em> would then be</p> <pre><code>SELECT TOP 1 TopicID, COUNT(1) TotalCount FROM @TopicComments WHERE CommentDate BETWEEN @StartDate AND @EndDate GROUP BY TopicID ORDER BY TotalCount DESC </code></pre> <p><strong>EDIT</strong></p> <p>Use somthing like</p> <pre><code>DECLARE @StartDate DATETIME, @EndDate DATETIME SET @EndDate = DATEADD(D, 0, DATEDIFF(D, 0, GETDATE())) --returns only the date part of GETDATE() SET @StartDate = @EndDate - 5 </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.
    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