Return rows that has 3 or more consecutive values as 1 with Order by on Date & Group By Region
I tried Tabibitosan method but no help. Can you please suggest some other solution for below scenario.
Using Oracle 11g:
Please find the below table format ,
Region Date Value
East 1/1/2018 1
East 1/2/2018 1
East 1/3/2018 0
East 1/4/2018 1
East 1/5/2018 1
East 1/7/2018 1
West 1/9/2018 0
West 1/10/2018 0
West 2/3/2018 1
West 2/4/2018 1
East 2/5/2018 1
West 2/8/2018 0
West 2/9/2018 0
West 2/10/2018 0
From the above table I should return the rows that has value 1 occurred 3 or more times with respect date(order by) and Region.
**My Output:**
Region Date Value
East 1/4/2018 1
East 1/5/2018 1
East 1/7/2018 1
Note : The date in the Date column may not contain all the days. Say, in the above 1/6/2018 is missing which is fine. I need to look for 'Value' column that has 1 consecutive for 3 or more rows when ordered by 'Date'.
sql oracle oracle11g analytics
add a comment |
I tried Tabibitosan method but no help. Can you please suggest some other solution for below scenario.
Using Oracle 11g:
Please find the below table format ,
Region Date Value
East 1/1/2018 1
East 1/2/2018 1
East 1/3/2018 0
East 1/4/2018 1
East 1/5/2018 1
East 1/7/2018 1
West 1/9/2018 0
West 1/10/2018 0
West 2/3/2018 1
West 2/4/2018 1
East 2/5/2018 1
West 2/8/2018 0
West 2/9/2018 0
West 2/10/2018 0
From the above table I should return the rows that has value 1 occurred 3 or more times with respect date(order by) and Region.
**My Output:**
Region Date Value
East 1/4/2018 1
East 1/5/2018 1
East 1/7/2018 1
Note : The date in the Date column may not contain all the days. Say, in the above 1/6/2018 is missing which is fine. I need to look for 'Value' column that has 1 consecutive for 3 or more rows when ordered by 'Date'.
sql oracle oracle11g analytics
add a comment |
I tried Tabibitosan method but no help. Can you please suggest some other solution for below scenario.
Using Oracle 11g:
Please find the below table format ,
Region Date Value
East 1/1/2018 1
East 1/2/2018 1
East 1/3/2018 0
East 1/4/2018 1
East 1/5/2018 1
East 1/7/2018 1
West 1/9/2018 0
West 1/10/2018 0
West 2/3/2018 1
West 2/4/2018 1
East 2/5/2018 1
West 2/8/2018 0
West 2/9/2018 0
West 2/10/2018 0
From the above table I should return the rows that has value 1 occurred 3 or more times with respect date(order by) and Region.
**My Output:**
Region Date Value
East 1/4/2018 1
East 1/5/2018 1
East 1/7/2018 1
Note : The date in the Date column may not contain all the days. Say, in the above 1/6/2018 is missing which is fine. I need to look for 'Value' column that has 1 consecutive for 3 or more rows when ordered by 'Date'.
sql oracle oracle11g analytics
I tried Tabibitosan method but no help. Can you please suggest some other solution for below scenario.
Using Oracle 11g:
Please find the below table format ,
Region Date Value
East 1/1/2018 1
East 1/2/2018 1
East 1/3/2018 0
East 1/4/2018 1
East 1/5/2018 1
East 1/7/2018 1
West 1/9/2018 0
West 1/10/2018 0
West 2/3/2018 1
West 2/4/2018 1
East 2/5/2018 1
West 2/8/2018 0
West 2/9/2018 0
West 2/10/2018 0
From the above table I should return the rows that has value 1 occurred 3 or more times with respect date(order by) and Region.
**My Output:**
Region Date Value
East 1/4/2018 1
East 1/5/2018 1
East 1/7/2018 1
Note : The date in the Date column may not contain all the days. Say, in the above 1/6/2018 is missing which is fine. I need to look for 'Value' column that has 1 consecutive for 3 or more rows when ordered by 'Date'.
sql oracle oracle11g analytics
sql oracle oracle11g analytics
edited Nov 23 '18 at 16:09
ABR
asked Nov 23 '18 at 15:56
ABRABR
274
274
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Try the following if you wish to get all the rows which match the condition.
with data
as (
select x.region,x.date1,x.value,x.pattern_start,x.rnk
from (
select region
,date1
,value
,row_number() over(order by region,date1) as rnk
,case when value=1
and lead(value,1) over(partition by region order by date1) = 1
and lead(value,2) over(partition by region order by date1) = 1
then row_number() over(order by region,date1)
end as pattern_start
,lead(value,2) over(partition by region order by date1) as next_val_2
,lead(value,3) over(partition by region order by date1) as next_val_3
from t)x
)
select *
from data y
where y.rnk in (select pattern_start from data union all
select pattern_start+1 from data union all
select pattern_start+2 from data
)
order by 1,2
Demo Link
https://dbfiddle.uk/?rdbms=oracle_11.2&fiddle=1aa6d5de2b0ec375f659d0243aba350a
add a comment |
Just use lead()
:
select t.*
from (select t.*,
lead(value) over (partition by region order by date) as value_1,
lead(value, 2) over (partition by region order by date) as value_2
from t
) t
where value = 1 and value_1 = 1 and value_2 = 1;
In the event that you have 4 or more in a row and only want the first one, you can add a lag()
:
select t.*
from (select t.*,
lag(value) over (partition by region order by date) as prev_value,
lead(value) over (partition by region order by date) as value_1,
lead(value, 2) over (partition by region order by date) as value_2
from t
) t
where value = 1 and value_1 = 1 and value_2 = 1 and
(prev_value is null or prev_value <> 1);
Thank you so much I'm verifying your query. Will the first query return all the rows when there is consecutive 4 values?
– ABR
Nov 23 '18 at 16:39
@ABR . . . It would return the first two rows, because each begin a sequence of 3.
– Gordon Linoff
Nov 23 '18 at 16:42
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%2f53449692%2freturn-rows-that-has-3-or-more-consecutive-values-as-1-with-order-by-on-date-g%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
Try the following if you wish to get all the rows which match the condition.
with data
as (
select x.region,x.date1,x.value,x.pattern_start,x.rnk
from (
select region
,date1
,value
,row_number() over(order by region,date1) as rnk
,case when value=1
and lead(value,1) over(partition by region order by date1) = 1
and lead(value,2) over(partition by region order by date1) = 1
then row_number() over(order by region,date1)
end as pattern_start
,lead(value,2) over(partition by region order by date1) as next_val_2
,lead(value,3) over(partition by region order by date1) as next_val_3
from t)x
)
select *
from data y
where y.rnk in (select pattern_start from data union all
select pattern_start+1 from data union all
select pattern_start+2 from data
)
order by 1,2
Demo Link
https://dbfiddle.uk/?rdbms=oracle_11.2&fiddle=1aa6d5de2b0ec375f659d0243aba350a
add a comment |
Try the following if you wish to get all the rows which match the condition.
with data
as (
select x.region,x.date1,x.value,x.pattern_start,x.rnk
from (
select region
,date1
,value
,row_number() over(order by region,date1) as rnk
,case when value=1
and lead(value,1) over(partition by region order by date1) = 1
and lead(value,2) over(partition by region order by date1) = 1
then row_number() over(order by region,date1)
end as pattern_start
,lead(value,2) over(partition by region order by date1) as next_val_2
,lead(value,3) over(partition by region order by date1) as next_val_3
from t)x
)
select *
from data y
where y.rnk in (select pattern_start from data union all
select pattern_start+1 from data union all
select pattern_start+2 from data
)
order by 1,2
Demo Link
https://dbfiddle.uk/?rdbms=oracle_11.2&fiddle=1aa6d5de2b0ec375f659d0243aba350a
add a comment |
Try the following if you wish to get all the rows which match the condition.
with data
as (
select x.region,x.date1,x.value,x.pattern_start,x.rnk
from (
select region
,date1
,value
,row_number() over(order by region,date1) as rnk
,case when value=1
and lead(value,1) over(partition by region order by date1) = 1
and lead(value,2) over(partition by region order by date1) = 1
then row_number() over(order by region,date1)
end as pattern_start
,lead(value,2) over(partition by region order by date1) as next_val_2
,lead(value,3) over(partition by region order by date1) as next_val_3
from t)x
)
select *
from data y
where y.rnk in (select pattern_start from data union all
select pattern_start+1 from data union all
select pattern_start+2 from data
)
order by 1,2
Demo Link
https://dbfiddle.uk/?rdbms=oracle_11.2&fiddle=1aa6d5de2b0ec375f659d0243aba350a
Try the following if you wish to get all the rows which match the condition.
with data
as (
select x.region,x.date1,x.value,x.pattern_start,x.rnk
from (
select region
,date1
,value
,row_number() over(order by region,date1) as rnk
,case when value=1
and lead(value,1) over(partition by region order by date1) = 1
and lead(value,2) over(partition by region order by date1) = 1
then row_number() over(order by region,date1)
end as pattern_start
,lead(value,2) over(partition by region order by date1) as next_val_2
,lead(value,3) over(partition by region order by date1) as next_val_3
from t)x
)
select *
from data y
where y.rnk in (select pattern_start from data union all
select pattern_start+1 from data union all
select pattern_start+2 from data
)
order by 1,2
Demo Link
https://dbfiddle.uk/?rdbms=oracle_11.2&fiddle=1aa6d5de2b0ec375f659d0243aba350a
edited Nov 23 '18 at 17:06
answered Nov 23 '18 at 16:46
George JosephGeorge Joseph
1,44249
1,44249
add a comment |
add a comment |
Just use lead()
:
select t.*
from (select t.*,
lead(value) over (partition by region order by date) as value_1,
lead(value, 2) over (partition by region order by date) as value_2
from t
) t
where value = 1 and value_1 = 1 and value_2 = 1;
In the event that you have 4 or more in a row and only want the first one, you can add a lag()
:
select t.*
from (select t.*,
lag(value) over (partition by region order by date) as prev_value,
lead(value) over (partition by region order by date) as value_1,
lead(value, 2) over (partition by region order by date) as value_2
from t
) t
where value = 1 and value_1 = 1 and value_2 = 1 and
(prev_value is null or prev_value <> 1);
Thank you so much I'm verifying your query. Will the first query return all the rows when there is consecutive 4 values?
– ABR
Nov 23 '18 at 16:39
@ABR . . . It would return the first two rows, because each begin a sequence of 3.
– Gordon Linoff
Nov 23 '18 at 16:42
add a comment |
Just use lead()
:
select t.*
from (select t.*,
lead(value) over (partition by region order by date) as value_1,
lead(value, 2) over (partition by region order by date) as value_2
from t
) t
where value = 1 and value_1 = 1 and value_2 = 1;
In the event that you have 4 or more in a row and only want the first one, you can add a lag()
:
select t.*
from (select t.*,
lag(value) over (partition by region order by date) as prev_value,
lead(value) over (partition by region order by date) as value_1,
lead(value, 2) over (partition by region order by date) as value_2
from t
) t
where value = 1 and value_1 = 1 and value_2 = 1 and
(prev_value is null or prev_value <> 1);
Thank you so much I'm verifying your query. Will the first query return all the rows when there is consecutive 4 values?
– ABR
Nov 23 '18 at 16:39
@ABR . . . It would return the first two rows, because each begin a sequence of 3.
– Gordon Linoff
Nov 23 '18 at 16:42
add a comment |
Just use lead()
:
select t.*
from (select t.*,
lead(value) over (partition by region order by date) as value_1,
lead(value, 2) over (partition by region order by date) as value_2
from t
) t
where value = 1 and value_1 = 1 and value_2 = 1;
In the event that you have 4 or more in a row and only want the first one, you can add a lag()
:
select t.*
from (select t.*,
lag(value) over (partition by region order by date) as prev_value,
lead(value) over (partition by region order by date) as value_1,
lead(value, 2) over (partition by region order by date) as value_2
from t
) t
where value = 1 and value_1 = 1 and value_2 = 1 and
(prev_value is null or prev_value <> 1);
Just use lead()
:
select t.*
from (select t.*,
lead(value) over (partition by region order by date) as value_1,
lead(value, 2) over (partition by region order by date) as value_2
from t
) t
where value = 1 and value_1 = 1 and value_2 = 1;
In the event that you have 4 or more in a row and only want the first one, you can add a lag()
:
select t.*
from (select t.*,
lag(value) over (partition by region order by date) as prev_value,
lead(value) over (partition by region order by date) as value_1,
lead(value, 2) over (partition by region order by date) as value_2
from t
) t
where value = 1 and value_1 = 1 and value_2 = 1 and
(prev_value is null or prev_value <> 1);
answered Nov 23 '18 at 16:08
Gordon LinoffGordon Linoff
764k35296400
764k35296400
Thank you so much I'm verifying your query. Will the first query return all the rows when there is consecutive 4 values?
– ABR
Nov 23 '18 at 16:39
@ABR . . . It would return the first two rows, because each begin a sequence of 3.
– Gordon Linoff
Nov 23 '18 at 16:42
add a comment |
Thank you so much I'm verifying your query. Will the first query return all the rows when there is consecutive 4 values?
– ABR
Nov 23 '18 at 16:39
@ABR . . . It would return the first two rows, because each begin a sequence of 3.
– Gordon Linoff
Nov 23 '18 at 16:42
Thank you so much I'm verifying your query. Will the first query return all the rows when there is consecutive 4 values?
– ABR
Nov 23 '18 at 16:39
Thank you so much I'm verifying your query. Will the first query return all the rows when there is consecutive 4 values?
– ABR
Nov 23 '18 at 16:39
@ABR . . . It would return the first two rows, because each begin a sequence of 3.
– Gordon Linoff
Nov 23 '18 at 16:42
@ABR . . . It would return the first two rows, because each begin a sequence of 3.
– Gordon Linoff
Nov 23 '18 at 16:42
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%2f53449692%2freturn-rows-that-has-3-or-more-consecutive-values-as-1-with-order-by-on-date-g%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