Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12345»»»

Sorting Months By Number (SQL Spackle) Expand / Collapse
Author
Message
Posted Saturday, November 13, 2010 1:33 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 12:54 PM
Points: 35,607, Visits: 32,193
Comments posted to this topic are about the item Sorting Months By Number (SQL Spackle)

--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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1020369
Posted Monday, November 15, 2010 1:05 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 12:54 PM
Points: 35,607, Visits: 32,193
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1020583
Posted Monday, November 15, 2010 1:44 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, August 13, 2013 8:44 AM
Points: 5, Visits: 26
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)

Post #1020596
Posted Monday, November 15, 2010 1:50 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Thursday, November 14, 2013 1:05 AM
Points: 96, Visits: 277
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
Post #1020598
Posted Monday, November 15, 2010 1:53 AM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Wednesday, May 28, 2014 12:41 PM
Points: 324, Visits: 188
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...
Post #1020599
Posted Monday, November 15, 2010 2:30 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: 2 days ago @ 9:53 AM
Points: 251, Visits: 665
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)



Post #1020613
Posted Monday, November 15, 2010 2:50 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, November 21, 2014 12:03 AM
Points: 44, Visits: 334
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)
;

Post #1020625
Posted Monday, November 15, 2010 3:01 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Today @ 7:02 AM
Points: 21, Visits: 29
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?
Post #1020631
Posted Monday, November 15, 2010 3:31 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Today @ 2:02 AM
Points: 86, Visits: 765
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
Post #1020640
Posted Monday, November 15, 2010 4:11 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Tuesday, November 18, 2014 10:02 AM
Points: 1,194, Visits: 789


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?
Post #1020648
« Prev Topic | Next Topic »

Add to briefcase 12345»»»

Permissions Expand / Collapse