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

  • SEARCH
  • Home
  • 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 8055179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:18:48+00:00 2026-06-05T08:18:48+00:00

I have the following query select datepart(yy, orderDate) as ‘year’, datename(mm, OrderDate) as ‘month’,

  • 0

I have the following query

select
    datepart(yy, orderDate) as 'year',
    datename(mm, OrderDate) as 'month',
    count(*) as 'Orders'
from orders     (yeah I know its missing the schema, its from a book)
group by
    datepart(yy, orderdate),
    datename(mm, orderdate)
order by
    datepart(yy, orderdate),
    datename(mm, orderdate);

which returns 3 columns but datename(mm, orderdate) returns a string and therefore ordering by it puts August before January etc.

The solution to this was the following:

select
    datepart(yy, orderDate) as 'year',
    datename(mm, OrderDate) as 'month',
    count(*) as 'Orders'
from orders  (yeah i know its missing the schema, its from a book)
group by
    datepart(yy, orderdate),
    datename(mm, orderdate),
    datepart(mm, orderdate)
order by
    datepart(yy, orderdate),
    datepart(mm, orderdate);

I’m still a little confused with the whole group by / order by sections and how it actually works.

As far as I have understood, group by is creating a work table with 4 columns (that might be wrong) datepart(yy, orderdate), datename(mm, orderdate), datepart(mm, orderdate), and a count column.

Each time it encounters an orderdate that it has in the work table it increases the count,
otherwise it adds a new row?

Originally I thought I could remove the DateName(mm, orderdate) from within the group by section but the book said that’s not possible.

If someone could step through what actually happens behind the scenes/point out a resource which explains how this works in a little more detail id appreciate it.

thanks for your help.

  • 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-06-05T08:18:49+00:00Added an answer on June 5, 2026 at 8:18 am

    Any time you use an aggregate function (COUNT,SUM,MAX, etc.) you need to include all other columns in a GROUP BY clause. COUNT in your example is returning the number of records that have the same value for datepart(yy, orderdate), datename(mm, orderdate), datepart(mm, orderdate).

    An example:

    SELECT col1, col2, col3, MAX(col4)
    FROM MyTable
    GROUP BY col1, col2, col3
    

    Say this returned:

    1,2,3,9
    1,2,5,9
    

    If you changed your query to this:

    SELECT col1, col2, MIN(col3), MAX(col4)
    FROM MyTable
    GROUP BY col1, col2
    

    It would return:

    1,2,3,9
    

    Notice that I added an Aggregate function to col3 (MIN) so I was able to remove col3 from my GROUP BY clause.

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

Sidebar

Related Questions

I have the following query: SELECT COUNT(*) FROM FirstTable ft INNER JOIN SecondTable st
I have the following query: SELECT DISTINCT w.name, count(*) FROM widgets AS w JOIN
I have the following query SELECT COUNT(*) FROM Table1 WHERE Column1 IN (SELECT Column1
I have the following query: select column_name, count(column_name) from table group by column_name having
I have the following query: select count(L.ID) from LA inner join L on (LA.leadid
i have following query SELECT *, count(jx_commissions.commission_amount) AS summe FROM jx_members INNER JOIN jx_commissions
I have following SQL query: SELECT Count(*) AS CountOfRecs FROM tblAccount INNER JOIN tblAccountOwner
I have following (simplified) query: SELECT ResolvedBy, COUNT(*) AS Count, fiCategory, fiSubCategory, fiSymptom FROM
I have the following query SELECT Count(*) as Total_Count, Col1 FROM Table1 GROUP BY
I have the following query: SELECT COLUMN1, COLUMN2, COUNT(*) FROM TABLE WHERE COLUMN3 IS

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.