July 6, 2005 at 1:24 pm
In english?
July 6, 2005 at 1:28 pm
Method #2:
Select DateName(YY, CrDate) as Year, DateName(M, CrDate) as Month, count(*) as Total
from dbo.SysObjects
where CrDate between @startdate and @enddate
group by DateName(YY, CrDate), DateName(M, CrDate), month(CrDate) --extra level
order by DateName(YY, CrDate), month(CrDate)
* Noel
July 6, 2005 at 1:31 pm
Nicer than my version... Looks like I still have to agree with you .
July 6, 2005 at 1:37 pm
Method #1
Select Left(Dte,4) Year, Right(Dte,2) Mon, Total
From
(Select convert(char(7), CrDate,20) Dte, count(*) as Total
from dbo.SysObjects
where CrDate between @startdate and @enddate
group by convert(char(7), CrDate,20)
) d
order by Dte
* Noel
July 6, 2005 at 1:39 pm
Dam! you keep typing faster than me
* Noel
July 6, 2005 at 1:46 pm
I still have to agree .
July 6, 2005 at 1:47 pm
I thaught that the month name was required in the select... but it obviously can be generated in the application. Thanx for the solutions.
July 6, 2005 at 1:55 pm
That is true is just me being lazy not to type a case statement including the Right. The point is that because you are grouping by one column expression instead of two the operation finishes very fast and you leave the complexity to the outer query to do the order by and Presentation stuff.
And Yes I'll have to learn to type faster I promise I will!
* Noel
July 6, 2005 at 2:05 pm
"operation finishes very fast and you leave the complexity to the outer query to do the order by and Presentation stuff."
As it should be...
And I'll try to type slower but I can't make any promess .
July 6, 2005 at 2:18 pm
select datepart(mm,date) as Mon,count(datepart(mm,date)) as Nbr
into ##Dates
from TableName
where datepart(yy,date) = 2005
group by date
having count(datepart(mm,date)) > 0
select [Month] = case
when mon = 1 then 'January'
when mon = 2 then 'Feburary'
when mon = 3 then 'March'
when mon = 4 then 'April'
when mon = 5 then 'May'
when mon = 6 then 'June'
when mon = 7 then 'July'
end,
[Number] = sum(nbr)
From ##Dates
group by mon
July 6, 2005 at 2:37 pm
No chance... not gonna risk locking up the temp systables for such a simple query... and do it a 2 steps too when only one is needed.
July 6, 2005 at 2:50 pm
He was Probably trying to demonstrate how to convert month number to characters
* Noel
July 6, 2005 at 2:52 pm
Maybe... but I'd still never use code like that in production.
July 7, 2005 at 12:30 am
Year included, sorted by month.
select
Year(entrydate) Y
, Month(entrydate) M
, datename(m,entrydate) MName
, count(*) C
from tblparticipants
where entrydate between @start and @end
group by
Year(entrydate)
, Month(entrydate)
, datename(m,entrydate)
order by
Year(entrydate)
, Month(entrydate)
July 7, 2005 at 9:23 am
declare @startdate datetime, @enddate datetime
select @startdate = '1/2/1996'
select @enddate = '1/4/1998'
select datename(m,OrderDate) + ' ' + Convert(char(4),year(OrderDate)) mon, count(*) cnt
from Northwind.dbo.Orders
where OrderDate between @startdate and @enddate
group by year(OrderDate), month(OrderDate), datename(m,OrderDate) + ' ' + Convert(char(4),year(OrderDate))
Order By year(OrderDate), month(OrderDate)
mon cnt
----------------------------------- -----------
July 1996 22
August 1996 25
September 1996 23
October 1996 26
November 1996 25
December 1996 31
January 1997 33
February 1997 29
March 1997 30
April 1997 31
May 1997 32
June 1997 30
July 1997 33
August 1997 33
September 1997 37
October 1997 38
November 1997 34
December 1997 48
January 1998 5
(19 row(s) affected)
Viewing 15 posts - 16 through 29 (of 29 total)
You must be logged in to reply to this topic. Login to reply