# Select records based on the closest given time

Dear SQL gurus,
I have a table T1:
Name Null? Type
ID NOT NULL NUMBER(5)
MOMENT NOT NULL DATE [DD.MM.YYYY HH24:MI]
MEASUREMENT NOT NULL NUMBER(8,3)
Example (ID, MOMENT, MEASUREMENT)
-- START OF EXAMPLE --
9380 18.11.2000 03:45 17.6
9380 18.11.2000 04:30 17.3
9380 18.11.2000 05:45 16.8
9380 18.11.2000 06:15 16.8
9380 18.11.2000 07:00 16.2
9380 18.11.2000 07:30 16.2
9380 18.11.2000 08:15 16
9380 18.11.2000 08:45 15.7
9380 18.11.2000 09:30 15.4
9380 18.11.2000 10:00 15.4
9380 18.11.2000 11:15 15.4
9380 18.11.2000 11:45 15.4
9380 18.11.2000 12:30 15.4
9380 18.11.2000 13:00 15.4
9380 18.11.2000 13:45 15.4
--- END OF EXAMPLE --
How to select records based on the:
- time period specified by the day only [DD.MM.YYYY] - CONDITION 1
- with values for 6AM only, and if not available, with values closest to 6AM - CONDITION 2
(if the time gap in MOMENT field is too big, lets say > 5h then choose the average between the value before 6AM (ex. 4:15AM) and the value after the 6AM (ex. 9:45AM))
CONDITION 1 (something like): moment between '01.01.2005' and '31.12.2004' - this is OK
CONDITION 2: I do not know how to formulate, especially if 6AM value is not availabe, and I have to find the closest available value, or get the avergae from the two adjacent values.
Maybe cursor magic??? Thanks a lot for your help.

About condition two, would the following select be of use to you? Picking the first record could be achived by rownum, analytic function, etc.
WITH t1 AS (SELECT 9380 id, TO_DATE('18.11.2000 03:45', 'dd.mm.yyyy hh24:mi') moment,  17.6 measurement
FROM dual
UNION
SELECT 9380 id, TO_DATE('18.11.2000 04:30', 'dd.mm.yyyy hh24:mi') moment,  17.3 measurement
FROM dual
UNION
SELECT 9380 id, TO_DATE('18.11.2000 05:45', 'dd.mm.yyyy hh24:mi') moment,  16.8 measurement
FROM dual
UNION
SELECT 9380 id, TO_DATE('18.11.2000 06:15', 'dd.mm.yyyy hh24:mi') moment,  16.8 measurement
FROM dual
SELECT id, moment, measurement, diff
FROM (SELECT id, moment, measurement,
moment - TO_DATE(TO_CHAR(moment, 'dd.mm.yyyy ') || '06:00', 'dd.mm.yyyy hh24:mi') diff
FROM t1
ORDER BY abs(diff) asc, SIGN(diff) desc;
C.

### Similar Messages

• Selecting records based on the flag

Hi All,
I have records like the following
Program_Name Effective_Date Valid_Flag
ABCD 2/10/2012 N
ABCD 2/14/2012 N
ABCD 2/20/2012 Y
ABCD 3/01/2012 N
ABCD 3/10/2012 N
ABCD 3/14/2012 Y
ABCD 3/25/2012 N
ABCD 3/26/2012 N
ABCD 3/27/2012 N
ABCD 3/28/2012 N
ABCD 3/29/2012 N
ABCD 4/25/2012 Y
I have to write a select statement to to keep the first record and then pull only the records when the Valid_Flag changed. The result set should be like below.
Program_Name Effective_Date Valid_Flag
ABCD 2/10/2012 N -- I have preserved the first record
ABCD 2/20/2012 Y -- Valid_Flag chages to a Y for teh first time and so on.
ABCD 3/01/2012 N
ABCD 3/14/2012 Y
ABCD 3/25/2012 N
ABCD 4/25/2012 Y
If there is no change in the flag, I do not have to pull that record. Please help with SQL suggestions. Thanks for your time and help.

ssk1974 wrote:
Hi All,
I have records like the following
Program_Name Effective_Date Valid_Flag
ABCD 2/10/2012 N
ABCD 2/14/2012 N
ABCD 2/20/2012 Y
ABCD 3/01/2012 N
ABCD 3/10/2012 N
ABCD 3/14/2012 Y
ABCD 3/25/2012 N
ABCD 3/26/2012 N
ABCD 3/27/2012 N
ABCD 3/28/2012 N
ABCD 3/29/2012 N
ABCD 4/25/2012 Y
I have to write a select statement to to keep the first record and then pull only the records when the Valid_Flag changed. The result set should be like below.
Program_Name Effective_Date Valid_Flag
ABCD 2/10/2012 N -- I have preserved the first record
ABCD 2/20/2012 Y -- Valid_Flag chages to a Y for teh first time and so on.
ABCD 3/01/2012 N
ABCD 3/14/2012 Y
ABCD 3/25/2012 N
ABCD 4/25/2012 Y
If there is no change in the flag, I do not have to pull that record. Please help with SQL suggestions. Thanks for your time and help.In the future, it would be nice if you could provide the sample data like i created below.
ME_XE?with data as
2  (
3     select 'ABCD' as col1, to_date('2/10/2012', 'mm/dd/yyyy')       as col2, 'N' as col3    from dual union all
4     select 'ABCD' as col1, to_date('2/14/2012', 'mm/dd/yyyy')       as col2, 'N' as col3    from dual union all
5     select 'ABCD' as col1, to_date('2/20/2012', 'mm/dd/yyyy')       as col2, 'Y' as col3    from dual union all
6     select 'ABCD' as col1, to_date('3/01/2012', 'mm/dd/yyyy')       as col2, 'N' as col3    from dual union all
7     select 'ABCD' as col1, to_date('3/10/2012', 'mm/dd/yyyy')       as col2, 'N' as col3    from dual union all
8     select 'ABCD' as col1, to_date('3/14/2012', 'mm/dd/yyyy')       as col2, 'Y' as col3    from dual union all
9     select 'ABCD' as col1, to_date('3/25/2012', 'mm/dd/yyyy')       as col2, 'N' as col3    from dual union all
10     select 'ABCD' as col1, to_date('3/26/2012', 'mm/dd/yyyy')       as col2, 'N' as col3    from dual union all
11     select 'ABCD' as col1, to_date('3/27/2012', 'mm/dd/yyyy')       as col2, 'N' as col3    from dual union all
12     select 'ABCD' as col1, to_date('3/28/2012', 'mm/dd/yyyy')       as col2, 'N' as col3    from dual union all
13     select 'ABCD' as col1, to_date('3/29/2012', 'mm/dd/yyyy')       as col2, 'N' as col3    from dual union all
14     select 'ABCD' as col1, to_date('4/25/2012', 'mm/dd/yyyy')       as col2, 'Y' as col3    from dual
15  )
16  select *
17  from
18  (
19     select
20             col1, col2, col3,
21             lag(col3) over (partition by col1 order by col2 asc) as last_flag
22     from data
23  )
24  where last_flag    != col3
25  or    last_flag    is null;
COL1         COL2                       COL LAS
ABCD         10-FEB-2012 12 00:00       N
ABCD         20-FEB-2012 12 00:00       Y   N
ABCD         01-MAR-2012 12 00:00       N   Y
ABCD         14-MAR-2012 12 00:00       Y   N
ABCD         25-MAR-2012 12 00:00       N   Y
ABCD         25-APR-2012 12 00:00       Y   N
6 rows selected.
Elapsed: 00:00:00.08
ME_XE?Cheers,

• Select records based on monthly anniversary date

Hi,
I have a table with a date_added field and I want to select records based on the monthly anniversary date of this field.
1, 10-DEC-2012
2, 11-NOV-2012
3, 10-MAR-2012
4, 28-FEB-2012
5, 30-DEC-2012
So For the 10th of Jan 2013, I would want to return records 1 and 3 only
I started looking at the extract function, but this soon falls down for records at the end of the month. For example, on the 28th Feb, I would also want to include records where the date_added day is the 29th, 30th or 31st. So, in the table above I would want to return records 4 and 5, but extract would only return 4.
Is there a simple function to do this month anniversary query - am I missing something very obvious? Or, do I need to write a query to explicitly cope with dates at the end of the month? So far I haven't found a sensible simple solution!
I'm using 11g
thanks

I didn't look into leap year, but this should give you a starting point:
select  *
from  t
where 1 = case last_day(to_date(:target_date,'mmddyyyy'))
when to_date(:target_date,'mmddyyyy')
then case
then 1
end
else case
then 1
end
end
/For example, target date is 1/10/2013:
SQL> variable target_date varchar2(8)
SQL> exec :target_date := '01102013';
PL/SQL procedure successfully completed.
SQL> with t as (
2             select 1 id,to_date('10-DEC-2012','dd-mon-yyyy') date_added from dual union all
3             select 2,to_date('11-NOV-2012','dd-mon-yyyy') from dual union all
4             select 3,to_date('10-MAR-2012','dd-mon-yyyy') from dual union all
5             select 4,to_date('28-FEB-2012','dd-mon-yyyy') from dual union all
6             select 5,to_date('30-DEC-2012','dd-mon-yyyy') from dual
7            )
8  select  *
9    from  t
10    where 1 = case last_day(to_date(:target_date,'mmddyyyy'))
11                when to_date(:target_date,'mmddyyyy')
12                  then case
14                           then 1
15                       end
16                else case
18                           then 1
19                       end
20              end
21  /
1 10-DEC-12
3 10-MAR-12
SQL> And target date is 2/28/2013:
SQL> exec :target_date := '02282013';
PL/SQL procedure successfully completed.
SQL> with t as (
2             select 1 id,to_date('10-DEC-2012','dd-mon-yyyy') date_added from dual union all
3             select 2,to_date('11-NOV-2012','dd-mon-yyyy') from dual union all
4             select 3,to_date('10-MAR-2012','dd-mon-yyyy') from dual union all
5             select 4,to_date('28-FEB-2012','dd-mon-yyyy') from dual union all
6             select 5,to_date('30-DEC-2012','dd-mon-yyyy') from dual
7            )
8  select  *
9    from  t
10    where 1 = case last_day(to_date(:target_date,'mmddyyyy'))
11                when to_date(:target_date,'mmddyyyy')
12                  then case
14                           then 1
15                       end
16                else case
18                           then 1
19                       end
20              end
21  /
4 28-FEB-12
5 30-DEC-12
SQL> SY.

• Retrive Records based on the content in a Clob Feild

Retrive Records based on the content in a Clob Feild
I have a Table NoteFile which contains a Clob Feild NoteFileContent
and is refrenced by PK DocID
i need to Match DocID with other Tables Document and folderdocument
My Query is as Below
SELECT Doc.Pap_Doc_ID, Doc.Pap_Doc_Name , NF.NoteFileContent
From NoteFile NF , Document Doc, folderdocument FD
Where Doc.Pap_Doc_ID = FD.FoldDoc_ID and FD.FoldDoc_ParentID <> 3 and
NF.DocID = DOC.Pap_Doc_ID And NF.NoteFileContent Like '%a%'
The above query throws
ORA-00600: internal error code, arguments: [504], [0x7ADEBE00], [2], [1],
[cache buffers chains], [723], [0], [0x7ADEBE00]

Hi,
We can resize it.Always give the height and weight in
percentage format. and turn off the horizontal n vertical scroll
policy in u r main application.
Hopes it helps you...

• SQL Query to retrieve the All records based on the Max Dates.

Hello all,
I am trying to retrieve the newest record based on the date field (  nextDate  ).
Currently there are only 4 records in the MC_Maintenance table and two in the Machine table.
Machine table
MC_id             EquipID
1                      0227
MC_id             EquipID
2                     0228
MC_Maintenance table
Maint_id           MC_id             Next_maint
1                      2                      08/25/2010
2                      2                      07/01/2010
3                      1                      06/11/2010
4                      1                      07/11/2010
What I  am trying to accomplish is,
list the two machines from the Machine table with the MAX(Next_maint) controlling the MC_Maintenance output list
These are the records that I would like to Display.
Maint_id           MC_id             Next_maint
1                      2                      08/25/2010
4                      1                      07/11/2010
Below is the SQL Query
SELECT
MC.MC_ID as ID,
MC.complete_Date as completed,
MC.next_maint as nextDate,
MC.maint_notes as Notes,
MC.facility as Facility,
M.EquipId,
M.name as name,
M.SerialNumber as SN,
M.dept as dept,
M.Freq as freq
From  MC_Maintenance MC, Machine M
where  MC.MC_ID =  M.MC_ID
'           USING MAX(nextDate )
Any ideas would help.
TJ

I would have thought that was a simple group by problem?
SELECT M.EquipID, MC.MC_ID, Max(MC.next_maint)
FROM MC_Maintenance MC INNER JOIN Machine M ON MC.MC_ID = M.MC_ID
GROUP BY M.EquipID, MC.MC_ID

• How to select records based on Max/Min on different columns and group by

I have a table with 5 columns(a,b,c,d,e), i need to select records based on MAX(c),Max(D) and Min(e) group by a,b. i am trying using : select max(c),max(d),min(e) from table group by a,b. this is not working. its giving me 1 6 1
a b c d e
1 1 1 2 1
1 1 1 6 4
1 1 1 6 3
when i group by a,b i am expecting the record 1 6 3

Hi,
Welcome to the forum!
962163 wrote:
I have a table with 5 columns(a,b,c,d,e), i need to select records based on MAX(c),Max(D) and Min(e) group by a,b. i am trying using : select max(c),max(d),min(e) from table group by a,b. this is not working. its giving me 1 6 1
a b c d e
1 1 1 2 1
1 1 1 6 4
1 1 1 6 3
when i group by a,b i am expecting the record 1 6 3It looks to me like "1 6 1" is the correct answer. You're asking for the lowest value of e, and 1 is lower than 3.
Maybe you don't want MIN (e). Explain why you want 3 (that is, how you decided that 3 is the correct value for the last column) and someone will help you code it.
Edited by: Frank Kulash on Sep 28, 2012 6:17 PM
Whenever you have a problem, you should psot CREATE TABLE and INSERT statements for your sample data. That way, the people who want to help you can re-create the problem and test their ideas. It often helps to clarify the problem, too. since this is your first message, I'll do it for you:
CREATE TABLE     table_x
(       a     NUMBER
,     b     NUMBER
,     c     NUMBER
,     d     NUMBER
,     e     NUMBER
INSERT INTO table_x (a, b, c, d, e) VALUES (1, 1, 1, 2, 1);
INSERT INTO table_x (a, b, c, d, e) VALUES (1, 1, 1, 6, 4);
INSERT INTO table_x (a, b, c, d, e) VALUES (1, 1, 1, 6, 3);
COMMIT;

• Web GUI new feature - creating a new record based on the current record

Vincent,
We need new feature in Web GUI:
creating a new record based on the current record with data
from Moscow with money

It is already enabled, in the next release.
If you joint TAB program, the march preview build contains it. User can select 1 or multiple users and make a copy.

• How to create a record based on the name of a file in the file-system?

Hi,
With a lot of pictures I want to have a database to gather some information about these pictures.
First question is how to generate a record based on a file in the file system?
e.g. the pictures are "c:\fotos\2009\01\disc_001.jpg" to "c:\foto\2009\01\dis_98.jpg" .
now i want to create records with as one of the attributes the name of the picture (not the picture itself). how to create these records (based on the information of the file-ssytem). i.e. the number of records should be the same as the number of pictures.
any suggestions?
any reaction will be appreciated.
Leo

You can create a list of files in the directory and read the list files from that directory.
SS

• Validating a selection field for meeting the ranges given in the Domain

Hi Experts,
Please let me know the proceedure to  Validating a selection field for meeting the ranges given in the Domain.
In my case, i am using the domain 'AUFTYP' Order category which has value range like 01 , 02 .... Which are displyed during F4 help.
Viven

Hi,
If the values are maintained at domain level guess the check will be done automatically. You dont have to validate the entries in the field.
Cheers
VJ

• My MacBook Pro recognizes which of my three printers is plugged in (USB), but can it automatically use that printer or do I have to select that printer from the list each time I switch between printers?

My MacBook Pro recognizes which of my three printers is plugged in (USB), but can it automatically use that printer or do I have to select that printer from the list each time I switch between printers?

My MacBook Pro recognizes which of my three printers is plugged in (USB), but can it automatically use that printer or do I have to select that printer from the list each time I switch between printers?

• Can a subreport select records based on values in the main report record?

Post Author: calvin
CA Forum: General
Perhaps my understanding of a subreport is incorrect as this is the first time I've used one, but it seems to me that the subreport should be able to use the values from the main report record in its (the subreport's) operations-but my subreport doesn't seem to be working that way.In my main report, I select a set of records from a 'request' table. I have a subreport in the detail section so the subreport is processed for each of the request records. That works, but I'm simply getting the same data reported multiple times. It's as if the subreport only uses the last request record rather than the current one. Stating it this way I can see that the problem might be evaluation time-it's processing the request records first, then processing the subreport, and only then printing everything. If this is correct then putting WhilePrintingRecords on the subreport should work-but the only way I know of to do that is in a formula. Can I call the subreport from a formula? Or am I totally off-track?Thanks.

Post Author: foghat
CA Forum: General
Have you established a link(s) between your main report and subreport?When viewing the main report, click edit --> subreport links and link the 2 based on whatever values from the main report you want.

• Select records based on criteria and update those records once read

hi,
I am very new to bpel and DB adapters.
I have a requirement where in I need to query two tables to fetch some records and update these selected records with a new value for field to indicate that bpel has processed these records.
Once I select these I needs the output to be mapped to the output variable.
I am able to select the records based on criteria , but how will i lock these records so that these records do not get processed again. This should be a very simple usecase just that I am not aware.
Thanks,
Robin

Once you have finished reading the records fire an update query , update some field in the table so that it does not get picked up next time.
if you are using polling for picking up the records, then use logical delete scenario, refer....http://docs.oracle.com/cd/E15523_01/integration.1111/e10231/adptr_db.htm#BABEEBIH

• Select records based on 1 field have either of 2 values

Good afternoon,
II have 2 tables, Account and Order, it's a one to many where 1 account can have many orders. I'm trying to pull the records where the account has orders that have a status of Completed or Quoted. I know I'm not explaining well so hopefully the example below will help.
Account A has 3 orders, all of them are completed.
Account B has 5 orders, 2 of them are completed, and 3 of them are quoted.
Account C has 2 orders, all of them are quoted.
I only want Account B to show up in my report. That is, I only want accounts that have both completed and quoted orders
I've tried various record selections but because the status field on a single order can only have one value I'm not sure how to get the results that I need.
Any help would be greatly appreciated.
Jeannette

hi Jeanette,
for this you may either have to write a report with a Command based object with a subselect and a WHERE IN clause.
there's an easier way using a Group Selection formula though...the disadvantage of this method is that this doesn't filter those records out at the database which could be a problem if you're dealing with 100's of thousands of records. if that's the case, then use the method mentioned at the top.
For a Group Selection method try the following:
1) create a Group on the Accountfield
2) create a formula called Completed that reads something like
if = 'Completed' then 1
3) create another formula called 'Quoted' that reads something like
if = 'Quoted' then 1
4) put these fields on the report and put a Summary on them at the Account Group level
5) go into the Group Selection Formula and find your group level summaries and create a formula similar to
Sum(AccountGroup, @Completed) > 0 and Sum(AccountGroup,@Quoted) > 1
This will bring back only Accounts with both completed and quoted records.
cheers,
jamie

• Creation of a new record based on the value of a picklist

Hi,
My requirement is whenever i select a picklist value in Opportunity record type based on the value selected a new opprtunity record should get created and get assigned to a role or A User otherwise a lead should get created and should be assigned to a Role or A User.
Can I achieve this in OnDemand?
Kindly help. This is urgent..

Can I achieve this in OnDemand?I think you can better try one of the Oracle CRM On Demand forums @ http://forums.oracle.com/forums/category.jspa?categoryID=162
instead of the Oracle SQL and PL/SQL forum.
This is urgent..The forums are relying on volunteers and it is weekend for most of us now.
If it is really that urgent, call Oracle Support.

• Changing the selection screen based on the option in list box option

Hi Experts,
I have a list box parameter in my selection screen,
i have four options and i am changing my selection screen based the option selected.
see my code below,
at selection-screen output.
GS_VRM_VALUES-KEY = '1'.
GS_VRM_VALUES-TEXT = TEXT-S02.
APPEND GS_VRM_VALUES TO GT_VRM_VALUES.
GS_VRM_VALUES-KEY = '2'.
GS_VRM_VALUES-TEXT = TEXT-S03.
APPEND GS_VRM_VALUES TO GT_VRM_VALUES.
GS_VRM_VALUES-KEY = '3'.
GS_VRM_VALUES-TEXT = TEXT-S04.
APPEND GS_VRM_VALUES TO GT_VRM_VALUES.
GS_VRM_VALUES-KEY = '4'.
GS_VRM_VALUES-TEXT = TEXT-S05.
APPEND GS_VRM_VALUES TO GT_VRM_VALUES.
CLEAR: GS_VRM_VALUES.
CALL FUNCTION 'VRM_SET_VALUES'
EXPORTING
ID                    = 'P_DROP'
VALUES                = GT_VRM_VALUES
EXCEPTIONS
ID_ILLEGAL_NAME       = 1
OTHERS                = 2
IF SY-SUBRC <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
LOOP AT SCREEN .
CASE P_DROP .
WHEN '1'.
IF SCREEN-GROUP1 = 'M2' OR
SCREEN-GROUP1 = 'M3' OR SCREEN-GROUP1 = 'M4'.
SCREEN-INPUT = 0.
SCREEN-ACTIVE = 0.
screen-invisible = 1.
MODIFY SCREEN.
ENDIF.
WHEN '2'.
IF SCREEN-GROUP1 = 'M3' OR SCREEN-GROUP1 = 'M4'.
SCREEN-INPUT = 0.
SCREEN-ACTIVE = 0.
screen-invisible = 1.
MODIFY SCREEN.
ENDIF.
WHEN '3'.
IF SCREEN-GROUP1 = 'M2' OR
SCREEN-GROUP1 = 'M4' .
SCREEN-INPUT = 0.
SCREEN-ACTIVE = 0.
screen-invisible = 1.
MODIFY SCREEN.
ENDIF.
WHEN '4'.
IF SCREEN-GROUP1 = 'M2' OR
SCREEN-GROUP1 = 'M3'.
SCREEN-INPUT = 0.
SCREEN-ACTIVE = 0.
screen-invisible = 1.
MODIFY SCREEN.
ENDIF.
WHEN OTHERS.
IF SCREEN-GROUP1 = 'M2' OR
SCREEN-GROUP1 = 'M3' OR SCREEN-GROUP1 = 'M4'.
SCREEN-INPUT = 0.
SCREEN-ACTIVE = 0.
screen-invisible = 1.
MODIFY SCREEN.
ENDIF.
ENDCASE.
ENDLOOP.
my problem is when i change the option in the list box, the particular modify group ie, the screen is getting changed only after i press enter, So please suggest me how i can change the screen without pressing enter.

Hi,
without using VRM_SET_VALUES...you just create one data element by going se11....create one domain for it...now give the values in the "value range" tab....and at the time of declaration just write ..
PARAMETERS var1 TYPE <your created data element>
AS LISTBOX VISIBLE LENGTH 20
USER-COMMAND onli
DEFAULT <one value>.
You will get the same effect as list box....and make it mandatory..
Arunima

### Maybe you are looking for

• Unable to sign in to iCloud, iMessage, FaceTime, or App Store

Ok, this is STUPID frustrating. I'm NOT a novice user with Mac's, but I'm stumped to figure this bug out...   13" MacBook Pro 5,5 2.53 GHz Core 2 Duo 8GB Ram 120GB SSD - OS & Applications 750GB 7200rpm HDD inplace of optical drive.  - User files/prof

• ATP not being triggered automatically after changing UE EXIT_SAPLATPC_002

Hello everyone. I am having a problem with ATP. This error is happening within VA02 (or VA01). I have just added some logic to EXIT_SAPLATPC_002 - include ZXATPU02 (which is in the modification ATP00001)  but now that I have made this change it preve

• Can someone help with this stuppid Media Encoder?!

I'm tired of reading hunderts of topics. Can someone please tell me why the Media Encoder is not encoding at all. I've tried everything, but my progressbar always shows loading 00:00:00. That is not normal. I'm sick of using expensive software, full

• Converting string to float

I have a decimal string "122339994" which i am trying to convert to float using Float.parseFloat. This results in incorrect value of 1.22339992E8. I thought for floats precsision comes into effect after decimal places. public static void main(String[