Showing posts with label subtract. Show all posts
Showing posts with label subtract. Show all posts

Saturday, February 25, 2012

Dates - Subtracting 3 valid days for each row in FactTable

Dear Friends,

I need your support to do a T-SQL...

For each row date field in a FactTable, I need to subtract 3 days or in some cases 2... the problem is not subtracting 3 or 2 days, but I need to verify in other table Holiday to check if some of the 3 previous dates are holiday or not...

In case of one of the 3 previous dates is holiday, I need to subtract 4 days to the main date in spite of 3

In case of two of the 3 previous dates are holidays, I need to subtract 5 days to the main date in spite of 3.

In case of all of the 3 previous dates are holidays, I need to subtract 6 days to the main date in spite of 3.

And I must garantee that I subtract 3 valid dates (not holidays) to the main date... for example, if one of the 3 previous is a holiday, i need to subtract 4 if the 4 day is not holiday... If it's a holiday I need to find the previous valid date...

How can do it? Could you help me?

here you go...

Code Snippet

/*

Create table Holidays

(

HolidayDate datetime

)

Insert Into Holidays values('12/28/2006')

Insert Into Holidays values('12/29/2006')

Insert Into Holidays values('12/30/2006')

Insert Into Holidays values('12/31/2006')

Insert Into Holidays values('01/01/2007')

Insert Into Holidays values('01/02/2007')

Insert Into Holidays values('01/10/2007')

Insert Into Holidays values('01/11/2007')

Insert Into Holidays values('01/20/2007')

Insert Into Holidays values('01/21/2007')

Insert Into Holidays values('01/22/2007')

*/

Create Function dbo.WorkingDateSubtract(@.NumberOfDays int, @.CurrentDate Datetime)

Returns DateTime

as

Begin

If Exists(Select 1 From Holidays Where HolidayDate = Dateadd(DD,-1,@.CurrentDate))

return dbo.WorkingDateSubtract(@.NumberOfDays, DateAdd(DD,-1,@.CurrentDate))

Else

If @.NumberOfDays = 0

return @.CurrentDate

Else

return dbo.WorkingDateSubtract(@.NumberOfDays-1, DateAdd(DD,-1,@.CurrentDate))

return null

End

Go

Select dbo.WorkingDateSubtract(3, '01/03/2007')

|||

Hi PedroCGD,

What about using the date dimension?. You can have a column in the dimension to know if a date is a holiday one or not, in that case you can use:

select

*

from

dbo.FactTable as f

cross apply

(

select top 3

d.date

from

dbo.dim_date as d

where

d.[date] < f.[date]

and d.IsHoliday = 0

order by

d.[date] DESC

)

go

BTW, which version of SS are you using?

AMB

|||

Hi Hunckback!

You gave me 2 good answers! Thanks...

I will think the better for my case!!

Thanks!!

|||

Customized function that works perfectly for variable holidays!

Code Snippet

ALTERFunction [dbo].[WorkingDateSubtract](@.NumberOfDays int, @.CurrentDate Datetime, @.City char(10))

ReturnsDateTime

as

Begin

IfExists(Select 1 From Holidays Where HolidayDate =Dateadd(DD,-1,@.CurrentDate)AND HV_City=@.City)

return dbo.WorkingDateSubtract(@.NumberOfDays,DateAdd(DD,-1,@.CurrentDate),@.City)

Else

If @.NumberOfDays = 0

return @.CurrentDate

Else

return dbo.WorkingDateSubtract(@.NumberOfDays-1,DateAdd(DD,-1,@.CurrentDate), @.City)

returnnull

End

Now, imagine if I have a table for fixed holidays, and the holidays will be repeated for all the years but in our table we have only

1998-12-25

It means, for each year the month/day 12-25 is a holiday...

how can I do it?

Thanks!

|||

Is there any spl flag then use the following query..

Code Snippet

If Exists(Select 1 From Holidays

Where (

(HolidayDate = Dateadd(DD,-1,@.CurrentDate) And RepeateFlag=0)

or

(Month(HolidayDate)=Month(@.CurrentDate) And Year(HolidayDate) = Year(@.CurrentDate)

And RepeateFlag=1)

) AND HV_City=@.City)

Return dbo.WorkingDateSubtract(@.NumberOfDays, DateAdd(DD,-1,@.CurrentDate),@.City)

|||

Why the use of RepeateFlag?

Thanks!

|||So, you mean to say all the holidays are common across all the year. If not how you will know from your table, this holiday is for all the year, this for only current year?|||

I changed the function to:

Code Snippet

ALTERFunction [dbo].[WorkingDateSubtract](@.NumberOfDays int, @.CurrentDate Datetime, @.City char(10))

ReturnsDateTime

as

Begin

IfExists(Select 1 From dbo.VariableHolidays

Where(

HolidayDate =Dateadd(DD,-1,@.CurrentDate)

OR((Month(HolidayDate)=Month(@.CurrentDate)AndYear(HolidayDate)=Year(@.CurrentDate)))

)

AND Cities_ShortName=@.City)

Return dbo.WorkingDateSubtract(@.NumberOfDays,DateAdd(DD,-1,@.CurrentDate),@.City)

Else

If @.NumberOfDays = 0

return @.CurrentDate

Else

return dbo.WorkingDateSubtract(@.NumberOfDays-1,DateAdd(DD,-1,@.CurrentDate), @.City)

returnnull

End

Does not work... :-(

If I have the date 1998-06-26 as holiday does not recognize date 2007-06-26 as holiday. and if I have the record 2007-06-26 in holiday table, does not recognize to.

But this statment works for all the dates for variableholidays:

Code Snippet

ALTERFunction [dbo].[WorkingDateSubtract](@.NumberOfDays int, @.CurrentDate Datetime, @.City char(10))

ReturnsDateTime

as

Begin

IfExists(Select 1 From dbo.VariableHolidays

Where HolidayDate =Dateadd(DD,-1,@.CurrentDate)

AND Cities_ShortName=@.City

)

return dbo.WorkingDateSubtract(@.NumberOfDays,DateAdd(DD,-1,@.CurrentDate),@.City)

Else

If @.NumberOfDays = 0

return @.CurrentDate

Else

return dbo.WorkingDateSubtract(@.NumberOfDays-1,DateAdd(DD,-1,@.CurrentDate), @.City)

returnnull

End

I was think to create 2 diferent tables, one for fixedHolidays and other for variableHolidays, but if I can do it in only one table, would be better!!

Other funtionality that is missing and I didn't told you before, and is to get only workdays... I tried the statment below but the result still return saturday and sunday... :-(

Code Snippet

ALTERFunction [dbo].[WorkingDateSubtract](@.NumberOfDays int, @.CurrentDate Datetime, @.City char(10))

ReturnsDateTime

as

Begin

IfExists(Select 1 From dbo.VariableHolidays

Where HolidayDate =Dateadd(DD,-1,@.CurrentDate)

AND Cities_ShortName=@.City

AND(DATEPART(DW,HolidayDate)=2

ORDATEPART(DW,HolidayDate)=3

ORDATEPART(DW,HolidayDate)=4

ORDATEPART(DW,HolidayDate)=5

ORDATEPART(DW,HolidayDate)=6

))

return dbo.WorkingDateSubtract(@.NumberOfDays,DateAdd(DD,-1,@.CurrentDate),@.City)

Else

If @.NumberOfDays = 0

return @.CurrentDate

Else

return dbo.WorkingDateSubtract(@.NumberOfDays-1,DateAdd(DD,-1,@.CurrentDate), @.City)

returnnull

End

How can I do it?

Thanks for all!!!

|||

Ok.. Things are getting complicated now rite Smile

Lets finish this now...

Code Snippet

Create Function [dbo].[WorkingDateSubtract]

(

@.NumberOfDays int,

@.CurrentDate Datetime,

@.City char(10)

)

Returns DateTime

as

Begin

If --Varibale Holidays

Exists(Select 1 From dbo.VariableHolidays

Where HolidayDate = @.CurrentDate

AND Cities_ShortName=@.City

)

Or --Fixed Holidays

Exists(Select 1 From dbo.FixedHolidays

Where Month(HolidayDate) = Month(@.CurrentDate)

And Day(HolidayDate) = Day(@.CurrentDate)

AND Cities_ShortName=@.City

)

Or --Saturday & Sundays

DateName(dw,@.CurrentDate) in ('Sunday', 'Saturday')

return dbo.WorkingDateSubtract(@.NumberOfDays, DateAdd(DD,-1,@.CurrentDate),@.City)

Else

If @.NumberOfDays = 0

return @.CurrentDate

Else

return dbo.WorkingDateSubtract(@.NumberOfDays-1, DateAdd(DD,-1,@.CurrentDate), @.City)

return null

End

|||

Dear Friend,

I delete the data in the tables FixedHolidays and VariableHolidays, in order to check if the funtion not return sundays and saturdays...

… 03-07-2007 Tuesday 02-07-2007 Monday 01-07-2007 Sunday 30-06-2007 Saturday 29-06-2007 Friday ok 28-06-2007 Thrusday NOK 27-06-2007 Wendsday ok 26-06-2007 Tuesday ok 25-06-2007 Monday 24-06-2007 Sunday 23-06-2007 Saturday NOK 22-06-2007 Friday ok 21-06-2007 Thrusday 20-06-2007 Tuesday 19-06-2007 Wendsday 18-06-2007 Tuesday 17-06-2007 Monday 16-06-2007 Sunday 15-06-2007 Saturday …

I was looking for each row, and for almost the days is correct, but for the day 28-06-2007 returns the saturday 23-06-2007

The numberofDays is 3 days for all rows in the example...

What you think about it?

Thanks for your important support!

|||Yes You are correct. I edited the code on my previous post. It should work fine now...Smile|||

Dear Manivannan,

The function recognize saturday/sunday, variable days, ans in the fixed holidays works but I edited:

Code Snippet

WhereMonth(HolidayDate)=Month(@.CurrentDate)

AndDay(HolidayDate)=Day(@.CurrentDate)

AND Cities_ShortName=@.City

I changed the YEAR to DAY in the function!

You were fantastic, You have here a friend!!!

If you need something from me, dont hesitate!

Regards and thanks!

|||

Dear Manivannan,

There is a bug... for the date 30-06-2007 that is a saturday... subtracting 3 days, the date returned is 26-06-2007 in spite of 27-06-2007...

The problem seems to be when the current date is a saturday/sunday... could you help me changing the function?

Thanks!!!!

|||

I thought the input always working day.

If you have the above requirement then we can't do it on the recursive method..

Here I edited the code..

Code Snippet

Create Function [dbo].[WorkingDateSubtract]

(

@.NumberOfDays int,

@.CurrentDate Datetime,

@.City char(10)

)

Returns DateTime

as

Begin

Declare @.Scope as Int;

Set @.Scope = 0;

While @.NumberOfDays <> 0

Begin

If --Varibale Holidays

Exists(Select 1 From dbo.VariableHolidays

Where HolidayDate = @.CurrentDate

AND Cities_ShortName=@.City

)

Or --Fixed Holidays

Exists(Select 1 From dbo.FixedHolidays

Where Month(HolidayDate) = Month(@.CurrentDate)

And Day(HolidayDate) = Day(@.CurrentDate)

AND Cities_ShortName=@.City

)

Or --Saturday & Sundays

DateName(dw,@.CurrentDate) in ('Sunday', 'Saturday')

Select @.NumberOfDays = Case When @.Scope=0 Then -1 Else 0 End + @.NumberOfDays,

@.CurrentDate = DateAdd(DD,-1,@.CurrentDate),

@.Scope = @.Scope +1

Else

If @.NumberOfDays = 0

Select @.CurrentDate = @.CurrentDate

Else

Select @.NumberOfDays = @.NumberOfDays-1,

@.CurrentDate = DateAdd(DD,-1,@.CurrentDate),

@.Scope = @.Scope +1

End

return @.CurrentDate

End

Please clarify me, the following all the dates will return '2007-06-27' as output. Is it correct?

Select [dbo].[WorkingDateSubtract](3,'2007-07-02','')

Select [dbo].[WorkingDateSubtract](3,'2007-07-01','')

Select [dbo].[WorkingDateSubtract](3,'2007-06-30','')

Suppose, if your requirement expect output ='2007-06-28' for input = '2007-07-01' then change the following line

(on the first IF condition)

Code Snippet

Select @.NumberOfDays = Case When @.Scope=0 Then -1 Else 0 End + @.NumberOfDays,

@.CurrentDate = DateAdd(DD,-1,@.CurrentDate),

@.Scope = @.Scope + Case When @.Scope= 0 Then 0 Else 1 End

Sunday, February 19, 2012

DateDiff returns odd result

I have a table of addresses for residents in a housing authority, and these
addresses may change so I need to find the first move in date and subtract it
from today's date to get the length of residency. Here is my query:
select distinct a1.HouseholdNbr,
(SELECT MIN(dbo.dateString(MoveInDate)) from tblResidentAddresses a2 where
a1.householdNbr = a2.householdNbr) as MoveInDate,
DateDiff(yy,(SELECT MIN(MoveInDate) from tblResidentAddresses a3 where
a1.householdNbr = a3.householdNbr), GetDAte()) as Residency
from tblResidentAddresses a1
where a1.householdnbr < 99000
order by HouseholdNbr
The length of years residency is correct except for years that are 2004 or
2005. Here is a sample result:
MoveinDate Residency
01/13/20014
08/08/200412
02/21/19978
02/19/198817
07/12/200410
01/17/19978
06/22/199411
06/27/19978
05/24/20046
01/20/20014
07/12/20046
06/01/200424
02/20/200413
01/01/200512
11/18/19978
04/19/20014
08/25/19996
03/22/200429
12/01/199411
Any suggestions why 2004 and 2005 produce wrong results? Thanks
Hi
Posting ddl and example data would help see
http://www.aspfaq.com/etiquette.asp?id=5006. I believe the problem is related
to your function dbo.dateString, try adding it to the datediff MIN()
statement as using datetimes is fine:
CREATE TABLE tblResidentAddresses ( householdnbr int not null
identity(1,1), MoveinDate datetime not null)
INSERT INTO tblResidentAddresses ( MoveinDate )
SELECT '20010113'
UNION ALL SELECT '20040808'
UNION ALL SELECT '19970221'
UNION ALL SELECT '19880219'
UNION ALL SELECT '20040712'
UNION ALL SELECT '19970117'
UNION ALL SELECT '19940622'
UNION ALL SELECT '19970527'
UNION ALL SELECT '20040524'
UNION ALL SELECT '20010120'
UNION ALL SELECT '20040712'
UNION ALL SELECT '20040601'
UNION ALL SELECT '20040220'
UNION ALL SELECT '20050101'
UNION ALL SELECT '19971118'
UNION ALL SELECT '20010419'
UNION ALL SELECT '19990825'
UNION ALL SELECT '20040322'
UNION ALL SELECT '19941201'
SELECT DISTINCT a1.householdnbr,
CONVERT(char(10),(SELECT MIN(MoveInDate) FROM tblResidentAddresses a2 WHERE
a1.householdnbr = a2.householdnbr),110) AS MoveInDate,
DATEDIFF(yy,(SELECT MIN(MoveInDate) FROM tblResidentAddresses a3 WHERE
a1.householdnbr = a3.householdnbr), GETDATE()) AS Residency
FROM tblResidentAddresses a1
WHERE a1.householdnbr < 99000
ORDER BY a1.householdnbr
Seems fine:
householdnbr MoveInDate Residency
-- -- --
1 01-13-2001 4
2 08-08-2004 1
3 02-21-1997 8
4 02-19-1988 17
5 07-12-2004 1
6 01-17-1997 8
7 06-22-1994 11
8 05-27-1997 8
9 05-24-2004 1
10 01-20-2001 4
11 07-12-2004 1
12 06-01-2004 1
13 02-20-2004 1
14 01-01-2005 0
15 11-18-1997 8
16 04-19-2001 4
17 08-25-1999 6
18 03-22-2004 1
19 12-01-1994 11
(19 row(s) affected)
John
"DLS" wrote:

> I have a table of addresses for residents in a housing authority, and these
> addresses may change so I need to find the first move in date and subtract it
> from today's date to get the length of residency. Here is my query:
> select distinct a1.HouseholdNbr,
> (SELECT MIN(dbo.dateString(MoveInDate)) from tblResidentAddresses a2 where
> a1.householdNbr = a2.householdNbr) as MoveInDate,
> DateDiff(yy,(SELECT MIN(MoveInDate) from tblResidentAddresses a3 where
> a1.householdNbr = a3.householdNbr), GetDAte()) as Residency
> from tblResidentAddresses a1
> where a1.householdnbr < 99000
> order by HouseholdNbr
> The length of years residency is correct except for years that are 2004 or
> 2005. Here is a sample result:
> MoveinDate Residency
> 01/13/20014
> 08/08/200412
> 02/21/19978
> 02/19/198817
> 07/12/200410
> 01/17/19978
> 06/22/199411
> 06/27/19978
> 05/24/20046
> 01/20/20014
> 07/12/20046
> 06/01/200424
> 02/20/200413
> 01/01/200512
> 11/18/19978
> 04/19/20014
> 08/25/19996
> 03/22/200429
> 12/01/199411
> Any suggestions why 2004 and 2005 produce wrong results? Thanks

DateDiff returns odd result

I have a table of addresses for residents in a housing authority, and these
addresses may change so I need to find the first move in date and subtract i
t
from today's date to get the length of residency. Here is my query:
select distinct a1.HouseholdNbr,
(SELECT MIN(dbo.dateString(MoveInDate)) from tblResidentAddresses a2 where
a1.householdNbr = a2.householdNbr) as MoveInDate,
DateDiff(yy,(SELECT MIN(MoveInDate) from tblResidentAddresses a3 where
a1.householdNbr = a3.householdNbr), GetDAte()) as Residency
from tblResidentAddresses a1
where a1.householdnbr < 99000
order by HouseholdNbr
The length of years residency is correct except for years that are 2004 or
2005. Here is a sample result:
MoveinDate Residency
01/13/2001 4
08/08/2004 12
02/21/1997 8
02/19/1988 17
07/12/2004 10
01/17/1997 8
06/22/1994 11
06/27/1997 8
05/24/2004 6
01/20/2001 4
07/12/2004 6
06/01/2004 24
02/20/2004 13
01/01/2005 12
11/18/1997 8
04/19/2001 4
08/25/1999 6
03/22/2004 29
12/01/1994 11
Any suggestions why 2004 and 2005 produce wrong results? ThanksHi
Posting ddl and example data would help see
http://www.aspfaq.com/etiquette.asp?id=5006. I believe the problem is relate
d
to your function dbo.dateString, try adding it to the datediff MIN()
statement as using datetimes is fine:
CREATE TABLE tblResidentAddresses ( householdnbr int not null
identity(1,1), MoveinDate datetime not null)
INSERT INTO tblResidentAddresses ( MoveinDate )
SELECT '20010113'
UNION ALL SELECT '20040808'
UNION ALL SELECT '19970221'
UNION ALL SELECT '19880219'
UNION ALL SELECT '20040712'
UNION ALL SELECT '19970117'
UNION ALL SELECT '19940622'
UNION ALL SELECT '19970527'
UNION ALL SELECT '20040524'
UNION ALL SELECT '20010120'
UNION ALL SELECT '20040712'
UNION ALL SELECT '20040601'
UNION ALL SELECT '20040220'
UNION ALL SELECT '20050101'
UNION ALL SELECT '19971118'
UNION ALL SELECT '20010419'
UNION ALL SELECT '19990825'
UNION ALL SELECT '20040322'
UNION ALL SELECT '19941201'
SELECT DISTINCT a1.householdnbr,
CONVERT(char(10),(SELECT MIN(MoveInDate) FROM tblResidentAddresses a2 WHERE
a1.householdnbr = a2.householdnbr),110) AS MoveInDate,
DATEDIFF(yy,(SELECT MIN(MoveInDate) FROM tblResidentAddresses a3 WHERE
a1.householdnbr = a3.householdnbr), GETDATE()) AS Residency
FROM tblResidentAddresses a1
WHERE a1.householdnbr < 99000
ORDER BY a1.householdnbr
Seems fine:
householdnbr MoveInDate Residency
-- -- --
1 01-13-2001 4
2 08-08-2004 1
3 02-21-1997 8
4 02-19-1988 17
5 07-12-2004 1
6 01-17-1997 8
7 06-22-1994 11
8 05-27-1997 8
9 05-24-2004 1
10 01-20-2001 4
11 07-12-2004 1
12 06-01-2004 1
13 02-20-2004 1
14 01-01-2005 0
15 11-18-1997 8
16 04-19-2001 4
17 08-25-1999 6
18 03-22-2004 1
19 12-01-1994 11
(19 row(s) affected)
John
"DLS" wrote:

> I have a table of addresses for residents in a housing authority, and thes
e
> addresses may change so I need to find the first move in date and subtract
it
> from today's date to get the length of residency. Here is my query:
> select distinct a1.HouseholdNbr,
> (SELECT MIN(dbo.dateString(MoveInDate)) from tblResidentAddresses a2 where
> a1.householdNbr = a2.householdNbr) as MoveInDate,
> DateDiff(yy,(SELECT MIN(MoveInDate) from tblResidentAddresses a3 where
> a1.householdNbr = a3.householdNbr), GetDAte()) as Residency
> from tblResidentAddresses a1
> where a1.householdnbr < 99000
> order by HouseholdNbr
> The length of years residency is correct except for years that are 2004 or
> 2005. Here is a sample result:
> MoveinDate Residency
> 01/13/2001 4
> 08/08/2004 12
> 02/21/1997 8
> 02/19/1988 17
> 07/12/2004 10
> 01/17/1997 8
> 06/22/1994 11
> 06/27/1997 8
> 05/24/2004 6
> 01/20/2001 4
> 07/12/2004 6
> 06/01/2004 24
> 02/20/2004 13
> 01/01/2005 12
> 11/18/1997 8
> 04/19/2001 4
> 08/25/1999 6
> 03/22/2004 29
> 12/01/1994 11
> Any suggestions why 2004 and 2005 produce wrong results? Thanks