Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 3 of 3 total
Thread How to Group By Month or Year in SQL
Wed, Jul 29 2009 9:26 PMPermanent Link

"Gregory Sebastian"
Hello,
I have a table that included an order date and sales value among other
things. I want to return a query that just contains 2 columns. The months in
the form "MMM-YYYY" and the sum of the sales value for the month ordered by
month accending.

The following below code comes close but it just returns the Year Number and
month number. I could not find a way to return the month in the form
Jan-2009, Feb-2009 etc. Is there a way to do it in SQL or do I need to add a
calculated field in Delphi .

Regards
Gregory Sebastian
(DBISam 4.27)

/* Start SQL */

Select

Date,
Extract(Year, DocDate) As YearNo,
Extract(MONTH, DocDate) As "MonthNo",
Sum(SalesValue) As TotalSales

From "SalesTable"
Where Date Between :StartDate And :EndDate
Group By YearNo, MonthNo;

/*End SQL*/
Thu, Jul 30 2009 10:34 AMPermanent Link

"Robert"

"Gregory Sebastian" <gregorys@nospam-ezysoft-dev.com> wrote in message
news:7F1D8393-9EF0-4819-8C50-8B256CA1BCC6@news.elevatesoft.com...
>
> The following below code comes close but it just returns the Year Number
> and month number. I could not find a way to return the month in the form
> Jan-2009, Feb-2009 etc. Is there a way to do it in SQL or do I need to add
> a calculated field in Delphi .
>

Thre is no function that I know of in SQL to do that. You can do somehting
like this

Select
Case
Extract(month from my_time)
When 1 then 'Jan-' + cast(extract(year from my_time) as varchar(4))
When 2 then 'Feb-' + cast(extract(year from my_time) as varchar(4))
end as amonth
from mytable

Robert

Thu, Jul 30 2009 7:33 PMPermanent Link

"Gregory Sebastian"
> Select
> Case
> Extract(month from my_time)
> When 1 then 'Jan-' + cast(extract(year from my_time) as varchar(4))
> When 2 then 'Feb-' + cast(extract(year from my_time) as varchar(4))
> end as amonth
> from mytable


Nice.   Thanks Robert.

Cheers

--
Gregory Sebastian
Image