|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Today @ 8:21 PM
Points: 32,893,
Visits: 26,765
|
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Today @ 8:21 PM
Points: 32,893,
Visits: 26,765
|
|
Heh... Yowch! I guess I should only expect 3 stars because these types of articles are so very short and written to specific problems that several folks said they'd like to see. Still, it would be nice to hear back from those folks that gave the lower ratings so we can find out what they'd really like to see.
--Jeff Moden "RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".
First step towards the paradigm shift of writing Set Based code: Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."
For better, quicker answers on T-SQL questions, click on the following... http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following... http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Monday, March 26, 2012 6:59 AM
Points: 4,
Visits: 19
|
|
You could go for casting the month numbers to names in the select rather than in the ORDER BY clause, as it allows you to use MONTH in most places, and DATENAME only once, which feels more natural:
SELECT [Month] = DATENAME(mm, DATEADD(mm, MONTH(SomeDateTime), 0)), Amount = SUM(SomeAmount) FROM #MyHead WHERE SomeDateTime >= '2010' AND SomeDateTime < '2011' GROUP BY MONTH(SomeDateTime) ORDER BY MONTH(SomeDateTime)
|
|
|
|
|
SSC Journeyman
      
Group: General Forum Members
Last Login: Yesterday @ 3:42 AM
Points: 88,
Visits: 241
|
|
Hi Jeff, I might consider using something like this approach
CREATE TABLE [dbo].[Months]( [MonthName] [nvarchar](10) NOT NULL, [MonthCalendarSequence] [int] NOT NULL, [MonthFinancialSequance] [int] NOT NULL ) ON [PRIMARY]
Insert into Months Values ('January',1,10) Insert into Months Values ('February',2,11) Insert into Months Values ('March',3,12) Insert into Months Values ('April',4,1) Insert into Months Values ('May',5,2) Insert into Months Values ('June',6,3) Insert into Months Values ('July',7,4) Insert into Months Values ('August',8,5) Insert into Months Values ('September',9,6) Insert into Months Values ('October',10,7) Insert into Months Values ('November',11,8) Insert into Months Values ('December',12,9)
SELECT [Month] = DATENAME(mm,SomeDateTime), Amount = SUM(SomeAmount) FROM #MyHead h inner join months m on m.monthname = DATENAME(mm,SomeDateTime) WHERE SomeDateTime >= '2010' AND SomeDateTime < '2011' GROUP BY DATENAME(mm,SomeDateTime) order by max(m.monthcalendarsequence)
Can't really decide what would be best
Pete
|
|
|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: Thursday, October 18, 2012 3:16 AM
Points: 264,
Visits: 178
|
|
Maybe the low ratings is due to the fact that most programmers and db-developers "know" this solution already: The old "sort numbers stored as text as numbers"... ("1" ,"2"..."10", "11" and NOT "1", "10", "11", "2", "3"...).
I like the technique, but you should clarify "any year" is not actually any year, it is a valid year within the sql server time span...
|
|
|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: Monday, May 13, 2013 3:02 AM
Points: 235,
Visits: 560
|
|
Jeff Moden (11/15/2010) Still, it would be nice to hear back from those folks that gave the lower ratings . . . I didn't give a low rating, but found a slip of the pen (it confused me for a moment).
-- This builds a table with random dates and amounts for 20 years -- starting in the year 2000. SELECT TOP (1000000) SomeDateTime = RAND(CHECKSUM(NEWID()))*7305 + CAST('2005' AS DATETIME),
The 2000 from the comment is not the same as the CAST('2005' AS DATETIME)
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Wednesday, May 15, 2013 11:34 PM
Points: 42,
Visits: 218
|
|
Another variaton, using the MONTH function:
SELECT [Month] = DATENAME(mm,SomeDateTime), Amount = SUM(SomeAmount) FROM #MyHead WHERE SomeDateTime >= '2010' AND SomeDateTime < '2011' GROUP BY DATENAME(mm,SomeDateTime), MONTH(SomeDateTime) ORDER BY MONTH(SomeDateTime) ;
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Tuesday, February 05, 2013 9:25 AM
Points: 21,
Visits: 25
|
|
What happens when you're reporting on a period greater than a year?
Won't both of the overlapping month's figures will be aggregated into a single month aggregate total?
|
|
|
|
|
SSC Journeyman
      
Group: General Forum Members
Last Login: Today @ 2:03 AM
Points: 82,
Visits: 557
|
|
How about this:
select datename(M,convert(varchar,months)+'/01/2010') as days from (select 1 as months union select 2 as months union select 3 as months union select 4 as months union select 5 as months union select 6 as months union select 7 as months union select 8 as months union select 9 as months union select 10 as months union select 11 as months union select 12 as months ) as M order by convert(datetime,convert(varchar,months)+'/01/2010')
~manoj sqlwithmanoj.wordpress.com
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Monday, September 17, 2012 7:30 AM
Points: 1,038,
Visits: 679
|
|
SELECT [Month] = DATENAME(mm,SomeDateTime),Year(Somedatetime), Amount = SUM(SomeAmount) FROM #MyHead GROUP BY DATENAME(mm,SomeDateTime),Year(Somedatetime) ORDER BY CAST(DATENAME(mm,SomeDateTime) + ' 1900' AS DATETIME),Year(Somedatetime)
james.wheeler10 (11/15/2010) What happens when you're reporting on a period greater than a year?
Won't both of the overlapping month's figures will be aggregated into a single month aggregate total?
|
|
|
|