SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How To Get a row(date) for each month


How To Get a row(date) for each month

Author
Message
Teee-SQL
Teee-SQL
SSChasing Mays
SSChasing Mays (622 reputation)SSChasing Mays (622 reputation)SSChasing Mays (622 reputation)SSChasing Mays (622 reputation)SSChasing Mays (622 reputation)SSChasing Mays (622 reputation)SSChasing Mays (622 reputation)SSChasing Mays (622 reputation)

Group: General Forum Members
Points: 622 Visits: 229
Hi All,

I need to get a minimum month and year from a table and then a row for every month/year up to today.
How to I achieve that, here's the query I'm using below:

Select
MIN(DATENAME(MONTH, dtePostedToWebsiteDate)) as MinMonth
,MIN(Year(dtePostedToWebsiteDate)) as MinYear
,'' as [RowForEachMonth] --??
From dtlVacancyPostAudit
Where bitPostToWebSite = 1

Please help.

Thanks
Teee
HanShi
HanShi
SSCrazy Eights
SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)

Group: General Forum Members
Points: 8780 Visits: 3718
I'm not sure it this is what you want.

Select DISTINCT
DATENAME(MONTH, dtePostedToWebsiteDate) as Month
,Year(dtePostedToWebsiteDate) as Year
From dtlVacancyPostAudit
Where bitPostToWebSite = 1
GROUP BY dtePostedToWebsiteDate
ORDER BY 2, 1

** Don't mistake the ‘stupidity of the crowd’ for the ‘wisdom of the group’! **
Kingston Dhasian
Kingston Dhasian
SSCertifiable
SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)

Group: General Forum Members
Points: 6106 Visits: 5285
Something like this?

DECLARE   @min DATETIME
DECLARE @max DATETIME

SELECT @min = MIN(dtePostedToWebsiteDate)
FROM dtlVacancyPostAudit
WHERE bitPostToWebSite = 1

SET @max = DATEADD(MONTH,DATEDIFF(MONTH,0,CURRENT_TIMESTAMP)+1,0)

SELECT DATENAME(MONTH,@min) AS MinMonth,
YEAR(@min) AS MinYear,
DATEADD(MONTH,sv.number,@min)
FROM master.dbo.spt_values AS sv -- You can use a Tally table instead as well
WHERE sv.type = 'P'
AND DATEADD(MONTH,sv.number,@min) < @max



You can check the below link for a Tally table creation script
http://www.sqlservercentral.com/scripts/Advanced+SQL/62486/


Kingston Dhasian

How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Teee-SQL
Teee-SQL
SSChasing Mays
SSChasing Mays (622 reputation)SSChasing Mays (622 reputation)SSChasing Mays (622 reputation)SSChasing Mays (622 reputation)SSChasing Mays (622 reputation)SSChasing Mays (622 reputation)SSChasing Mays (622 reputation)SSChasing Mays (622 reputation)

Group: General Forum Members
Points: 622 Visits: 229
I need something like this but now its not grouping the months properly:

Select
MIN(DATENAME(MONTH, dtePostedToWebsiteDate)) as MinMonth
,MIN(Year(dtePostedToWebsiteDate)) as MinYear
From dtlVacancyPostAudit
Where bitPostToWebSite = 1
Group By dtePostedToWebsiteDate
Having dtePostedToWebsiteDate between MIN(dtePostedToWebsiteDate) And GETDATE()

This is the output I'm getting:
MinMonth MinYear
June 2012
July 2012
July 2012
July 2012
July 2012
July 2012
July 2012
September 2012
September 2012
September 2012
September 2012
September 2012
September 2012
September 2012
November 2012
November 2012
November 2012
November 2012
November 2012
January 2013
February 2013
February 2013
February 2013
March 2013
March 2013
March 2013
March 2013
March 2013
April 2013
April 2013
May 2013
May 2013
May 2013
June 2013
Teee-SQL
Teee-SQL
SSChasing Mays
SSChasing Mays (622 reputation)SSChasing Mays (622 reputation)SSChasing Mays (622 reputation)SSChasing Mays (622 reputation)SSChasing Mays (622 reputation)SSChasing Mays (622 reputation)SSChasing Mays (622 reputation)SSChasing Mays (622 reputation)

Group: General Forum Members
Points: 622 Visits: 229
@Kingston Dhasian, Thank you so much for the script that's returning the dates it works perfectly.
Kingston Dhasian
Kingston Dhasian
SSCertifiable
SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)

Group: General Forum Members
Points: 6106 Visits: 5285
Teee (6/13/2013)
@Kingston Dhasian, Thank you so much for the script that's returning the dates it works perfectly.


I am glad it worked for you and I hope you understand what the code does and how
I would also advise you to go through the articles on Tally articles on this site which help you solve a lot of similar problems
You can start with the link given below
http://www.sqlservercentral.com/articles/T-SQL/62867/


Kingston Dhasian

How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search