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

creating a table to show month and year Expand / Collapse
Author
Message
Posted Monday, December 3, 2012 4:27 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Thursday, February 20, 2014 3:23 AM
Points: 50, Visits: 126
I need to refer to a temp table which has all the months and dynamically shows the years.
for example I need rolling 12 months to show in this table:
November 2012
December 2012
January 2013
February 2013
March 2013
April 2013
May 2013
June 2013
July 2013
August 2013
September 2013
October 2013

And when we are in december I would expect december 2012 to be at the top and an extra line for November 2013. (November 2012 to dissapear).

Is this possible?
Post #1391845
Posted Monday, December 3, 2012 4:38 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 5:38 AM
Points: 2,379, Visits: 7,579
CREATE TABLE #yourTempTable([MonthName] VARCHAR(9), [Year] INT);

WITH CTE(N) AS (SELECT 1 FROM (SELECT 1 UNION ALL SELECT 1)a(N)),
CTE2(N) AS (SELECT 1 FROM CTE x CROSS JOIN CTE y),
CTE3(N) AS (SELECT 1 FROM CTE2 x CROSS JOIN CTE2 y),
TALLY(N) AS (SELECT 0 UNION ALL
SELECT TOP 11 ROW_NUMBER() OVER(ORDER BY (SELECT NULL))
FROM CTE3),
DATETALLY(N) AS (SELECT DATEADD(month, DATEDIFF(month, 0, GETDATE())+N, 0)
FROM TALLY)
INSERT INTO #yourTempTable
SELECT DATENAME(month,N), YEAR(N)
FROM DATETALLY;

SELECT *
FROM #yourTempTable;




Not a DBA, just trying to learn

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better, quicker answers on SQL Server performance related questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/



If you litter your database queries with nolock query hints, are you aware of the side effects?
Try reading a few of these links...

(*) Missing rows with nolock
(*) Allocation order scans with nolock
(*) Consistency issues with nolock
(*) Transient Corruption Errors in SQL Server error log caused by nolock
(*) Dirty reads, read errors, reading rows twice and missing rows with nolock


LinkedIn | Blog coming soon (for sufficiently large values of "soon" )!
Post #1391848
Posted Monday, December 3, 2012 4:43 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Thursday, February 20, 2014 3:23 AM
Points: 50, Visits: 126
Fantastic thank you
Post #1391851
Posted Monday, December 3, 2012 4:48 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 5:38 AM
Points: 2,379, Visits: 7,579
Sachin 80451 (12/3/2012)
Fantastic thank you


No problem. Do you understand how it works?



Not a DBA, just trying to learn

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better, quicker answers on SQL Server performance related questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/



If you litter your database queries with nolock query hints, are you aware of the side effects?
Try reading a few of these links...

(*) Missing rows with nolock
(*) Allocation order scans with nolock
(*) Consistency issues with nolock
(*) Transient Corruption Errors in SQL Server error log caused by nolock
(*) Dirty reads, read errors, reading rows twice and missing rows with nolock


LinkedIn | Blog coming soon (for sufficiently large values of "soon" )!
Post #1391852
Posted Thursday, December 6, 2012 3:33 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Thursday, February 20, 2014 3:23 AM
Points: 50, Visits: 126
Unfortunately i dont have the faintest idea.
Is there any way you can break it down for me?
Post #1393408
Posted Thursday, December 6, 2012 4:02 AM


Say Hey Kid

Say Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey Kid

Group: General Forum Members
Last Login: Monday, October 13, 2014 4:54 AM
Points: 708, Visits: 3,290
Sachin 80451 (12/6/2012)
Unfortunately i dont have the faintest idea.
Is there any way you can break it down for me?


Okay let me thave a go.

Cadavre introduces the concept of a tally table. In his solution he creates the table on the fly which is great but as this numbers table is so useful I highly recommend you create this table permanently in a utility database perhaps?

Here is one way of creating the Tally table:

If exists (select 1 from information_schema.tables where table_name = 'Tally')
drop table dbo.[Tally]
GO

CREATE TABLE dbo.Tally (N INT,CONSTRAINT PK_Tally_N PRIMARY KEY CLUSTERED (N))

DECLARE @Counter INT
SET @Counter = 0

WHILE @Counter <= 10000
BEGIN
INSERT INTO dbo.Tally (N)
VALUES (@Counter)
SET @Counter = @Counter + 1

Now that we have our numbers we can get the first day of each month as follows:

SELECT DATEADD(month, DATEDIFF(month, 0, GETDATE())+N, 0) as N
FROM TALLY

The above bit of code looks complicated but I will try and explain. Our goal is to get one record per month as per yor requirement. To do this we start with:

SELECT DATEDIFF(month, 0, GETDATE())+N
FROM TALLY

This gives us the number of months since 01/01/1900 this is the date you get when you cast the value 0 as a date. So the above query will run somethign like this.

1) Number of months since 01/01/1900 to this day = 1355 + N (tally table starts at 0) so = 1355
2) Number of months since 01/01/1900 to this day = 1355 + N (next value of N is 1 ) so = 1356

and so on.

If we then add the number of months back to 01/01/1900 we will get back 1 record per month


And to get the name we can make the above a derived table or CTE as follows:

select DATENAME(month,N), YEAR(N)
FROM (SELECT DATEADD(month, DATEDIFF(month, 0, GETDATE())+N, 0) as N
FROM TALLY) as a


Hope this helps.


---------------------------------------------------------


It takes a minimal capacity for rational thought to see that the corporate 'free press' is a structurally irrational and biased, and extremely violent, system of elite propaganda.
David Edwards - Media lens

Society has varying and conflicting interests; what is called objectivity is the disguise of one of these interests - that of neutrality. But neutrality is a fiction in an unneutral world. There are victims, there are executioners, and there are bystanders... and the 'objectivity' of the bystander calls for inaction while other heads fall.
Howard Zinn
Post #1393421
Posted Monday, December 24, 2012 7:03 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Tuesday, October 7, 2014 10:53 PM
Points: 3,421, Visits: 5,359
Cadavre (12/3/2012)
CREATE TABLE #yourTempTable([MonthName] VARCHAR(9), [Year] INT);

WITH CTE(N) AS (SELECT 1 FROM (SELECT 1 UNION ALL SELECT 1)a(N)),
CTE2(N) AS (SELECT 1 FROM CTE x CROSS JOIN CTE y),
CTE3(N) AS (SELECT 1 FROM CTE2 x CROSS JOIN CTE2 y),
TALLY(N) AS (SELECT 0 UNION ALL
SELECT TOP 11 ROW_NUMBER() OVER(ORDER BY (SELECT NULL))
FROM CTE3),
DATETALLY(N) AS (SELECT DATEADD(month, DATEDIFF(month, 0, GETDATE())+N, 0)
FROM TALLY)
INSERT INTO #yourTempTable
SELECT DATENAME(month,N), YEAR(N)
FROM DATETALLY;

SELECT *
FROM #yourTempTable;



Jeff Moden would probably be disappointed with me if I didn't post this somewhat terser version:

SELECT Month=DATENAME(month, MyDate), Year=DATEPART(year, MyDate)
FROM [master].dbo.spt_values Tally
CROSS APPLY (
SELECT DATEADD(month, number-1, GETDATE())) a (MyDate)
WHERE [Type] = 'P' AND Number BETWEEN 1 AND 12





My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1400013
Posted Tuesday, December 25, 2012 10:14 PM


SSC-Dedicated

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

Group: General Forum Members
Last Login: Today @ 9:36 PM
Points: 35,347, Visits: 31,885
Sachin 80451 (12/3/2012)
I need to refer to a temp table which has all the months and dynamically shows the years.
for example I need rolling 12 months to show in this table:


Since there are no DATETIME reference columns in that table, I have to ask... WHY? What and how are you going to use this? I'm not trying to be difficult here. I just see a world of hurt coming up and I'm trying to help you avoid it. In order to do that, I need to know what and how you intend to use this table.


--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 #1400142
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse