Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4567574
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T18:57:02+00:00 2026-05-21T18:57:02+00:00

It returns all rows sorted by CountOfRecords DESC. I’d like to have just seven

  • 0

It returns all rows sorted by CountOfRecords DESC. I’d like to have just seven rows here, showing the highest value ever for the specified day. So each day of the week should occur once and only once. Is this possible without a lot of pain?

SELECT 
    Count(*) As CountOfRecords, 
    CAST(FLOOR(CAST([visit].[datetimeentered] AS float)) AS smalldatetime) AS DateEntered, 
    DatePart(dw, visit.datetimeentered) As DayOfTheWeek
FROM visit
INNER JOIN useragent ON useragent.useragentid = visit.useragentid
WHERE useragent.isbot = 0
GROUP BY CAST(FLOOR(CAST([visit].[datetimeentered] AS float)) AS smalldatetime), 
    DatePart(dw, visit.datetimeentered)
ORDER BY CountOfRecords DESC

Edit1:
I think both answers are getting at the same thing. I’ve accepted the one that was posted first. I’m also going to mention that it worked right off the bat while the other did not. I’m getting the following errors when I run marc_s’s query:

Msg 207, Level 16, State 1, Line 29
Invalid column name 'RowNum'.
Msg 207, Level 16, State 1, Line 25
Invalid column name 'CountOfRecords'.
Msg 207, Level 16, State 1, Line 26
Invalid column name 'DateEntered'.
Msg 207, Level 16, State 1, Line 27
Invalid column name 'DayOfTheWeek'.

Special thanks to marc_s for pointing out a simple way to get the only the date portion from a datetime.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T18:57:02+00:00Added an answer on May 21, 2026 at 6:57 pm

    Based on your query:

    WITH groupedByDay AS (
      SELECT 
        Count(*) As CountOfRecords, 
        CAST(FLOOR(CAST([visit].[datetimeentered] AS float)) AS smalldatetime) AS DateEntered
      FROM visit
      INNER JOIN useragent ON useragent.useragentid = visit.useragentid
      WHERE useragent.isbot = 0
      GROUP BY CAST(FLOOR(CAST([visit].[datetimeentered] AS float)) AS smalldatetime)
    ),
    ranked AS (
      SELECT
        CountOfRecords,
        DateEntered,
        DOW = DATENAME(dw, DateEntered),
        rank = ROW_NUMBER() OVER (PARTITION BY DatePart(dw, DateEntered)
                                  ORDER BY CountOfRecords DESC)
      FROM groupedByDay
    )
    SELECT
      CountOfRecords,
      DateEntered,
      DOW
    FROM ranked
    WHERE rank = 1
    ORDER BY CountOfRecords DESC
    

    This will return one row for each day of week represented in the table. If there may be duplicates by CountOfRecords among the maximum values and you want to return them all, use RANK() instead of ROW_NUMBER().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this d[d$Age > 2, ] and it returns all the rows that
MyModel.objects.filter(field__icontains=value) returns all the rows whose field contains value . How to do the
I have this query which returns all rows from a table: select Cost, Name,
I have a LINQ from SQL query that returns sorted rows. For instance, it
How would I go about doing a query that returns results of all rows
I have an SQL query that returns all companies records ignore the ones with
The below function returns all the rows in Files table even when the where
I would like to get the top N rows from an Oracle table sorted
I want to find all the rows with the same value in. (Or at
If I used an Index column in a query which returns all the Rows,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.