Solved

DateTime Group By question

Posted on 2004-04-15
5
1,919 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 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

[Webinar] Disaster Recovery and Cloud Management

Learn from Unigma and CloudBerry industry veterans which providers are best for certain use cases and how to lower cloud costs, how to grow your Managed Services practice in IaaS clouds, and how to utilize public cloud for Disaster Recovery

Question has a verified solution.

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

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
I have a large data set and a SSIS package. How can I load this file in multi threading?
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.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

867 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now