Validate date range overlapping
Hello all. I am trying to write a code which checks if the date does not overlap. For example, in my form I have id, from_date (mm/dd/yyyy), to_date (mm/dd/yyyy).. what i am trying to do is, the dates must not ovelap for particular id..
Sample:
id | from_date | to_date
1 | 01/01/2012 | 01/31/2012
1 | 04/01/2012 | 04/30/2012 --> this must be allowed
2 | 01/15/2012 | 01/15/2012 --> this must also be allowed as id differs
1 | 01/15/2012 | 01/15/2012 --> this must not be allowed because for id=1, 15th Jan is overalapping with the 1st row's date range (1st Jan to 31st Jan).
PS : I dont want to use DB trigger, but rather write a code in Backing bean
(I will let you know what code i tried so far, if needed)
Edited by: 925715 on Apr 5, 2012 3:36 AM
Assalaam Alaikum Jabr. thankss for the reply. For some reason it doesn't seem to work. I tried to create a validation method, at Entity level and also at Attribute level in Entity class of detail table. it is committing all the dates. Any ways, i will check again, if I am missing something here. And, must the validator be created at entity or attribute level??
Edited by: 925715 on Apr 7, 2012 4:32 PM
Similar Messages
-
Validate Date Range in inputDate component
Hi
I am using JDev 11.1.1.2.0
I have an inputDate component which on click i need to give dates from today to future dates not passed dates.
So i have included 'validate date time range' component in the inputDate component and in the minimum property i have declared 'adf.currentDate' using expression builder and set the maximum as Jan 31,2099.
But when i execute my application i am able to select passed dates using the inputDate component, please help how can i check the date using a validator in inputDate component.
Thanks
Sudeepya i had tried with that property too....
i have declared minValue as adf.currentDate and on that it changes the value of maxValue to current date in Jan 3,2012
i have convertDatetime component in my inputDate to display the selected date in certain format.
It gives error as the format doesnot matches with the convertDatetime and inputDate component.
How can i use the minValue and maxValue of inputDate component?
the component:-
<af:inputDate
label="Planned Termination Date"
id="id2"
value="#{modifyuser.pterminationdate}"
valueChangeListener="#{modifyuserfields.plannedTermDate}"
minValue="adf.currentDate" maxValue="2099-12-31">
<af:convertDateTime pattern="dd-MMM-yyyy"/>
</af:inputDate> -
hi.
i am updating item master.
just i am trying to update to inactive from active
i allready given the date ranges
but above error is comming..
Dim vItem As SAPbobsCOM.Items
Dim RetVal As Long
Dim ErrCode As Long
Dim ErrMsg As String
vItem = ocompany.GetBusinessObject(BoObjectTypes.oItems)
'Retrieve a record by its key from the database
RetVal = vItem.GetByKey(Icode)
'' vItem.UserFields.Fields.Item("U_Status").Value = "Sold"
vItem.Frozen = BoYesNoEnum.tYES
vItem.FrozenFrom = "09/02/2014"
vItem.FrozenTo = "09/02/2014"hi.
Thanks for your reply..
overlapping ..means
'Retrieve a record by its key from the database
RetVal = vItem.GetByKey(Icode)
'' vItem.UserFields.Fields.Item("U_Status").Value = "Sold"
vItem.Frozen = BoYesNoEnum.tYES
vItem.FrozenFrom = "09/02/2014"
vItem.FrozenTo = "09/02/2014"
vItem.FrozenFrom = "09/02/2014"
vItem.FrozenTo = "10/02/2014"
i all ready tried above two ways.. given date same and given two diff dates....
but the same Error is comming..
Any info.plz update me.. -
Validate Date Range - Javascript
Hi Gang,
This is close to a repost... but more of a followup to a
thread that has
already moved down everyones list.
I have a pretty simple form validation routine I need to
implement, but I
don't know the Javascript syntax well enough to work it out
(my background
is VBscript, but this needs to be client side). I received
some good URL's
to check out some free scripts.. but unfortunately, I don't
know javascript
well enough to hack them up correctly.
If someone can help out with a bit of code, it would be a
huge help.
What I need to do is validate a numeric range as well as not
leaving the
field blank.
For example what is entered must be a number between 1900 and
2006.
Here is what I am using for the basic required function:
The name of the form is "ThisForm_Right", and the field name
is "DOB_Year"
(to explain the piece in the code below)
if (ThisForm_Right.DOB_Year.value=="")
alert ("Please enter the YEAR of your DOB.");
ThisForm_Right.DOB_Year.focus();
return false;
Can some kind sole help with a modified version to also check
for an entered
number range?
I suspect some sort of "And" statement, Like
If
ThisForm_Right.DOB_Year.Value==""
AND
Year.Value NOT between (1900 and 2006)
alert ("Please enter the YEAR of your DOB between 1900 and
2006.");
ThisForm_Right.DOB_Year.focus();
return false;
I just don't know the real syntax :>
Thanks
-D>thanks you so much for the code, I'll test it out and
reply with >the
>results.
Shouldn't that be "Thanks so much for the Cod"?
"Dave Bar" <[email protected]> wrote in message
news:e28c7t$hk1$[email protected]..
> ahh yes, you are right..
> I should have said OR instead of AND.
> If Field is blank OR range is not between 1900 &
2006. Thanks for catching
> and pointing that out.
>
> thanks you so much for the code, I'll test it out and
reply with the
> results.
> Thanks
> -Dave
>
>
>
>
> "Lionstone" <[email protected]> wrote
in message
> news:e28b1a$fu5$[email protected]..
>> That's probably because you asked for a fish to help
you out
>>
>>> Can some kind sole help with a modified version
to also check for an
>>> entered
>>> number range?
>>
>> and fish can't type.
>>
>> You're off on the logic though, since something is
not likely to be both
>> blank and outside a certain number range. If it's
blank or outside the
>> range, either condition is sufficient for an error,
right?
>> You also want to make sure that no letters, etc, are
entered, because
>> that will mess up your comparisons to the other
numbers. Keeping in mind
>> that javascript validation can be defeated by
sneezing and you'll need to
>> re-validate on the server, this will be closer to
right (not tested).
>>
>> var ThisYear = ThisForm_Right.DOB_Year.value;
>> ThisYear = ThisYear.replace(/[^/d]/g,"");
>> if(ThisYear.length == 0)
>> {
>> alert("Please enter the year of your DOB.");
>> ThisForm_Right.DOB_Year.focus();
>> return false;
>> }
>> else
>> {
>> ThisYear = parseInt(ThisYear);
>> if((ThisYear < 1900) || (ThisYear > 2006))
>> {
>> alert ("The year of your DOB must be between 1900
and 2006.");
>> ThisForm_Right.DOB_Year.focus();
>> return false;
>> }
>> }
>>
>>
>
> -
Need help! Can't validate date overlaps for a tabular column
Hello all,
The validation I created to validate whether new or updates rows does not overlap with any records in the table isn't working.
The columns are StartDt and EndDt
Validation Type: NOT EXISTS
Validation expresion:
select 1
from sample S
WHERE S.STARTDT BETWEEN :STARTDT AND :ENDDT
OR(S.ENDDT BETWEEN :STARTDT AND :ENDDT)
or (S.STARTDT <= :STARTDT and S.ENDDT >= :ENDDT)
or (:STARTDT <= S.STARTDT and :ENDDT >= S.ENDDT)
Error Message:
Times overlap
When Buttons pressed
-Select Button-
The validation works if a new row or updated row overlaps any existing start and end date records but it doesn't work if I update startdate and end date rows that doesn't overlap existing date times
Example:
TABLE DISPLAYS THE FOLLOWING RECORDS
NAME START DATE END DATE
TEST 1/1/2012 12/31/2012
If I update the rows to the following values, I receive an error message (although those values don't overlap any records in the table)
NAME START DATE END DATE
TEST 6/1/2012 10/30/2012
Any ideas? Do I need to change my code?
Click the link to view the answer to this question. Need help! Can't validate date overlaps for a tabular column
Message was edited by: CharlieMackLogic for date range overlap testing only requires two conditions in the WHERE clause:
ORACLE-BASE - Overlapping Date Ranges
WHERE S.STARTDT <= :ENDDT AND S.ENDDT >= :STARTDT -- TRUE for ranges that overlap
It looks a little odd, but, if you scribble out a 'logic table', it makes sense.
Condition
A.start <= B.end
A.end >= B.start
A__A B--B
true
false
A__B=A--B
true
true
A__B==B__A
true
true
B--A==B__A
true
true
B--B A__A
false
true
MK -
iPad Air iOS 8.1 Stock Photo App. 14000 Photos. Overlapping date ranges in Moments. What is the criteria used for deciding after import where photos are included in Moments. I was expecting each day to have its own exclusive slot for example, not 26 - 27 January 2007 with 8 photos then 27 January 2007 1 photo. Why is there not two seperate slots, one for the 26th and one for 27th?? This makes locating photos by day more arkward than it need be and seems to follow no logic. This occurs over 150 times and is very confusing. Is this an issue or expected behaviour?
iPad Air iOS 8.1 Stock Photo App. 14000 Photos. Overlapping date ranges in Moments. What is the criteria used for deciding after import where photos are included in Moments. I was expecting each day to have its own exclusive slot for example, not 26 - 27 January 2007 with 8 photos then 27 January 2007 1 photo. Why is there not two seperate slots, one for the 26th and one for 27th?? This makes locating photos by day more arkward than it need be and seems to follow no logic. This occurs over 150 times and is very confusing. Is this an issue or expected behaviour?
-
Difficulty working with overlapping date ranges and sums
I have data like the following and need help querying it for a specific result:
startdate enddate place class number
03-30-2006 04-18-2006 A 22 2000
04-01-2006 04-30-2006 A 22 6000
03-30-2006 04-15-2006 B 11 1700
03-15-2006 04-12-2006 B 11 2900
I'd like to return the sum of the averages for any given day, by place, and by class, like this:
day place class average
03-15-2006 B 11 100
03-16-2006 B 11 100
03-30-2006 A 22 100
03-30-2006 B 11 200
03-31-2006 A 22 100
03-31-2006 B 11 200
04-1-2006 A 22 300
04-01-2006 B 11 200
04-30-2006 A 11 200
I'm stuck, any help would be GREATLY appreciated.307b218a-cebe-46f1-87b6-946b4076ce79 wrote:
Here is what I said in my original post:
I'd like to return the sum of the averages for any given day, by place, and by class.....Then gave an example of output.
I think that sentence describes the relations perfectly succinctly.
How can it, when there's no SUM involved
307b218a-cebe-46f1-87b6-946b4076ce79 wrote:
For the date of 3-30-2006, the sum of the averages for region A in class 22 is 100, since from 3-30-2006 to 4-18-2006 is 20 days, and 2000/20 = 100, and there are no other class 22's in place A in that date range. Same goes for 3-31-20006.
However for 4-1-2006 the sum of the averages is 300, since I have one record who's average is 100 (2000/20), and another (6000/30 = 200) and 200 + 100 = 300.
For 4/30, I have one average in that range for place A class 22, and its average for that period is 200.
Does that help?
Much better, thank-you ..
try this:
WITH data AS (
SELECT TO_DATE('03-30-2006','mm-dd-yyyy') startdate, TO_DATE('04-18-2006','mm-dd-yyyy') enddate, 'A' place, 22 class, 2000 cnumber FROM DUAL UNION ALL
SELECT TO_DATE('04-01-2006','mm-dd-yyyy') startdate, TO_DATE('04-30-2006','mm-dd-yyyy') enddate, 'A' place, 22 class, 6000 cnumber FROM DUAL UNION ALL
SELECT TO_DATE('03-30-2006','mm-dd-yyyy') startdate, TO_DATE('04-15-2006','mm-dd-yyyy') enddate, 'B' place, 11 class, 1700 cnumber FROM DUAL UNION ALL
SELECT TO_DATE('03-15-2006','mm-dd-yyyy') startdate, TO_DATE('04-12-2006','mm-dd-yyyy') enddate, 'B' place, 11 class, 2900 cnumber FROM DUAL ),
data2 AS ( SELECT startdate, enddate, enddate-startdate+1 numdays, place, class, cnumber
FROM data d
SELECT startdate+x.lvl day, place, class, cnumber/numdays avg
FROM data2 d,
( SELECT level lvl FROM dual CONNECT BY level <= 10000 ) x
WHERE x.lvl <= numdays
ORDER BY d.startdate, x.lvl
It helps to be specific and detailed -
Continious data range algorithm
I have in my database table 2 important date columns: StartDate (Not null) and EndDate(Allowed Null).
I want to ensure that all records in the table would always create perfect contiues date ranges with no holes inside.
Wor example there may not be records [1-may..1-may, 3-may-...] because there would be a hole [2-may...2-may]. Holes are not allowed.
And overlapping is not allowed, for example [1-may..1-may, 1-may-2may, 3-may-...] is not allowed because overlapping occures on day 1-may. Overlapping and holes are not allowed. But it is allowed that table has no records at all. But all DML manipulations with existing records must ensure that overlapping and holes won't occur.
How to write such check? How to ensure that data ranges would stay continous with no holes and no overlaps?
Oracle 11g.You're setting the wrong value for the start of a group when there is no (null) lagging end date. In my example I set the value to 0 when it was null as I was expecting each group to start at 1. In your case you've set the date to 1/1/1900 which isn't necessarily the day before the first start date of the group. Instead just default it to the start date - 1 to force a match...
SQL> ed
Wrote file afiedt.buf
1 with t as (select 1 as id, to_date('01.04.2013', 'DD.MM.YYYY') as val1, to_date('04.04.2013', 'DD.MM.YYYY') as val2 from dual union all
2 select 1, to_date('05.04.2013', 'DD.MM.YYYY'), to_date('06.04.2013', 'DD.MM.YYYY') from dual union all
3 select 1, to_date('07.04.2013', 'DD.MM.YYYY'), null from dual union all
4 select 2, to_date('01.04.2013', 'DD.MM.YYYY'), to_date('03.04.2013', 'DD.MM.YYYY') from dual union all
5 select 2, to_date('04.04.2013', 'DD.MM.YYYY'), to_date('07.04.2013', 'DD.MM.YYYY') from dual union all
6 select 2, to_date('09.04.2013', 'DD.MM.YYYY'), to_date('12.04.2013', 'DD.MM.YYYY') from dual union all
7 select 2, to_date('13.04.2013', 'DD.MM.YYYY'), null from dual union all
8 select 3, to_date('01.04.2013', 'DD.MM.YYYY'),to_date('03.04.2013', 'DD.MM.YYYY') from dual union all
9 select 3, to_date('04.04.2013', 'DD.MM.YYYY'), null from dual union all
10 select 4, to_date('01.04.2013', 'DD.MM.YYYY'), to_date('01.04.2013', 'DD.MM.YYYY') from dual union all
11 select 4, to_date('01.04.2013', 'DD.MM.YYYY'), null from dual
12 )
13 --
14 select id
15 ,val1 as "start"
16 ,val2 as "end"
17 ,lag(val2) over (partition by id order by val1)
18 ,case when
19 nvl(lag(val2) over (partition by id order by val1),val1-1) != val1-1 then
20 'hole or overlap'
21 else null
22 end as chk
23 from t
24* order by 1, 2
25 /
ID start end LAG(VAL2)OVER(PARTIT CHK
1 01-APR-2013 00:00:00 04-APR-2013 00:00:00
1 05-APR-2013 00:00:00 06-APR-2013 00:00:00 04-APR-2013 00:00:00
1 07-APR-2013 00:00:00 06-APR-2013 00:00:00
2 01-APR-2013 00:00:00 03-APR-2013 00:00:00
2 04-APR-2013 00:00:00 07-APR-2013 00:00:00 03-APR-2013 00:00:00
2 09-APR-2013 00:00:00 12-APR-2013 00:00:00 07-APR-2013 00:00:00 hole or overlap
2 13-APR-2013 00:00:00 12-APR-2013 00:00:00
3 01-APR-2013 00:00:00 03-APR-2013 00:00:00
3 04-APR-2013 00:00:00 03-APR-2013 00:00:00
4 01-APR-2013 00:00:00 01-APR-2013 00:00:00
4 01-APR-2013 00:00:00 01-APR-2013 00:00:00 hole or overlap
11 rows selected. -
Looking for a query to find first/last dates in overlapping dates...
Hi,
I'm looking for a query to find the first dates and last dates in a table conaining overlapping dates.
I have a subscription table which has for each Customer start and end date for different subscriptions.
I want to know the different ranges of date where there is subscriptions active.
so if the table has this:
CustID, Start date, end date
1, 2008-01-01, 2012-06-06
1 ,2009-01-01, 2011-01-01
1, 2011-01-01, 2013-02-02
1, 2013-01-01, 2013-08-08
1, 2014-01-01, 2014-04-04
I want to produce this result:
custid, range start, range end
1, 2008-01-01, 2013-08-08
1, 2014-01-01, 2014-04-04
the first row is the range identified from the 4 rows in my subscription table.
thanks :)I think I found it...
http://stackoverflow.com/questions/5213484/eliminate-and-reduce-overlapping-date-ranges
let me try this method
Hi,
m writing to follow up with you on this post. Thanks for you posting a reply to share your workground. Was the problem resolved after performing the above link? If you are satisfied with the above solution, I’d like to mark this issue as "Answered".
Please also feel free to unmark the issue, with any new findings or concerns you may have.
Thanks,
Sofiya Li
If you have any feedback on our support, please click here.
Sofiya Li
TechNet Community Support -
Date range windowing query...I think
Trying to do something I've not done before perhaps you can help or point me in the right direction.
I've got a simple table with events, something like name, start_date, end_date. These events can be duplicates, they can be distinct, they can have the same start_date and different names, or they can have the same name and start_date but different end_dates - any kind of overlap is allowed. There can also be gaps in the events where there is no event that covers a particular date. Really the only thing I can count on is that none of the values will be null. Basically a mess, as far as determining into which event's start-end date range a particular date falls, when there is overlap. However, that's what I need to do. The other wrinkle is that it's okay for me to ignore the end date of an event, for the purposes of gap-filling. That is to say, if there is no event that covers Jun 1, 2010, then I can 'choose' the event that ends prior to Jun 1, 2010.
I would like to, assuming some analytic function magic that I don't have, take this table and produce another that recomputes the start and end dates so that there is no overlap and the gaps are filled. As an overly simplistic example, given the input:
name start end
foo1 jan 1 jan 31
foo2 jan 15 jan 31
foo3 mar 1 mar 30
it would generate
foo1 jan 1 feb 28
foo3 mar 1 mar 30
or, alternatively
foo1 jan 1 jan 14
foo2 jan 15 feb 28
foo3 mar 1 mar 30
almost any reasonable elimination of overlaps is okay, so long as it makes sense. Are there any of the analytic functions that make this less painful?I'm not sure what the PK of your table is and how "funny" the data can get, but your second result can be got by this:
create table test1 (
name varchar2(10),
startd date,
endd date);
insert into test1 values('foo1', to_date('20100101','yyyymmdd'),to_date('20100131','yyyymmdd'));
insert into test1 values('foo2', to_date('20100115','yyyymmdd'),to_date('20100131','yyyymmdd'));
insert into test1 values('foo3', to_date('20100301','yyyymmdd'),to_date('20100330','yyyymmdd'));
commit;
select name,to_char(startd,'mon dd') startd,
to_char(lead(startd -1,1,endd) over (order by startd),'mon dd')endd
from test1
order by startd;
or, alternatively
foo1 jan 1 jan 14
foo2 jan 15 feb 28
foo3 mar 1 mar 30foo1 jan 01 jan 14
foo2 jan 15 feb 28
foo3 mar 01 mar 30
- andy -
Hi ,
I have a column with date range in the format 07/21/2014-07/21/2014,
07/16/2014-07/16/2014
07/22/2014-07/22/2014
there will be thousands of records like this.
In ssrs I need to pass a pramater with date range =07/16/2014-07/22/2014 then it should display all the records between those dates. I not getting the logic here how to write it. it is pulling all the dates it is not filtering the dates right now.
Can anybody help me on this issue..
BALUSUSRIHARSHAIt will be much easier if you have two columns holding the range and you pass two parameters to find out the overlapping ranges.
select c1, ..., cn
from T
where @sdt <= dt2 and @edt >= dt1; -- considering the range (dt1, dt2)
or just the exact range:
select c1, ..., cn
from T
where dt1 = @sdt and dt2 = @edt; -- considering the range (dt1, dt2)
Since you store the range in one column of character data type then you need to split the value in two, the start and end points of the range, and do the same with the parameter but this will be easier to change the report to send two parameters instead
one.
Search in this forum or Transact-SQL forum for a function to split a string. Here is a good reference:
Arrays and Lists in SQL Server
http://www.sommarskog.se/arrays-in-sql.html
Once you have created the function, it is a matter of using the APPLY operator, pivot the range and use same predicate as in my first query.
Here is an example but I would encorage you to change the model.
SET NOCOUNT ON;
USE tempdb;
GO
--Itzik's VATN
CREATE FUNCTION dbo.GetNums(@n AS BIGINT) RETURNS TABLE
AS
RETURN
WITH
L0 AS(SELECT 1 AS c UNION ALL SELECT 1),
L1 AS(SELECT 1 AS c FROM L0 AS A CROSS JOIN L0 AS B),
L2 AS(SELECT 1 AS c FROM L1 AS A CROSS JOIN L1 AS B),
L3 AS(SELECT 1 AS c FROM L2 AS A CROSS JOIN L2 AS B),
L4 AS(SELECT 1 AS c FROM L3 AS A CROSS JOIN L3 AS B),
L5 AS(SELECT 1 AS c FROM L4 AS A CROSS JOIN L4 AS B),
Nums AS(SELECT ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS n FROM L5)
SELECT TOP (@n) n FROM Nums ORDER BY n;
GO
--Erland's split_me
CREATE FUNCTION dbo.inline_split_me(@param nvarchar(MAX))
RETURNS TABLE AS
RETURN(
SELECT
ROW_NUMBER() OVER(ORDER BY n) AS rn,
LTRIM(rtrim(convert(nvarchar(4000),
substring(@param, n,
charindex(N'-' COLLATE Slovenian_BIN2,
@param + convert(nvarchar(MAX), N'-'),
n) - n)
))) AS Value
FROM dbo.GetNums(LEN(@param))
WHERE substring(convert(nvarchar(MAX), N'-') + @param, n, 1) = N'-' COLLATE Slovenian_BIN2
GO
DECLARE @r varchar(21) = '07/16/2014-07/22/2014';
DECLARE @T TABLE (dt_range varchar(21));
INSERT INTO @T (dt_range)
VALUES
('07/21/2014-07/21/2014'),
('07/16/2014-07/16/2014'),
('07/22/2014-07/22/2014'),
('07/23/2014-07/25/2014');
SELECT
T.dt_range,
R.sdt,
R.edt,
W.p_sdt,
W.p_edt
FROM
@T AS T
CROSS APPLY
SELECT
CAST([1] AS date) AS sdt,
CAST([2] AS date) AS edt
FROM
dbo.inline_split_me(T.dt_range) AS A
PIVOT
MAX(Value)
FOR rn IN ([1], [2])
) AS P
) AS R
CROSS JOIN
SELECT
CAST([1] AS date) AS p_sdt,
CAST([2] AS date) AS p_edt
FROM
dbo.inline_split_me(@r) AS A
PIVOT
MAX(Value)
FOR rn IN ([1], [2])
) AS P
) AS W
WHERE
R.sdt <= W.p_edt
AND R.edt >= W.p_sdt;
GO
DROP FUNCTION dbo.inline_split_me, dbo.GetNums;
GO
Notice that the last row inserted is not part of the result set because it falls outside of the parameter date range.
AMB
Some guidelines for posting questions... -
Hi, I am using Oracle 10g. I want a query which gives me below output.
Data Setup
========
create table t_date_range (
ID number (2),
start_date date,
end_date date);
insert into t_date_range values (1,to_date('20110101', 'YYYYMMDD'),to_date('20110331', 'YYYYMMDD'));
insert into t_date_range values (2,to_date('20110401', 'YYYYMMDD'),to_date('20110531', 'YYYYMMDD'));
insert into t_date_range values (3,to_date('20110701', 'YYYYMMDD'),to_date('20110731', 'YYYYMMDD'));
insert into t_date_range values (4,to_date('20110901', 'YYYYMMDD'),to_date('20111130', 'YYYYMMDD'));
insert into t_date_range values (5,to_date('20111201', 'YYYYMMDD'),to_date('20111231', 'YYYYMMDD'));
commit;
SQL> select ID, to_char(start_date,'DD-MON-YYYY') START_DATE, to_char(end_date,'DD-MON-YYYY') END_DATE from t_date_range;
ID START_DATE END_DATE
1 01-JAN-2011 31-MAR-2011
2 01-APR-2011 31-MAY-2011
3 01-JUL-2011 31-JUL-2011
4 01-SEP-2011 25-OCT-2011
5 26-OCT-2011 30-NOV-2011
6 01-DEC-2011 31-DEC-2011
6 rows selected.
I want result in this form:
START_DATE END_DATE
01-JAN-2011 31-MAY-2011
01-JUL-2011 31-JUL-2011
01-SEP-2011 31-DEC-2011
Means if there is a difference of exact one day between "start_date of 2nd row" and "end_date of first row" then make a single row as shows in above results set.
Thanks!Hi,
Solomon Yakobson wrote:
Keep in mind, Franks's solution assumes date ranges do not overlap. ...That's true. If rows can overlap, then we might need to use two sub-queries, like you did: one to see if a new group starts with this row, and another to count how many new groups have already started.
The solution you posted assumes a relationship between id and dates. If we add a row like this to the sample data:
insert into t_date_range values (6,to_date('20101201', 'YYYYMMDD'),to_date('20121231', 'YYYYMMDD'));that overlaps all the others, then how would that solution work?
LAG won't work, because no matter how we sort the rows, we can't be sure that overlapping rows will be consecutive.
Here's one way to handle overlapping rows:
WITH got_new_grp AS
SELECT id, start_date, end_date
, CASE
WHEN start_date > 1 + MAX (end_date) OVER ( ORDER BY start_date
, id
ROWS BETWEEN UNBOUNDED PRECEDING
AND 1 PRECEDING
THEN 1
END AS new_grp
FROM t_date_range
, got_grp AS
SELECT start_date, end_date
, COUNT (new_grp) OVER ( ORDER BY start_date
, id
) AS grp
FROM got_new_grp
SELECT MIN (start_date) AS start_date
, MAX (end_date) AS end_date
FROM got_grp
GROUP BY grp
ORDER BY start_date
;This solution uses id (assumed to be unique) just to get a consistent ordering, in case 2 (or more) rows have exactly the same start_date. It does not assume any relationship between ids and either start_date or end_date.
I'm also assuming that start_date<=end_date on each row.
Edited by: Frank Kulash on May 11, 2011 12:54 PM
The above refers to the solution you oriognally posted, which was:
with t1 as (
select id,
start_date,
end_date,
case
when lag(end_date) over(order by id) + 1 >= start_date then 0
else 1
end start_of_group
from t_date_range
t2 as (
select id,
start_date,
end_date,
sum(start_of_group) over(order by id) grp
from t1
select min(start_date) start_date,
max(end_date) end_date
from t2
group by grp
order by grp
; -
Hi,
I have an Oracle form which allows users to enter :start_date_active(mandatory) and :end_date_active (can be null).
I need to check in form using SQL if there already exists a record for this date range or if there is any overlapping date ranges for the value entered and disallow user from entering that.
I have my query as follows:
SELECT (1)
FROM <table> xsv
WHERE 1=1
AND ( (:START_DATE_ACTIVE >= xsv.start_date_active) AND
(:START_DATE_ACTIVE <= nvl(xsv.end_date_active,'31-DEC-4712'))
OR
(:END_DATE_ACTIVE >= xsv.start_date_active) AND
(:END_DATE_ACTIVE <= nvl(xsv.end_date_active,'31-DEC-4712'))
But, this incorrectly allows me to enter over lapping date ranges when the :end_date_active is null.
It should work as follows :
eg.
start_date_active end_date_active
01-Apr-2009 allowed
01-Mar-2009 10-Mar-2009 allowed
01-feb-2009 Not allowed, as it overlaps with 1st record
10-feb-2009 05-Mar-2009 Not allowed, as it overlaps with 2nd record
05-Mar-2009 20-mar-2009 Not allowed, as it overlaps with 2nd record
and so on.
How do I modify my query to deal with null end dates ?
Many Thanks
ShankarHi, Shankar,
Whenever you have a question, it helps to post:
(1) The version of Oracle (and any other relevant software) you're using
(2) A little sample data (just enough to show what the problem is) from all the relevant tables
(3) The results you want from that data (You posted this. It would be better if it were formatted.)
(4) Your best attempt so far (formatted) (You posted this, but since it's unformated, it's very hard to read.)
(5) The full error message (if any), including line number
Executable SQL statements (like "CREATE TABLE AS ..." or "INSERT ..." statements) are best for (2).
Formatted tabular output is okay for (3). Type these 6 characters
{code}
(small letters only, inside curly brackets) before and after formatted text, to preserve spacing.
Use different names for the parameters and the columns. For example, if the column is called start_date_active, call the corresponding parameter :p_start_date_active. It will help to keep them straight.
In this statement, if the column end_date_active IS NULL, you're treating it as an impossibly late date:
(:END_DATE_ACTIVE <= nvl(xsv.end_date_active,'31-DEC-4712'))That's good. (You should really use TO_DATE ('31-DEC-4712', 'DD-MON-YYYY'), though.)
If it's possible for the parameter :end_date_active to be NULL, you should use NVL in the same way, to equate it with some appropriate date.
The correct way to see if the range a to b (a <= b) overlaps with x to y (x <= y) is:
WHERE b >= x
AND a <= yIf you want an expression that's TRUE when they do not overlap, then say
WHERE NOT ( b >= x
AND a <= y
)or, equivalently,
WHERE b < x
OR a > y -
Hi!
I have a subscription system, where people can subscribe to a publication during a certain period of time. I would like to know how long a person has subscribed to any publication, so i wonder does anyone know how to perform a "date range union"? By that, i mean that a query on this data:
with test_data as (select 1 id, to_date('01-JAN-2000','DD-MON-YYYY') start_date, to_date('27-JUN-2003','DD-MON-YYYY') stop_date from dual union
select 2, to_date('05-APR-2003','DD-MON-YYYY'), to_date('04-JAN-2004','DD-MON-YYYY') from dual union
select 3, to_date('03-JUL-2002','DD-MON-YYYY'), to_date('02-JUL-2008','DD-MON-YYYY') from dual)
select * from test_data;
should produce
START_DATE STOP_DATE
01-JAN-2000 00:00:00 02-JUL-2008 00:00:00
So there can be any number of periods, and they can overlap entirely as well as "on one end".
I have solved this using PL/SQL, but would like to know if there is a SQL solution. I have also solved this with a fixed number of periods (2,3...) but not with any number.Sorry my fault. Soon after posting I found a case when the result was wrong, so I corrected the query (added default value 1 to lag(gap,1) i.e. lag(gap,1,1)) without leaving a notice. I'm home now - no database access - but I can comment to make it easier for you.
select the_start start_date,stop_date
from (
-- propagating the_start value: we keep data ordered by start date
-- we carry the value of the_start from overlap row to the non-overlap row
select start_date,stop_date,gap,
case when gap = 1 and lag(gap,1) over (order by start_date) = 0
then lag(the_start,1) over (order by start_date)
else start_date
end the_start
from (
-- excluding intermediate overlaps: gap = 0 and the_start is null
select start_date,stop_date,gap,the_start
from (
-- identifying start days: we keep data ordered by start date
-- when we have the first overlap (current gap = 0, but the gap on the previous row <> 0) we record the start_date as the_start
select start_date,stop_date,gap,
case when gap = 0 and lag(gap,1,1) over (order by start_date) = 1
then start_date
end the_start
from (
-- identifying period breaks: we keep data ordered by start date
-- when the start_date on the next row is not greater than the stop_date on the current row we have an overlap
select start_date,stop_date,
case when lead(start_date,1) over (order by start_date) <= stop_date
then 0
else 1
end gap
from test_data
where gap = 1 or the_start is not null
where gap = 1
order by start_dateTo check intermediate results run step by step starting from the inner query (just comment out the outer queries). I'm pretty sure LAG, not LEAD
Regards
Etbin -
How can I set a date range for a Date/Time Field in LiveCycle ES2?
I need to set a specific date range for a form set up in LiveCycle ES2. Cannot see where i allows me to do that without a code entered.
Hi,
You can't set a start and end date for the date/time field, the best you can do is validate the range after the user has selected it.
An alternate is to make your own date / time field, like this sample http://adobelivecycledesignercookbookbybr001.blogspot.com.au/2013/05/an-alternative-date-p icker-for.html
Regards
Bruce
Maybe you are looking for
-
Hi Developers, I have a question about copying of a resultset object into another ResultSet object. To make it much clear of what I am trying to do, I am retreiving a particular row by using absolute(int) method in a given Scrollable ResultSet object
-
Portal Test Connection Failed for JDBC Connector
Hi, We are trying to create a JDBC system in EP to connect SQL Server 2005 database. Our aim is to build a model in VC using data services from SQL Server 2005. We deployed the Driver file sqljdbc.jar and connection was successful when tested by cre
-
How do you change the name of the home icon?
How do I change the title name of the admin icon on the sidebar? The original title is "admin" but it can be changed to an original name. It's the icon to click that opens the folder that contains: Desktop, Documents, Library Downloads, Pictures, Mus
-
Af:query advanced mode.How to hide ???
I am using Jdeveloper 11.1.2.3.0 I have a af:query component. I need to have the advanced mode features like add fields but I want to know is there a way to make the screen have the look and feel of basic mode.(Just to have one search box for a field
-
need of class I need of class aii_map_api.jar. anybody can help me? send me, please. thank you.