Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Grouping

Author  Topic 

pravin14u
Posting Yak Master

246 Posts

Posted - 2008-04-19 : 06:40:15
I have 2 columns

RecordDate(DateTime)
Quantity(Int)

I would like to group the data quarter wise like,

Quarter 1 500
Quarter 2 340
Quarter 3 450
Quarter 4 400

Note: Say quarters are fixed from Jan-Mar,Apr-Jun etc.,



Prakash.P
The secret to creativity is knowing how to hide your sources!

nr
SQLTeam MVY

12543 Posts

Posted - 2008-04-19 : 06:54:18
select q, qty = sum(qty)
from
(
select q = 'Quarter ' + convert(varchar(10),datepart(qq,RecordDate)), qty = sum(Quantity)
from tbl
) a
group by q

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

PeterNeo
Constraint Violating Yak Guru

357 Posts

Posted - 2008-04-19 : 07:12:55
try this with slight modifications

select Year, q, qty = sum(qty)
from
(
select Year(RecordDate) as Year, q = 'Quarter ' + convert(varchar(10),datepart(qq,RecordDate)), qty = sum(Quantity)
from tbl
Group by Year(RecordDate), convert(varchar(10),datepart(qq,RecordDate))
) a
group by Year,q
Go to Top of Page
   

- Advertisement -