Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

DateTime Group By question

Posted on 2004-04-15
5
Medium Priority
?
1,941 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
  • 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 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

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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
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.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

916 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