Solved

DateTime Group By question

Posted on 2004-04-15
5
1,928 Views
Last Modified: 2009-12-16
I am writing a report and the users and they would like to select the grouping of the report.

All the grouping will be on a single datetime field (oddly enough named datetime).

They would like to be able to choose if the report is grouped by half hour, day, week, or month.

I can accomplish day by using:
GROUP BY CONVERT(VARCHAR(10),DateTime, 110)

But how can I accomplish half hour, week, or month?

Many thanks for the help.
0
Comment
Question by:sifuhall
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 10833766
Group By DatePart(Week, Datetime)

Group By DatePart(Month, Datetime)
0
 
LVL 8

Expert Comment

by:plq
ID: 10833790
Without giving you a red carpet solution heres some clues

select convert(float, getdate())

returns a VB style date number

You can then round this off with number arithmetic

You can convert these numbers back into dates using the convert function
0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 500 total points
ID: 10833822
Group By DatePart(Week, [Datetime])

Group By DatePart(Month, [Datetime])

Group by Year([DateTime]), Month([DateTime]), Day([DateTime]), DatePart(Minute, [DateTime])/30
0
 
LVL 8

Expert Comment

by:plq
ID: 10833871
thats a lot easier than my suggestion !!!
0
 
LVL 2

Expert Comment

by:BoyPupa
ID: 10835961
I'm assuming by your example for day that when you say group by week you would not want similar weeks form different years grouped together.  If this is the case then you should look into BOL under "datepart"

some examples:
--group by week
group by datepart(year,[datetime])
             ,datepart(month,[datetime])  

alternatively if you, for some reason, do not want to use subgroups you can build your group by column
examples :
--group by month
group by 100*datepart(year,[datetime])+datepart(month,[datetime])  

--group by week
group by 100*datepart(year,[datetime])+datepart(week,[datetime])

--group by day
group by 1000*datepart(year,[datetime])+datepart(dayofyear,[datetime])

--group by hour
group by 1000000*datepart(year,getdate())+1000*datepart(dayofyear,getdate()) + 10* datepart(hour,getdate())

-- Half Hour is a little different since there is no native datpart with that name
group by 1000000*datepart(year,getdate())+1000*datepart(dayofyear,getdate()) + 10* datepart(hour,getdate()) + datepart(minute,getdate())%30  







0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question