SQL Reset row_number when column value changed
I've been trying to solve this but failed to do so. I need to get the duration of how long does a server has been unavailable. Here is the following data
Date | Time | Address | Status
11-14 | 6:32 | 1.1.1.1 | Down --- Count Start
11-14 | 6:34 | 1.1.1.1 | Down
11-14 | 6:54 | 1.1.1.1 | UP
11-14 | 7:20 | 1.1.1.1 | Down --- Reset Count to 1
11-14 | 7:25 | 1.1.1.1 | Down
11-14 | 7:30 | 1.1.1.1 | Up
11-14 | 7:40 | 1.1.1.1 | Down --- Reset Count to 1
11-14 | 6:35 | 2.2.2.2 | Down --- Now this is a different counter cause of different IP
I have this query
SELECT [date]
,[time]
,[address]
,[ms]
,[bytes]
,[ttl]
,[Status]
,COALESCE(lag([Status]) over(order by [time]),'--') [Row]
INTO #temp
FROM
(
SELECT [date]
,[time]
,[address]
,[ms]
,[bytes]
,[ttl]
,CASE WHEN [status] = 'Success' THEN 'UP' ELSE 'DOWN' END [Status]
FROM [ESPS].[dbo].[Ping History] p
INNER JOIN [SuperDashboard].[dbo].[IP_Mapping] i ON i.[IP] = p.address
WHERE [Date] = CONVERT(Date,GETDATE())
) a
SELECT [date]
,[time]
,[address]
,[ms]
,[bytes]
,[ttl]
,[Status]
FROM
(
SELECT [date]
,[time]
,[address]
,[ms]
,[bytes]
,[ttl]
,[Status]
,CASE WHEN [Status] != [Row] THEN 1 ELSE 0 END [row]
FROM #temp
) a WHERE [row] = 1
DROP TABLE #temp
but this is only applicable with only one address as things get mixed up when I try to add 2.2.2.2
. The ideal output of this is to get the duration of downtime of a server everytime it encounters a downtime. I hope someone can help me with this or point me to the right direction atleast.
Edit 1: Expected output should be
Date | Start DownTime | End DownTime | Address
11-14 | 6:32 | 6:54 | 1.1.1.1
11-14 | 7:20 | 7:30 | 1.1.1.1
11-14 | 7:40 | | 1.1.1.1
11-14 | 6:35 | | 2.2.2.2
sql sql-server-2017
add a comment |
I've been trying to solve this but failed to do so. I need to get the duration of how long does a server has been unavailable. Here is the following data
Date | Time | Address | Status
11-14 | 6:32 | 1.1.1.1 | Down --- Count Start
11-14 | 6:34 | 1.1.1.1 | Down
11-14 | 6:54 | 1.1.1.1 | UP
11-14 | 7:20 | 1.1.1.1 | Down --- Reset Count to 1
11-14 | 7:25 | 1.1.1.1 | Down
11-14 | 7:30 | 1.1.1.1 | Up
11-14 | 7:40 | 1.1.1.1 | Down --- Reset Count to 1
11-14 | 6:35 | 2.2.2.2 | Down --- Now this is a different counter cause of different IP
I have this query
SELECT [date]
,[time]
,[address]
,[ms]
,[bytes]
,[ttl]
,[Status]
,COALESCE(lag([Status]) over(order by [time]),'--') [Row]
INTO #temp
FROM
(
SELECT [date]
,[time]
,[address]
,[ms]
,[bytes]
,[ttl]
,CASE WHEN [status] = 'Success' THEN 'UP' ELSE 'DOWN' END [Status]
FROM [ESPS].[dbo].[Ping History] p
INNER JOIN [SuperDashboard].[dbo].[IP_Mapping] i ON i.[IP] = p.address
WHERE [Date] = CONVERT(Date,GETDATE())
) a
SELECT [date]
,[time]
,[address]
,[ms]
,[bytes]
,[ttl]
,[Status]
FROM
(
SELECT [date]
,[time]
,[address]
,[ms]
,[bytes]
,[ttl]
,[Status]
,CASE WHEN [Status] != [Row] THEN 1 ELSE 0 END [row]
FROM #temp
) a WHERE [row] = 1
DROP TABLE #temp
but this is only applicable with only one address as things get mixed up when I try to add 2.2.2.2
. The ideal output of this is to get the duration of downtime of a server everytime it encounters a downtime. I hope someone can help me with this or point me to the right direction atleast.
Edit 1: Expected output should be
Date | Start DownTime | End DownTime | Address
11-14 | 6:32 | 6:54 | 1.1.1.1
11-14 | 7:20 | 7:30 | 1.1.1.1
11-14 | 7:40 | | 1.1.1.1
11-14 | 6:35 | | 2.2.2.2
sql sql-server-2017
can you show your expected result based on your above source data?
– jap_jap
Nov 14 '18 at 8:42
@im_one question has been updated sir.
– Ordiz Imbano
Nov 14 '18 at 11:00
add a comment |
I've been trying to solve this but failed to do so. I need to get the duration of how long does a server has been unavailable. Here is the following data
Date | Time | Address | Status
11-14 | 6:32 | 1.1.1.1 | Down --- Count Start
11-14 | 6:34 | 1.1.1.1 | Down
11-14 | 6:54 | 1.1.1.1 | UP
11-14 | 7:20 | 1.1.1.1 | Down --- Reset Count to 1
11-14 | 7:25 | 1.1.1.1 | Down
11-14 | 7:30 | 1.1.1.1 | Up
11-14 | 7:40 | 1.1.1.1 | Down --- Reset Count to 1
11-14 | 6:35 | 2.2.2.2 | Down --- Now this is a different counter cause of different IP
I have this query
SELECT [date]
,[time]
,[address]
,[ms]
,[bytes]
,[ttl]
,[Status]
,COALESCE(lag([Status]) over(order by [time]),'--') [Row]
INTO #temp
FROM
(
SELECT [date]
,[time]
,[address]
,[ms]
,[bytes]
,[ttl]
,CASE WHEN [status] = 'Success' THEN 'UP' ELSE 'DOWN' END [Status]
FROM [ESPS].[dbo].[Ping History] p
INNER JOIN [SuperDashboard].[dbo].[IP_Mapping] i ON i.[IP] = p.address
WHERE [Date] = CONVERT(Date,GETDATE())
) a
SELECT [date]
,[time]
,[address]
,[ms]
,[bytes]
,[ttl]
,[Status]
FROM
(
SELECT [date]
,[time]
,[address]
,[ms]
,[bytes]
,[ttl]
,[Status]
,CASE WHEN [Status] != [Row] THEN 1 ELSE 0 END [row]
FROM #temp
) a WHERE [row] = 1
DROP TABLE #temp
but this is only applicable with only one address as things get mixed up when I try to add 2.2.2.2
. The ideal output of this is to get the duration of downtime of a server everytime it encounters a downtime. I hope someone can help me with this or point me to the right direction atleast.
Edit 1: Expected output should be
Date | Start DownTime | End DownTime | Address
11-14 | 6:32 | 6:54 | 1.1.1.1
11-14 | 7:20 | 7:30 | 1.1.1.1
11-14 | 7:40 | | 1.1.1.1
11-14 | 6:35 | | 2.2.2.2
sql sql-server-2017
I've been trying to solve this but failed to do so. I need to get the duration of how long does a server has been unavailable. Here is the following data
Date | Time | Address | Status
11-14 | 6:32 | 1.1.1.1 | Down --- Count Start
11-14 | 6:34 | 1.1.1.1 | Down
11-14 | 6:54 | 1.1.1.1 | UP
11-14 | 7:20 | 1.1.1.1 | Down --- Reset Count to 1
11-14 | 7:25 | 1.1.1.1 | Down
11-14 | 7:30 | 1.1.1.1 | Up
11-14 | 7:40 | 1.1.1.1 | Down --- Reset Count to 1
11-14 | 6:35 | 2.2.2.2 | Down --- Now this is a different counter cause of different IP
I have this query
SELECT [date]
,[time]
,[address]
,[ms]
,[bytes]
,[ttl]
,[Status]
,COALESCE(lag([Status]) over(order by [time]),'--') [Row]
INTO #temp
FROM
(
SELECT [date]
,[time]
,[address]
,[ms]
,[bytes]
,[ttl]
,CASE WHEN [status] = 'Success' THEN 'UP' ELSE 'DOWN' END [Status]
FROM [ESPS].[dbo].[Ping History] p
INNER JOIN [SuperDashboard].[dbo].[IP_Mapping] i ON i.[IP] = p.address
WHERE [Date] = CONVERT(Date,GETDATE())
) a
SELECT [date]
,[time]
,[address]
,[ms]
,[bytes]
,[ttl]
,[Status]
FROM
(
SELECT [date]
,[time]
,[address]
,[ms]
,[bytes]
,[ttl]
,[Status]
,CASE WHEN [Status] != [Row] THEN 1 ELSE 0 END [row]
FROM #temp
) a WHERE [row] = 1
DROP TABLE #temp
but this is only applicable with only one address as things get mixed up when I try to add 2.2.2.2
. The ideal output of this is to get the duration of downtime of a server everytime it encounters a downtime. I hope someone can help me with this or point me to the right direction atleast.
Edit 1: Expected output should be
Date | Start DownTime | End DownTime | Address
11-14 | 6:32 | 6:54 | 1.1.1.1
11-14 | 7:20 | 7:30 | 1.1.1.1
11-14 | 7:40 | | 1.1.1.1
11-14 | 6:35 | | 2.2.2.2
sql sql-server-2017
sql sql-server-2017
edited Nov 14 '18 at 10:59
Ordiz Imbano
asked Nov 14 '18 at 7:12
Ordiz ImbanoOrdiz Imbano
117
117
can you show your expected result based on your above source data?
– jap_jap
Nov 14 '18 at 8:42
@im_one question has been updated sir.
– Ordiz Imbano
Nov 14 '18 at 11:00
add a comment |
can you show your expected result based on your above source data?
– jap_jap
Nov 14 '18 at 8:42
@im_one question has been updated sir.
– Ordiz Imbano
Nov 14 '18 at 11:00
can you show your expected result based on your above source data?
– jap_jap
Nov 14 '18 at 8:42
can you show your expected result based on your above source data?
– jap_jap
Nov 14 '18 at 8:42
@im_one question has been updated sir.
– Ordiz Imbano
Nov 14 '18 at 11:00
@im_one question has been updated sir.
– Ordiz Imbano
Nov 14 '18 at 11:00
add a comment |
2 Answers
2
active
oldest
votes
This is a type of "group-and-islands" problem. If you can combine all the downs and following up (if any) into a single group, then the rest is just aggregation.
And, you can do this by counting the number of UPs that occur on or after each record. This is a simple cumulative sum, and then the rest is aggregation:
select address, date, grp,
min(case when status = 'DOWN' then time end) as startDown,
max(case when status = 'UP' then time end) as endUp
from (select t.*,
sum(case when status = 'UP' then 1 else 0 end) over (partition by address, date order by time desc) as grp
from t
) t
group by address, date, grp;
add a comment |
Use lag() function
DEMO
select *,case when stat='Down' and (prevval='Up' or prevval is null) then 1 else 0 end as val from
(
select *,lag(stat) over(partition by address order by address) as prevval
from #temp
)A
OUTPUT:
address stat prevval val
1.1.1.1 Down 1
1.1.1.1 Up Down 0
1.1.1.1 Down Up 1
1.1.1.1 Down Down 0
1.1.1.1 Up Down 0
1.1.1.1 Down Up 1
1.1.1.1 Down Down 0
2.2.2.2 Down 1
hi sir I added the final expected output can you check it out?
– Ordiz Imbano
Nov 14 '18 at 11:02
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%2f53294857%2fsql-reset-row-number-when-column-value-changed%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
This is a type of "group-and-islands" problem. If you can combine all the downs and following up (if any) into a single group, then the rest is just aggregation.
And, you can do this by counting the number of UPs that occur on or after each record. This is a simple cumulative sum, and then the rest is aggregation:
select address, date, grp,
min(case when status = 'DOWN' then time end) as startDown,
max(case when status = 'UP' then time end) as endUp
from (select t.*,
sum(case when status = 'UP' then 1 else 0 end) over (partition by address, date order by time desc) as grp
from t
) t
group by address, date, grp;
add a comment |
This is a type of "group-and-islands" problem. If you can combine all the downs and following up (if any) into a single group, then the rest is just aggregation.
And, you can do this by counting the number of UPs that occur on or after each record. This is a simple cumulative sum, and then the rest is aggregation:
select address, date, grp,
min(case when status = 'DOWN' then time end) as startDown,
max(case when status = 'UP' then time end) as endUp
from (select t.*,
sum(case when status = 'UP' then 1 else 0 end) over (partition by address, date order by time desc) as grp
from t
) t
group by address, date, grp;
add a comment |
This is a type of "group-and-islands" problem. If you can combine all the downs and following up (if any) into a single group, then the rest is just aggregation.
And, you can do this by counting the number of UPs that occur on or after each record. This is a simple cumulative sum, and then the rest is aggregation:
select address, date, grp,
min(case when status = 'DOWN' then time end) as startDown,
max(case when status = 'UP' then time end) as endUp
from (select t.*,
sum(case when status = 'UP' then 1 else 0 end) over (partition by address, date order by time desc) as grp
from t
) t
group by address, date, grp;
This is a type of "group-and-islands" problem. If you can combine all the downs and following up (if any) into a single group, then the rest is just aggregation.
And, you can do this by counting the number of UPs that occur on or after each record. This is a simple cumulative sum, and then the rest is aggregation:
select address, date, grp,
min(case when status = 'DOWN' then time end) as startDown,
max(case when status = 'UP' then time end) as endUp
from (select t.*,
sum(case when status = 'UP' then 1 else 0 end) over (partition by address, date order by time desc) as grp
from t
) t
group by address, date, grp;
answered Nov 14 '18 at 12:43
Gordon LinoffGordon Linoff
773k35306408
773k35306408
add a comment |
add a comment |
Use lag() function
DEMO
select *,case when stat='Down' and (prevval='Up' or prevval is null) then 1 else 0 end as val from
(
select *,lag(stat) over(partition by address order by address) as prevval
from #temp
)A
OUTPUT:
address stat prevval val
1.1.1.1 Down 1
1.1.1.1 Up Down 0
1.1.1.1 Down Up 1
1.1.1.1 Down Down 0
1.1.1.1 Up Down 0
1.1.1.1 Down Up 1
1.1.1.1 Down Down 0
2.2.2.2 Down 1
hi sir I added the final expected output can you check it out?
– Ordiz Imbano
Nov 14 '18 at 11:02
add a comment |
Use lag() function
DEMO
select *,case when stat='Down' and (prevval='Up' or prevval is null) then 1 else 0 end as val from
(
select *,lag(stat) over(partition by address order by address) as prevval
from #temp
)A
OUTPUT:
address stat prevval val
1.1.1.1 Down 1
1.1.1.1 Up Down 0
1.1.1.1 Down Up 1
1.1.1.1 Down Down 0
1.1.1.1 Up Down 0
1.1.1.1 Down Up 1
1.1.1.1 Down Down 0
2.2.2.2 Down 1
hi sir I added the final expected output can you check it out?
– Ordiz Imbano
Nov 14 '18 at 11:02
add a comment |
Use lag() function
DEMO
select *,case when stat='Down' and (prevval='Up' or prevval is null) then 1 else 0 end as val from
(
select *,lag(stat) over(partition by address order by address) as prevval
from #temp
)A
OUTPUT:
address stat prevval val
1.1.1.1 Down 1
1.1.1.1 Up Down 0
1.1.1.1 Down Up 1
1.1.1.1 Down Down 0
1.1.1.1 Up Down 0
1.1.1.1 Down Up 1
1.1.1.1 Down Down 0
2.2.2.2 Down 1
Use lag() function
DEMO
select *,case when stat='Down' and (prevval='Up' or prevval is null) then 1 else 0 end as val from
(
select *,lag(stat) over(partition by address order by address) as prevval
from #temp
)A
OUTPUT:
address stat prevval val
1.1.1.1 Down 1
1.1.1.1 Up Down 0
1.1.1.1 Down Up 1
1.1.1.1 Down Down 0
1.1.1.1 Up Down 0
1.1.1.1 Down Up 1
1.1.1.1 Down Down 0
2.2.2.2 Down 1
edited Nov 14 '18 at 7:25
answered Nov 14 '18 at 7:16
fa06fa06
13.2k2917
13.2k2917
hi sir I added the final expected output can you check it out?
– Ordiz Imbano
Nov 14 '18 at 11:02
add a comment |
hi sir I added the final expected output can you check it out?
– Ordiz Imbano
Nov 14 '18 at 11:02
hi sir I added the final expected output can you check it out?
– Ordiz Imbano
Nov 14 '18 at 11:02
hi sir I added the final expected output can you check it out?
– Ordiz Imbano
Nov 14 '18 at 11:02
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%2f53294857%2fsql-reset-row-number-when-column-value-changed%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
can you show your expected result based on your above source data?
– jap_jap
Nov 14 '18 at 8:42
@im_one question has been updated sir.
– Ordiz Imbano
Nov 14 '18 at 11:00