Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Quarterly Dates..


Quarterly Dates..

Author
Message
Sahasam
Sahasam
SSC-Enthusiastic
SSC-Enthusiastic (101 reputation)SSC-Enthusiastic (101 reputation)SSC-Enthusiastic (101 reputation)SSC-Enthusiastic (101 reputation)SSC-Enthusiastic (101 reputation)SSC-Enthusiastic (101 reputation)SSC-Enthusiastic (101 reputation)SSC-Enthusiastic (101 reputation)

Group: General Forum Members
Points: 101 Visits: 185
Hi All,

I want to calculate Sales for each quarter for the past 10 Years. How can i write a query for this?
Dan.Humphries
Dan.Humphries
SSC Eights!
SSC Eights! (827 reputation)SSC Eights! (827 reputation)SSC Eights! (827 reputation)SSC Eights! (827 reputation)SSC Eights! (827 reputation)SSC Eights! (827 reputation)SSC Eights! (827 reputation)SSC Eights! (827 reputation)

Group: General Forum Members
Points: 827 Visits: 1212
That is an extremely broad question? have you tried anything as of yet? do you have a specific question about a particular element? For example do you know how to do everything but calculate the Qtr number?

Dan

If only I could snap my figures and have all the correct indexes apear and the buffer clean and.... Start day dream here.
Sahasam
Sahasam
SSC-Enthusiastic
SSC-Enthusiastic (101 reputation)SSC-Enthusiastic (101 reputation)SSC-Enthusiastic (101 reputation)SSC-Enthusiastic (101 reputation)SSC-Enthusiastic (101 reputation)SSC-Enthusiastic (101 reputation)SSC-Enthusiastic (101 reputation)SSC-Enthusiastic (101 reputation)

Group: General Forum Members
Points: 101 Visits: 185
I have to insert data into the table at the end of each quarter for the past 10 years.
For now i am able to insert data for the past 10 years.but i need it for quarterly.


-- Set @EOM to end-of-last-quarter date
DECLARE @EOM datetime
SET @EOM = CAST(YEAR(GETDATE()) as varchar(4)) + '-'
+ CAST((MONTH(GETDATE())-1) / 3 * 3 + 1 AS varchar)
+ '-01'
SET @EOM = DATEADD(day,-1,@EOM)
--SET @EOM = '03-31-2010' -- For Testing Purpose
--PRINT @EOM

--Table to Include Last 10 years Worth of Sales Amt data at the End of Each Year
Declare @StartDate datetime
Declare @EndDate datetime

SET @StartDate = CONVERT(DATETIME, CAST(YEAR(@EOM)-10 as varchar(4)) + '-01-01',120)
SET @EndDate = DATEADD(year,1,@StartDate-1)

--PRINT @Startdate
--PRINT @EndDate

--Loop to retrieve past 10 years worth of Data at the end of each year
WHILE @EndDate < @EOM
BEGIN

SET @StartDate = DATEADD(Year,1,@StartDate)
SET @EndDate = DATEADD(Year,1,@EndDate)

IF @EndDate > @EOM
SET @EndDate=@EOM

--Insert into Table ---- @EndDate

-- PRINT CONVERT(varchar(11),@StartDate,113) + ' - ' + CONVERT(varchar(11),@EndDate,113)
End
Dan.Humphries
Dan.Humphries
SSC Eights!
SSC Eights! (827 reputation)SSC Eights! (827 reputation)SSC Eights! (827 reputation)SSC Eights! (827 reputation)SSC Eights! (827 reputation)SSC Eights! (827 reputation)SSC Eights! (827 reputation)SSC Eights! (827 reputation)

Group: General Forum Members
Points: 827 Visits: 1212
I am still not sure where your question is but I am assuming it is in the date portion of your query.

the following will return the quarter date. You can use this concept to pull dates that fall in that quarter.

select datepart(qq,getdate())



I also see you use a loop to pull the sales.
Take a look at this article from Jeff Moden I is a very usual piece of information and describes the same type of this you are talking about.

http://www.sqlservercentral.com/articles/T-SQL/62867/

Dan

If only I could snap my figures and have all the correct indexes apear and the buffer clean and.... Start day dream here.
WayneS
WayneS
SSCertifiable
SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)

Group: General Forum Members
Points: 6261 Visits: 10404
This will show you how to convert a range of dates into year/quarters. I'm also adding in the first and last date/time for each quarter. You should be able to use this to go forth and adapt to your requirements.

declare @StartDate datetime
set @StartDate = DateAdd(month, -1, DateAdd(year, DATEDIFF(year, 0, GetDate()), 0))

-- See Jeff Moden's article
-- The "Numbers" or "Tally" Table: What it is and how it replaces a loop.
-- at http://www.sqlservercentral.com/articles/T-SQL/62867/.
-- NOTE! A permanent tally table will always be MUCH faster
-- than this inline one. See the above article to create your own!
;WITH
Tens (N) AS (SELECT 0 UNION ALL SELECT 0 UNION ALL SELECT 0 UNION ALL
SELECT 0 UNION ALL SELECT 0 UNION ALL SELECT 0 UNION ALL
SELECT 0 UNION ALL SELECT 0 UNION ALL SELECT 0 UNION ALL SELECT 0),
Tally (N) AS (SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 0)) FROM Tens t1 CROSS JOIN Tens t2),
Dates AS (SELECT MyDate = DateAdd(month, N, @StartDate) FROM Tally ),
Quarters AS (SELECT MyDate, MyYear = YEAR(MyDate), Qtr = DatePart(quarter, MyDate)
FROM Dates)
SELECT MyYear, Qtr,
QtrStartDate = MIN(MyDate),
QtrEndDate = MAX(DateAdd(ms, -3, DateAdd(month, 1, MyDate)))
FROM Quarters
GROUP BY MyYear, Qtr
ORDER BY MyYear, Qtr



Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
Links: For better assistance in answering your questions, How to ask a question, Performance Problems, Common date/time routines,
CROSS-TABS and PIVOT tables Part 1 & Part 2, Using APPLY Part 1 & Part 2, Splitting Delimited Strings

Sahasam
Sahasam
SSC-Enthusiastic
SSC-Enthusiastic (101 reputation)SSC-Enthusiastic (101 reputation)SSC-Enthusiastic (101 reputation)SSC-Enthusiastic (101 reputation)SSC-Enthusiastic (101 reputation)SSC-Enthusiastic (101 reputation)SSC-Enthusiastic (101 reputation)SSC-Enthusiastic (101 reputation)

Group: General Forum Members
Points: 101 Visits: 185
Hey Wayne,

I need Quarterly dates for the past 10 years. i tried to modify your code, but i am able to retrieve only past 9 years of data.

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