Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# DateTime Group By question

Posted on 2004-04-15
Medium Priority
1,936 Views
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
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
• 2
• 2

LVL 75

Expert Comment

ID: 10833766
Group By DatePart(Week, Datetime)

Group By DatePart(Month, Datetime)
0

LVL 8

Expert Comment

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

Anthony Perkins earned 2000 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

ID: 10833871
thats a lot easier than my suggestion !!!
0

LVL 2

Expert Comment

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

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
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…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
###### Suggested Courses
Course of the Month5 days, 9 hours left to enroll