How to add values of weekend and holiday's to the previous working day
I have to add weekend and holiday's value to the previous working day value so that weekend and holiday's should not display in the report but if we don't have previous working day we should simply skip the row as 2018-01-01
skipped in the below output
**DAYS VALUE**
2018-01-01 10 Holiday-1
2018-01-02 20
2018-01-03 30
2018-01-04 40
2018-01-05 50
2018-01-06 60 Saturday
2018-01-07 70 Sunday
2018-01-08 80
2018-01-09 90
2018-01-10 100 Holiday-2
OUTPUT
2018-01-02 20
2018-01-03 30
2018-01-04 40
2018-01-05 180
2018-01-08 80
2018-01-09 190
I am trying with LEAD, LAG, DATEDIFF
and in other ways but not getting any solution so please guys help he with this problem.
sql sql-server-2012
add a comment |
I have to add weekend and holiday's value to the previous working day value so that weekend and holiday's should not display in the report but if we don't have previous working day we should simply skip the row as 2018-01-01
skipped in the below output
**DAYS VALUE**
2018-01-01 10 Holiday-1
2018-01-02 20
2018-01-03 30
2018-01-04 40
2018-01-05 50
2018-01-06 60 Saturday
2018-01-07 70 Sunday
2018-01-08 80
2018-01-09 90
2018-01-10 100 Holiday-2
OUTPUT
2018-01-02 20
2018-01-03 30
2018-01-04 40
2018-01-05 180
2018-01-08 80
2018-01-09 190
I am trying with LEAD, LAG, DATEDIFF
and in other ways but not getting any solution so please guys help he with this problem.
sql sql-server-2012
How do you know which dates are holidays? Is this a third column in the table, where DAYS and VALUE are?
– Andrey Nikolov
Nov 14 '18 at 9:05
Sorry, I forgot to mention that, I have separate holiday table for that.
– Susang
Nov 14 '18 at 9:07
add a comment |
I have to add weekend and holiday's value to the previous working day value so that weekend and holiday's should not display in the report but if we don't have previous working day we should simply skip the row as 2018-01-01
skipped in the below output
**DAYS VALUE**
2018-01-01 10 Holiday-1
2018-01-02 20
2018-01-03 30
2018-01-04 40
2018-01-05 50
2018-01-06 60 Saturday
2018-01-07 70 Sunday
2018-01-08 80
2018-01-09 90
2018-01-10 100 Holiday-2
OUTPUT
2018-01-02 20
2018-01-03 30
2018-01-04 40
2018-01-05 180
2018-01-08 80
2018-01-09 190
I am trying with LEAD, LAG, DATEDIFF
and in other ways but not getting any solution so please guys help he with this problem.
sql sql-server-2012
I have to add weekend and holiday's value to the previous working day value so that weekend and holiday's should not display in the report but if we don't have previous working day we should simply skip the row as 2018-01-01
skipped in the below output
**DAYS VALUE**
2018-01-01 10 Holiday-1
2018-01-02 20
2018-01-03 30
2018-01-04 40
2018-01-05 50
2018-01-06 60 Saturday
2018-01-07 70 Sunday
2018-01-08 80
2018-01-09 90
2018-01-10 100 Holiday-2
OUTPUT
2018-01-02 20
2018-01-03 30
2018-01-04 40
2018-01-05 180
2018-01-08 80
2018-01-09 190
I am trying with LEAD, LAG, DATEDIFF
and in other ways but not getting any solution so please guys help he with this problem.
sql sql-server-2012
sql sql-server-2012
asked Nov 14 '18 at 6:55
SusangSusang
4,6912724
4,6912724
How do you know which dates are holidays? Is this a third column in the table, where DAYS and VALUE are?
– Andrey Nikolov
Nov 14 '18 at 9:05
Sorry, I forgot to mention that, I have separate holiday table for that.
– Susang
Nov 14 '18 at 9:07
add a comment |
How do you know which dates are holidays? Is this a third column in the table, where DAYS and VALUE are?
– Andrey Nikolov
Nov 14 '18 at 9:05
Sorry, I forgot to mention that, I have separate holiday table for that.
– Susang
Nov 14 '18 at 9:07
How do you know which dates are holidays? Is this a third column in the table, where DAYS and VALUE are?
– Andrey Nikolov
Nov 14 '18 at 9:05
How do you know which dates are holidays? Is this a third column in the table, where DAYS and VALUE are?
– Andrey Nikolov
Nov 14 '18 at 9:05
Sorry, I forgot to mention that, I have separate holiday table for that.
– Susang
Nov 14 '18 at 9:07
Sorry, I forgot to mention that, I have separate holiday table for that.
– Susang
Nov 14 '18 at 9:07
add a comment |
2 Answers
2
active
oldest
votes
When there is a row in your Holidays calendar table (I will assume, that weekends are there too), you need to find the max date, prior the current one, for which there is no row in holidays table. Then group by this "real date" and sum the value. Something like this:
declare @t table([DAYS] date, [VALUE] int)
declare @Holidays table([DAYS] date, Note varchar(100))
insert into @t values
('2018-01-01', 10),
('2018-01-02', 20),
('2018-01-03', 30),
('2018-01-04', 40),
('2018-01-05', 50),
('2018-01-06', 60),
('2018-01-07', 70),
('2018-01-08', 80),
('2018-01-09', 90),
('2018-01-10', 100)
insert into @Holidays values
('2018-01-01', 'Holiday-1'),
('2018-01-06', 'Saturday'),
('2018-01-07', 'Sunday'),
('2018-01-10', 'Holiday-2')
;with cte as (
select
IIF(h1.[DAYS] is not null /* i.e. it is a holiday */,
(select max([DAYS])
from @t t2
where t2.[DAYS] < t1.[DAYS] and not exists(select * from @Holidays h2 where h2.[DAYS] = t2.[DAYS])), t1.[DAYS]) as RealDate
, t1.[VALUE]
from @t t1
left join @Holidays h1 on t1.DAYS = h1.[DAYS]
)
select
RealDate
, sum([VALUE]) as RealValue
from cte
where RealDate is not null
group by RealDate
It's working fine(+1) though I am usingTOP 1
approach asCTE
creating problem in my query. I will share my query.
– Susang
Nov 15 '18 at 3:40
add a comment |
You can do this with cumulative sums (to define groups) and aggregation. Define the groups as the number of non-holidays on or before a given day, then aggregate. This is the same value for a non-holiday followed by a holiday.
Then aggregate:
select max(days) as days, sum(value)
from (select t.*,
sum(case when holiday is null then 1 else 0 end) over (order by days asc) as grp
from t
) t
group by grp;
EDIT:
With a separate holidays table, you just need to add the join
:
select max(days) as days, sum(value)
from (select t.*,
sum(case when h.holiday is null then 1 else 0 end) over (order by t.days asc) as grp
from t left join
holidays h
on t.days = h.date
) t
group by grp;
I should go through the second query but it's returning all the sample data without any change.
– Susang
Nov 15 '18 at 3:41
@Susang . . . Arggh!group by grp
, notgroup by days
! Why else calculate it?
– Gordon Linoff
Nov 15 '18 at 13:30
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53294628%2fhow-to-add-values-of-weekend-and-holidays-to-the-previous-working-day%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
When there is a row in your Holidays calendar table (I will assume, that weekends are there too), you need to find the max date, prior the current one, for which there is no row in holidays table. Then group by this "real date" and sum the value. Something like this:
declare @t table([DAYS] date, [VALUE] int)
declare @Holidays table([DAYS] date, Note varchar(100))
insert into @t values
('2018-01-01', 10),
('2018-01-02', 20),
('2018-01-03', 30),
('2018-01-04', 40),
('2018-01-05', 50),
('2018-01-06', 60),
('2018-01-07', 70),
('2018-01-08', 80),
('2018-01-09', 90),
('2018-01-10', 100)
insert into @Holidays values
('2018-01-01', 'Holiday-1'),
('2018-01-06', 'Saturday'),
('2018-01-07', 'Sunday'),
('2018-01-10', 'Holiday-2')
;with cte as (
select
IIF(h1.[DAYS] is not null /* i.e. it is a holiday */,
(select max([DAYS])
from @t t2
where t2.[DAYS] < t1.[DAYS] and not exists(select * from @Holidays h2 where h2.[DAYS] = t2.[DAYS])), t1.[DAYS]) as RealDate
, t1.[VALUE]
from @t t1
left join @Holidays h1 on t1.DAYS = h1.[DAYS]
)
select
RealDate
, sum([VALUE]) as RealValue
from cte
where RealDate is not null
group by RealDate
It's working fine(+1) though I am usingTOP 1
approach asCTE
creating problem in my query. I will share my query.
– Susang
Nov 15 '18 at 3:40
add a comment |
When there is a row in your Holidays calendar table (I will assume, that weekends are there too), you need to find the max date, prior the current one, for which there is no row in holidays table. Then group by this "real date" and sum the value. Something like this:
declare @t table([DAYS] date, [VALUE] int)
declare @Holidays table([DAYS] date, Note varchar(100))
insert into @t values
('2018-01-01', 10),
('2018-01-02', 20),
('2018-01-03', 30),
('2018-01-04', 40),
('2018-01-05', 50),
('2018-01-06', 60),
('2018-01-07', 70),
('2018-01-08', 80),
('2018-01-09', 90),
('2018-01-10', 100)
insert into @Holidays values
('2018-01-01', 'Holiday-1'),
('2018-01-06', 'Saturday'),
('2018-01-07', 'Sunday'),
('2018-01-10', 'Holiday-2')
;with cte as (
select
IIF(h1.[DAYS] is not null /* i.e. it is a holiday */,
(select max([DAYS])
from @t t2
where t2.[DAYS] < t1.[DAYS] and not exists(select * from @Holidays h2 where h2.[DAYS] = t2.[DAYS])), t1.[DAYS]) as RealDate
, t1.[VALUE]
from @t t1
left join @Holidays h1 on t1.DAYS = h1.[DAYS]
)
select
RealDate
, sum([VALUE]) as RealValue
from cte
where RealDate is not null
group by RealDate
It's working fine(+1) though I am usingTOP 1
approach asCTE
creating problem in my query. I will share my query.
– Susang
Nov 15 '18 at 3:40
add a comment |
When there is a row in your Holidays calendar table (I will assume, that weekends are there too), you need to find the max date, prior the current one, for which there is no row in holidays table. Then group by this "real date" and sum the value. Something like this:
declare @t table([DAYS] date, [VALUE] int)
declare @Holidays table([DAYS] date, Note varchar(100))
insert into @t values
('2018-01-01', 10),
('2018-01-02', 20),
('2018-01-03', 30),
('2018-01-04', 40),
('2018-01-05', 50),
('2018-01-06', 60),
('2018-01-07', 70),
('2018-01-08', 80),
('2018-01-09', 90),
('2018-01-10', 100)
insert into @Holidays values
('2018-01-01', 'Holiday-1'),
('2018-01-06', 'Saturday'),
('2018-01-07', 'Sunday'),
('2018-01-10', 'Holiday-2')
;with cte as (
select
IIF(h1.[DAYS] is not null /* i.e. it is a holiday */,
(select max([DAYS])
from @t t2
where t2.[DAYS] < t1.[DAYS] and not exists(select * from @Holidays h2 where h2.[DAYS] = t2.[DAYS])), t1.[DAYS]) as RealDate
, t1.[VALUE]
from @t t1
left join @Holidays h1 on t1.DAYS = h1.[DAYS]
)
select
RealDate
, sum([VALUE]) as RealValue
from cte
where RealDate is not null
group by RealDate
When there is a row in your Holidays calendar table (I will assume, that weekends are there too), you need to find the max date, prior the current one, for which there is no row in holidays table. Then group by this "real date" and sum the value. Something like this:
declare @t table([DAYS] date, [VALUE] int)
declare @Holidays table([DAYS] date, Note varchar(100))
insert into @t values
('2018-01-01', 10),
('2018-01-02', 20),
('2018-01-03', 30),
('2018-01-04', 40),
('2018-01-05', 50),
('2018-01-06', 60),
('2018-01-07', 70),
('2018-01-08', 80),
('2018-01-09', 90),
('2018-01-10', 100)
insert into @Holidays values
('2018-01-01', 'Holiday-1'),
('2018-01-06', 'Saturday'),
('2018-01-07', 'Sunday'),
('2018-01-10', 'Holiday-2')
;with cte as (
select
IIF(h1.[DAYS] is not null /* i.e. it is a holiday */,
(select max([DAYS])
from @t t2
where t2.[DAYS] < t1.[DAYS] and not exists(select * from @Holidays h2 where h2.[DAYS] = t2.[DAYS])), t1.[DAYS]) as RealDate
, t1.[VALUE]
from @t t1
left join @Holidays h1 on t1.DAYS = h1.[DAYS]
)
select
RealDate
, sum([VALUE]) as RealValue
from cte
where RealDate is not null
group by RealDate
answered Nov 14 '18 at 9:18
Andrey NikolovAndrey Nikolov
4,2633821
4,2633821
It's working fine(+1) though I am usingTOP 1
approach asCTE
creating problem in my query. I will share my query.
– Susang
Nov 15 '18 at 3:40
add a comment |
It's working fine(+1) though I am usingTOP 1
approach asCTE
creating problem in my query. I will share my query.
– Susang
Nov 15 '18 at 3:40
It's working fine(+1) though I am using
TOP 1
approach as CTE
creating problem in my query. I will share my query.– Susang
Nov 15 '18 at 3:40
It's working fine(+1) though I am using
TOP 1
approach as CTE
creating problem in my query. I will share my query.– Susang
Nov 15 '18 at 3:40
add a comment |
You can do this with cumulative sums (to define groups) and aggregation. Define the groups as the number of non-holidays on or before a given day, then aggregate. This is the same value for a non-holiday followed by a holiday.
Then aggregate:
select max(days) as days, sum(value)
from (select t.*,
sum(case when holiday is null then 1 else 0 end) over (order by days asc) as grp
from t
) t
group by grp;
EDIT:
With a separate holidays table, you just need to add the join
:
select max(days) as days, sum(value)
from (select t.*,
sum(case when h.holiday is null then 1 else 0 end) over (order by t.days asc) as grp
from t left join
holidays h
on t.days = h.date
) t
group by grp;
I should go through the second query but it's returning all the sample data without any change.
– Susang
Nov 15 '18 at 3:41
@Susang . . . Arggh!group by grp
, notgroup by days
! Why else calculate it?
– Gordon Linoff
Nov 15 '18 at 13:30
add a comment |
You can do this with cumulative sums (to define groups) and aggregation. Define the groups as the number of non-holidays on or before a given day, then aggregate. This is the same value for a non-holiday followed by a holiday.
Then aggregate:
select max(days) as days, sum(value)
from (select t.*,
sum(case when holiday is null then 1 else 0 end) over (order by days asc) as grp
from t
) t
group by grp;
EDIT:
With a separate holidays table, you just need to add the join
:
select max(days) as days, sum(value)
from (select t.*,
sum(case when h.holiday is null then 1 else 0 end) over (order by t.days asc) as grp
from t left join
holidays h
on t.days = h.date
) t
group by grp;
I should go through the second query but it's returning all the sample data without any change.
– Susang
Nov 15 '18 at 3:41
@Susang . . . Arggh!group by grp
, notgroup by days
! Why else calculate it?
– Gordon Linoff
Nov 15 '18 at 13:30
add a comment |
You can do this with cumulative sums (to define groups) and aggregation. Define the groups as the number of non-holidays on or before a given day, then aggregate. This is the same value for a non-holiday followed by a holiday.
Then aggregate:
select max(days) as days, sum(value)
from (select t.*,
sum(case when holiday is null then 1 else 0 end) over (order by days asc) as grp
from t
) t
group by grp;
EDIT:
With a separate holidays table, you just need to add the join
:
select max(days) as days, sum(value)
from (select t.*,
sum(case when h.holiday is null then 1 else 0 end) over (order by t.days asc) as grp
from t left join
holidays h
on t.days = h.date
) t
group by grp;
You can do this with cumulative sums (to define groups) and aggregation. Define the groups as the number of non-holidays on or before a given day, then aggregate. This is the same value for a non-holiday followed by a holiday.
Then aggregate:
select max(days) as days, sum(value)
from (select t.*,
sum(case when holiday is null then 1 else 0 end) over (order by days asc) as grp
from t
) t
group by grp;
EDIT:
With a separate holidays table, you just need to add the join
:
select max(days) as days, sum(value)
from (select t.*,
sum(case when h.holiday is null then 1 else 0 end) over (order by t.days asc) as grp
from t left join
holidays h
on t.days = h.date
) t
group by grp;
edited Nov 15 '18 at 13:30
answered Nov 14 '18 at 12:47
Gordon LinoffGordon Linoff
782k35310414
782k35310414
I should go through the second query but it's returning all the sample data without any change.
– Susang
Nov 15 '18 at 3:41
@Susang . . . Arggh!group by grp
, notgroup by days
! Why else calculate it?
– Gordon Linoff
Nov 15 '18 at 13:30
add a comment |
I should go through the second query but it's returning all the sample data without any change.
– Susang
Nov 15 '18 at 3:41
@Susang . . . Arggh!group by grp
, notgroup by days
! Why else calculate it?
– Gordon Linoff
Nov 15 '18 at 13:30
I should go through the second query but it's returning all the sample data without any change.
– Susang
Nov 15 '18 at 3:41
I should go through the second query but it's returning all the sample data without any change.
– Susang
Nov 15 '18 at 3:41
@Susang . . . Arggh!
group by grp
, not group by days
! Why else calculate it?– Gordon Linoff
Nov 15 '18 at 13:30
@Susang . . . Arggh!
group by grp
, not group by days
! Why else calculate it?– Gordon Linoff
Nov 15 '18 at 13:30
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53294628%2fhow-to-add-values-of-weekend-and-holidays-to-the-previous-working-day%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
How do you know which dates are holidays? Is this a third column in the table, where DAYS and VALUE are?
– Andrey Nikolov
Nov 14 '18 at 9:05
Sorry, I forgot to mention that, I have separate holiday table for that.
– Susang
Nov 14 '18 at 9:07