Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Data extract on selected dates?

Author  Topic 

Rasta Pickles
Posting Yak Master

174 Posts

Posted - 2011-12-19 : 14:05:50
Hi all,

First post so be gentle

I've inherited a query that uses a

SET @monthenddate = '2011-01-31'

type of entry.

I run the query and it pulls back the data for that month. If I want February's data, I change @monthenddate to '2011-02-28' and so on.

I've been told I may need to extract nine years worth of data and so I'm not overly keen to be running this query 108 times, changing the date each time!

Is there something I can build in to direct the query to extract data based on an array type thing containing all month end dates?

I'm thinking of looping through a "where date in (dates in here)" argument. A WHILE statement perhaps?

Excuse my lack of technical expertise, I'm more used to VBA than SQL!

All replies greatly appreciated and thank you for reading.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-12-19 : 14:07:29
Show us the query.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2011-12-19 : 14:35:24
WHERE [date] BETWEEN @date1 and @date2

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-12-19 : 20:47:53
You can create a calendar table, populate that table with the 108 dates and join with the table. You can also create the table on the fly as shown below and then join to it. If you can post your query, someone on the forum would be able to suggest more specific advice.
DECLARE @startDate DATETIME = '20110115';
DECLARE @months INT = 108;

WITH calendar AS
(
SELECT 1 as N, DATEADD(MONTH, DATEDIFF(MONTH, '18991231',@startDate),'18991231') AS Dt
UNION ALL
SELECT N+1, DATEADD(MONTH,DATEDIFF(MONTH, '18991231',@startDate)+N,'18991231') AS Dt
FROM calendar
WHERE N < @months
)
SELECT DT FROM calendar ORDER BY 1 OPTION (MAXRECURSION 0);
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2011-12-19 : 22:19:36
what the f....really?



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2011-12-20 : 02:40:07
If it is based on a input date, you can simple do

WHERE [date] BETWEEN dateadd(month,-108,@date) and @date

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Rasta Pickles
Posting Yak Master

174 Posts

Posted - 2011-12-20 : 13:27:41
quote:
Originally posted by tkizer

Show us the query.

Tara Kizer



Ok, here you go (bear in mind I inherited this so I don't know whether it's efficient coding or the best way of accomplishing the task):


DECLARE @EndOfDayDate DATE
SET @EndOfDayDate = '2011-02-28'
SET @EndOfDayDate = DATEADD(DAY, 1, @EndOfDayDate)
DECLARE @ActiveSubscriptions AS TABLE(SUBSCRIPTION_ID INT
,FINANCIER_ID INT
,PUBLISHING_COMPANY_ID INT
,OUTSTANDING_SUBSCRIPTION_TOTAL MONEY)
INSERT INTO @ActiveSubscriptions
SELECT AG.SUBSCRIPTION_ID
,AG.FINANCIER_ID
,AG.PUBLISHING_COMPANY_ID
,SUM(CASE WHEN TH.TRANSACTION_TYPE_ID IN (1,2)
THEN ISNULL(TH.PREMIUM_VALUE,0) + ISNULL(TH.INTEREST_VALUE, 0)
ELSE (ISNULL(TH.PREMIUM_VALUE,0) + ISNULL(TH.INTEREST_VALUE,0)) * -1 END)
AS OUTSTANDING_SUBSCRIPTION_TOTAL
FROM SUBSCRIPTIONS AS AG
INNER JOIN TRANSACTION_HEADER AS TH
ON TH.SUBSCRIPTION_ID = AG.SUBSCRIPTION_ID
WHERE (
AG.CANCELLATION_DATE IS NULL
OR AG.CANCELLATION_DATE > @EndOfDayDate
)
AND
(AG.RETIRED_DATE IS NULL
OR AG.RETIRED_DATE > @EndOfDayDate)
AND TH.TRANSACTION_TYPE_ID IN (1, 2, 3, 8, 11, 210, 211, 214)
AND TH.EFFECTIVE_DATE < @EndOfDayDate
GROUP BY AG.SUBSCRIPTION_ID
,AG.INSURER_ID
,AG.PUBLISHING_COMPANY_ID
,AG.FINANCIER_ID
HAVING SUM(CASE WHEN TH.TRANSACTION_TYPE_ID IN (1,2)
THEN TH.PREMIUM_VALUE
ELSE TH.PREMIUM_VALUE * -1
END) <> 0



-- Current Outstanding Balance and counts
SELECT F.LONG_NAME AS FINANCIER_NAME
,COUNT(AA.SUBSCRIPTION_ID) AS ACTIVESUBSCRIPTIONs
FROM @ActiveSubscriptions AS AA
INNER JOIN FINANCIERS AS F
ON F.FINANCIER_ID = AA.FINANCIER_ID
GROUP BY F.LONG_NAME
ORDER BY F.LONG_NAME


SELECT F.LONG_NAME AS FINANCIER_NAME
,sum(AA.OUTSTANDING_SUBSCRIPTION_TOTAL) AS OPENINGSUBSCRIPTIONBALANCE
FROM @ActiveSubscriptions AS AA
INNER JOIN FINANCIERS AS F
ON F.FINANCIER_ID = AA.FINANCIER_ID
GROUP BY F.LONG_NAME
ORDER BY F.LONG_NAME


It's the @EndOfDayDate that I need to change for each end of month balance.....2011-01-31, 2011-02-28 etc etc.

Going back nine years apparently.

Does this help?
Go to Top of Page

Rasta Pickles
Posting Yak Master

174 Posts

Posted - 2011-12-25 : 13:01:29
bump..........anyone?
Go to Top of Page
   

- Advertisement -