Search for records after (or before) a given date
HelloI am trying to build a query based on a pa_credat from the given input date, the following logic must be applied:
-Find the ID of the first (chronologically) record in a table where credat > = pa_credat.
-If no such record exists, find the first record where credat < pa_credat. First of all mean closer here in the time of pa_credat.
Consider the records in the table as a timeline. Since a certain date, I need to find the nearest registration or thereafter, and if there isn't, find the record closest before that date.
I'm doing all this in a sql statement, but I think that it is neither elegant nor efficient (on a large data set):
select objectid
from ( select objectid
from ( select objectid, credat
from ( select objectid, credat
from mytable
where credat >= pa_credat
order by credat asc )
where rownum = 1
union all
select objectid, credat
from ( select objectid, credat
from mytable
where credat < pa_credat
order by credat desc )
where rownum = 1 )
order by credat desc )
where rownum = 1
Is there a better approach to this problem?Some examples of data:
create table mytable
( objectid integer,
credat date );
insert into mytable values (1,to_date('01-03-2011','DD-MM-YYYY'));
insert into mytable values (2,to_date('02-03-2011','DD-MM-YYYY'));
insert into mytable values (3,to_date('03-03-2011','DD-MM-YYYY'));
insert into mytable values (4,to_date('04-03-2011','DD-MM-YYYY'));
insert into mytable values (5,to_date('05-03-2011','DD-MM-YYYY'));
insert into mytable values (6,to_date('06-03-2011','DD-MM-YYYY'));
insert into mytable values (7,to_date('07-03-2011','DD-MM-YYYY'));
The entry / the following output should01/01/2011-> objectid = 1
03/01/2011-> objectid = 1
03/03/2011-> objectid = 3
04/01/2011-> objectid = 7
My version of db is 10.2.0.4.0.
Thank you very much!
Pleiadians wrote:
Thanks, I'll try that!Based on your suggestions that I came to the next solution
select objectid from ( select objectid , row_number() over (order by sign(pa_credat-credat),abs(pa_credat-credat)) rn from mytable ) where rn = 1;
The rising sign order ensures that records with credat > pa_credat are first. The only problem is with the credat = pa_credat case... the sign = 0.
Right; SIGN returns-1, 0 or 1, but you want to 0 sort before-1. You can use CASES or DECODE to return-2 instead of 0. That would leave the ORDER BY clause with
- 2 expressions
- 1 CASE or DECODE
- 2 date arithmetic operations
- 2 (SIGN and ABS) function calls
Why would you want to do rather than what I posted, which contains
- 2 expressions (ditto)
- 1 CASE or DECODE (ditto)
- 0 day of arithmetic operations (not 2)
- function calls (not 2) 0
? Which means less coding? Which is more effective? Which seems easier to read and debug?
Tags: Database
Similar Questions
-
How can I search for files in a range of digital data
Is there a way to specify a range of digital data in Windows 7 search for example File1 through deposer10 included
Thank you
So you're saying that file names could be, for example, hotdog4.txt for hotdog15.txt? Well, you can look for in an orderly series, numerically and alphabetically, with two periods between the first and the last item, as in name:hotdog4.txt... hotdog15.txt
Interestingly, Windows by default notice when the names correspond to a digital frame and so will sort hotdog4 to hotdog10 and so on, despite 4 being a number greater than 1 Prime figure 10. I found a reference to this topic (but do not disable anything probably, just for the pleasure of reading):
-
I want to allow a user to enter a beginning date and to set the period they want to find records of members who have joined some end dates. Funny, it is... I got half of the working time. For example I have 4 folders between 26/10/2005 and 01/08/2006. When I enter 01/01/2005 as startDate and endDate 31/08/2006, I get 4 records. However, if I change the endDate to 09/01/2006 I get all records in the database! ??? Why is this? I can't get my head around it!
Here is my code:
First the code for the form for the user to input search criteria on:
< html >
< body >
< action = "FORM memberJDateSearch.cfm" method = "post" > "
< P > start date: < input type = "text" name = "startDate" >
< br > End Date: < input type = "text" name = "endDate" >
< input type is 'reset' value is 'Clear' >
< input type = "submit" value = "Submit" >
< / MAKE >
< / body >
< / html >
Pretty simple. Now, the code of the page process and display:
< html >
< body >
< cfquery "memberJDateSearch" datasource = name = access "jpkelle2" >
SELECT *.
Members
WHERE ((joinDate BETWEEN #CreateODBCDate (startDate) # AND #CreateODBCDate (endDate) #))
< / cfquery >
< table border = 1 bgcolor = "beige" cellpadding = '3' cellspacing = "0" >
< b >
< /Th > < th > Member ID
Name < th > < /th >
Sex < th > < /th >
< th > Birth Date < /th >
Address < th > < /th >
< th > Email < /th >
Date < th > joined < /th >
< /tr >
< CFOUTPUT Query = "memberJDateSearch" >
< b >
< td > < center > #memberID # < Center > < table >
< td width = "15" > #forename # #initial # #surname # < table >
< td > #sex # < table >
< td width = "10%" > #disp('#dob#') # < table >
< td > #address #, #town #, #county #, #postCode # < table >
< td > #email # < table >
< td width = "10%" > #disp('#joinDate#') # < table >
< /tr >
< / CFOUTPUT >
< /table >
< hr > < p > end of the list of members. < /p >
< / body >
< / html >
any ideas? Please help me.Try formatting your dates first (before the call to CreateODBCDate). I just tried this on my test page and it worked correctly. I removed calls DateFormat, now dates in your format, and it didn't work. See if something like the following will help you:
SELECT *.
Members
WHERE ((joinDate BETWEEN #CreateODBCDate (startDate) # AND #CreateODBCDate (endDate) #))
-
Search for files after system recovery
I had to do a system restore on my computer and when I did the description says that all my files would be in a separate folder (photos etc), but after that I did the cover I can't find the files anywhere... Any ideas?
Your best idea would be to contact the manufacturer of the computer for which you used the recovery process.
Ask them where these files are normally saved during / before their recovery process.And you can try looking for a windows.old folder.
See if this applies to you:
http://support.Microsoft.com/default.aspx/KB/933209
How to recover files from an earlier version of Windows in Windows Vista
See you soon.
Mick Murphy - Microsoft partner
-
I had several cases before the update and they appeared on the home screen, now I can't locate any folder. How do find you existing records?
Could you tell us where the records were initially? Local or Adobe Document Cloud?
Have you tried the following to Adobe Document Cloud? (I apologize if you already did).
- Open a web browser.
- Go to https://cloud.acrobat.com/
- Tap / click on "Document Cloud" under the storage section in the left pane.
You should see your files and folders in the cloud of Document Adobe regardless of whether if you connect from a desktop computer or iPad/iPhone.
You can have more than one Adobe (for example your and your husband). Please be sure to try all of them.
If you believe that your files and folders in Adobe Document cloud were lost, I would recommend asking a question in the forum services Cloud of Document PDF . Adobe staff in the forum should be able to look up the activities in your account (s).
-
Stuck on a sql query to search for records that have the same parent child records
Oracle 10 g 2 Enterprise Edition.
Hello
I'm writing a logic to find records in a parent table, who have the same values in a child table.
This is part of a larger application, but I am stuck on that part for now, so I have mocked some of the below simplified tables to capture the heart of the
the problem is that I'm stuck.
Let's say I have a responsible parent, child employee table table and there are a number of many relationships between them.
The aptly named Join_Table manages the relationship between them. If a manager can manage several employees, an employee can be managed by
many managers.
I have a feeling it's stupidly easy, but it seems to me having a bad episode of brain freeze today!
If I ask for Manager John, so I want to find other managers who manage the exact list and even employees.-- parent table CREATE TABLE manager ( id number primary key, name varchar2(100)); -- child table CREATE TABLE employee ( id number primary key, name varchar2(100)); -- link table CREATE TABLE join_table ( manager_id NUMBER, employee_id NUMBER, CONSTRAINT join_table_pk PRIMARY KEY (manager_id, employee_id), CONSTRAINT manager_fk FOREIGN KEY (manager_id) REFERENCES manager(id), CONSTRAINT employee_fk FOREIGN KEY (employee_id) REFERENCES employee(id) ); -- Insert some managers INSERT INTO manager (id, name) VALUES (1, 'John'); INSERT INTO manager (id, name) VALUES (2, 'Bob'); INSERT INTO manager (id, name) VALUES (3, 'Mary'); INSERT INTO manager (id, name) VALUES (4, 'Sue'); INSERT INTO manager (id, name) VALUES (5, 'Alan'); INSERT INTO manager (id, name) VALUES (6, 'Mike'); -- Insert some employees INSERT INTO employee (id, name) VALUES (101, 'Paul'); INSERT INTO employee (id, name) VALUES (102, 'Simon'); INSERT INTO employee (id, name) VALUES (103, 'Ken'); INSERT INTO employee (id, name) VALUES (104, 'Kevin'); INSERT INTO employee (id, name) VALUES (105, 'Jack'); INSERT INTO employee (id, name) VALUES (106, 'Jennifer'); INSERT INTO employee (id, name) VALUES (107, 'Tim'); -- Insert the links -- John manages Paul, Simon, Ken INSERT INTO join_table (manager_id, employee_id) VALUES (1, 101); INSERT INTO join_table (manager_id, employee_id) VALUES (1, 102); INSERT INTO join_table (manager_id, employee_id) VALUES (1, 103); -- Bob manages Paul, Simon, Kevin, Jack INSERT INTO join_table (manager_id, employee_id) VALUES (2, 101); INSERT INTO join_table (manager_id, employee_id) VALUES (2, 102); INSERT INTO join_table (manager_id, employee_id) VALUES (2, 104); INSERT INTO join_table (manager_id, employee_id) VALUES (2, 105); -- Mary manages Jennifer, Tim INSERT INTO join_table (manager_id, employee_id) VALUES (3, 106); INSERT INTO join_table (manager_id, employee_id) VALUES (3, 107); -- Sue manages Jennifer, Tim INSERT INTO join_table (manager_id, employee_id) VALUES (4, 106); INSERT INTO join_table (manager_id, employee_id) VALUES (4, 107); -- Alan manages Paul, Simon, Ken, Jennifer, Tim INSERT INTO join_table (manager_id, employee_id) VALUES (5, 101); INSERT INTO join_table (manager_id, employee_id) VALUES (5, 102); INSERT INTO join_table (manager_id, employee_id) VALUES (5, 103); INSERT INTO join_table (manager_id, employee_id) VALUES (5, 106); INSERT INTO join_table (manager_id, employee_id) VALUES (5, 107); -- Mike manages Paul, Simon, Ken INSERT INTO join_table (manager_id, employee_id) VALUES (6, 101); INSERT INTO join_table (manager_id, employee_id) VALUES (6, 102); INSERT INTO join_table (manager_id, employee_id) VALUES (6, 103); -- For sanity CREATE UNIQUE INDEX employee_name_uidx ON employee(name);
Answer should be Mike.
If I ask for Manager of Mary, the answer should be Sue.
This query will give me the list of managers who manage some of the same employees as John, but not the same employees accurate...
I thought about using set operations to find managers with a list of employees less than my employees is null and where my employees under their list of employees is null. But there must be an easier way more elegant.SELECT DISTINCT m.name AS manager FROM manager m, join_table jt, employee e WHERE m.id = jt.manager_id AND jt.employee_id = e.id AND e.id IN ( SELECT e.id FROM manager m, join_table jt, employee e WHERE m.id = jt.manager_id AND jt.employee_id = e.id AND m.name = 'John') ORDER BY 1;
Any ideas?
BTW, I need to run as a batch on tables with > 20 million rows so the efficiency of queries is key.What about...
WITH manager_list AS ( SELECT name, LTRIM(MAX(SYS_CONNECT_BY_PATH(id,',')) KEEP (DENSE_RANK LAST ORDER BY curr),',') AS employees FROM (SELECT m.name, e.id, ROW_NUMBER() OVER (PARTITION BY m.name ORDER BY e.id) AS curr, ROW_NUMBER() OVER (PARTITION BY m.name ORDER BY e.id) -1 AS prev FROM manager m, join_table jt, employee e WHERE m.id = jt.manager_id AND jt.employee_id = e.id AND m.name = :P_MANAGER) GROUP BY name CONNECT BY prev = PRIOR curr AND name = PRIOR name START WITH curr = 1 ), all_list AS ( SELECT name, LTRIM(MAX(SYS_CONNECT_BY_PATH(id,',')) KEEP (DENSE_RANK LAST ORDER BY curr),',') AS employees FROM (SELECT m.name, e.id, ROW_NUMBER() OVER (PARTITION BY m.name ORDER BY e.id) AS curr, ROW_NUMBER() OVER (PARTITION BY m.name ORDER BY e.id) -1 AS prev FROM manager m, join_table jt, employee e WHERE m.id = jt.manager_id AND jt.employee_id = e.id) GROUP BY name CONNECT BY prev = PRIOR curr AND name = PRIOR name START WITH curr = 1 ) SELECT a.* FROM manager_list m, all_list a WHERE m.employees = a.employees
Would be easier in 11g, but I do not have a facility here so this is based on 10g.
See you soon
Ben
-
Search for documents after adding user accounts
A few days earlier, I added two user accounts (an administrator and a user). After that I added these accounts and started to use it, I discovered that I could not find or access my old documents (word .doc files, etc.). Is anyway to find or recover them? Thank you.
Hello
Were they stored in my documents folder in the profiles earlier if yes, use admin previlages on an account, you will be able to access documents stored in profiles more early. You will need to navigate to c:\Documents and Settings, you will find all the profile names. Navigate to the required profile and check in my documents.
-
Query do not search for records
Dear all,
IAM trying to select a set of rows in a table based on the closed_date column that is a data type date.
Select to_char (closed_date,' HH24 MON-dd-yy') of productionorder where closed_date between 31 December 04 ' AND ' 1 January 05'
This query returns me 9 rows as shown below:
TO_CHAR(CLOSED_DATE,'DD-MON-YYHH24:MI')
31-dec-04-08:00
31-dec-04-08:00
31-dec-04-08:00
31-dec-04-08:00
31-dec-04-08:00
31-dec-04-08:00
31-dec-04-08:00
31-dec-04-08:00
31-dec-04-08:00
However, when I use the following query, it does not return me the same set of rows expected!
Select to_char (closed_date,' HH24 MON-dd-yy') of productionorder where closed_date between 30 December AND 31 December 04 ' 04 '
There is no record...
Why these records are not displayed?
Thank you pl. help...
MaheshTry this...
Select to_char (closed_date,' HH24 MON-dd-yy')
of productionorder
When trunc (closed_date) between trunc (TO_DATE('30-DEC-04','DD-MON-YY')) AND trunc (TO_DATE('31-DEC-04','DD-MON-YY')) -
regular expression to search for records with only numbers
Hello
I need a query to find only numebers. My collar is of type varchar and has values such as
col1
--------------------------
1234456789
madh144reddy
123end
end123
I need ouput as only numbers EG
o/p should be
1234456789
REGEXP_LIKE (col1, ' [[: digit :]]');]])Hello, sorry misunderstood, try:
WITH test_tab AS ( SELECT '1234456789' col1 FROM DUAL UNION ALL SELECT 'madh144reddy' FROM DUAL UNION ALL SELECT '123end' FROM DUAL UNION ALL SELECT 'end123' FROM DUAL) -- end test data SELECT * FROM test_tab WHERE REGEXP_LIKE(col1,'^[[:digit:]]+$'); COL1 ------------ 1234456789
-
The search for the best way to get the data
Sorry for my bad English.
* 1. Definition * the task
I have the table "tab01":
ID; EVENT_DATE; Item1; Item2... itemN
______________________________
1; 01.01.2009; A; null; ... null
2; 02.01.2009; B; 1; ... null
3; 03.01.2009; null; 2; ... 3
4; 04.01.2009; null; null; ... 4
I need to get the last non-null column values (item1... itemN) on the date chosen (result line).
For example, the result on 03.01.2009 will be:
EVENT_DATE; Item1; Item2... itemN
______________________________
03.01.2009; B; 2 ... 3
* 2. My decision *.
My decision (it works, but I think that's not good) was:
1. create the package with
(a) methods to get and set the date,
(b) the functions which are the values required;
2. create the view based on the functions of the package.
create or replace package is P_STATE
-The public variable declarations
Trunc (sysdate) default date StateDate;
-Declarations of function and public procedure
procedure SetStateDate (date date);
function GetStateDate return date;
function GetItem1 return tab01.item1%type;
...
function GetItemN return tab01.itemN%type;
end P_STATE;
create or replace package body is P_STATE
procedure SetStateDate (date date) is
Start
StateDate: = Al;
end;
function GetStateDate return date is
Start
Return StateDate;
end;
function tab01.item1%type return GetItem1 is
Result tab01.item1%type;
cursor c1 is select item1 tab01
where item1 is not null and
EVENT_DATE < = StateDate
event_date desc order.
Start
Open c1;
Fetch c1 into result.
Close c1;
Return (result);
end;
...
function GetItemN return tab01.itemN%type is
...
end P_STATE;
create or replace view v_tab01
(date_on, item1,... itemN)
in select
p_state. GetStateDate,
p_state. GetItem1,
...
p_state. GetItemN
Double;
Can you help me to find the best way?You might if it were a feature in pipeline. Check it out here: [http://www.akadia.com/services/ora_pipe_functions.html]
-
Search for items that are in one node to a date given (responses/dashboard)
Hi and thanks for reading,
I have the following problem:
An element is in arriving at a node and starting from this node a little later. In my table, I have the finish and the start datetime. Now, I would like to create a guest/filter on the dashboard, so that I give in a datetime and the dashboard shows me all the elements that are there to this date. For the moment I have two guests for arrival is less than the given date and departure is larger than the given date. But I would like to have a single table that filters for all the elements that are there to this date.
Any suggestions are appreciated
EvgenyYou also have another solution.
You can use a command prompt and set a variable presentation date then in your report filter:
arrival is smaller than the presentation variable and departure is bigger than the presentation variable
Check here how to set a variable of presentation:
http://gerardnico.com/wiki/dat/OBIEE/obiee_setting_variableClick here for the file server:
http://gerardnico.com/wiki/dat/OBIEE/presentation_service/obiee_presentation_variable_area#filterAnd here for the format of the default mask:
http://gerardnico.com/wiki/dat/OBIEE/logical_sql/obiee_date_variableSee you soon
Nico -
Reg: Search for nullable columns
Hi all
I have a DIAGNOSIS table that has a QDESC as a nullable column.
DIAGCODE and SDESC columns are not nullable.
I wrote a stored procedure to retrieve the records from the table of DIAGNOSIS based on the values of columns specified.
P_ < nom_de_colonne > indicates that the parameter passed to the stored procedure
Correspong SQL statement goes as follows.
SELECT * DIAGNOSTIC
WHERE
UPPER (DIAGCODE) AS
(CASE WHEN LENGTH (p_DIAGCODE) > 0 THEN '%' |) Upper (p_DIAGCODE) | '%' ELSE '%' END)
AND UPPER (SDESC) AS
(CASE WHEN LENGTH (p_SDESC) > 0 THEN '%' |) Upper (p_SDESC) | '%' ELSE '%' END)
-QDESC is a nullable field; but the following logic doesn't :-(
AND UPPER (QDESC) AS
(CASE WHEN LENGTH (p_QDESC) > 0 THEN '%' |) Upper (p_QDESC) | '%' ELSE '%' END)
Examples of data in the table is as follows.
DIAGCODE SDESC QDESC
--------------- ------------ ------------
123 / / DESC 1 AAA
123 / / DESC 2
123 / / DESC 3 BBB
123 / / DESC 4
When I use the query above to search for records with DIAGCODE like 123, I get
DIAGCODE SDESC QDESC
--------------- ------------ ------------
123 / / DESC 1 AAA
123 / / DESC 3 BBB
That is to say QDESC with null values are not displayed.
How am I supposed to change the query to get the correct result?
Any help would be much appreciated.
Thank you best regards n,.
TanujaThere are several illusions here.
First of all: SQL has a logic to three values: TRUE, FALSE and NULL. The result of any operator is TRUE or FALSE and that you can only search for NULL values explicitly using IS NULL.
Second: You need the dynamic SQL statements: immediate execution or dbms_SQL. Your variable is a column name, not a constant, if you need to build a string that contains the required SQL statement and present the use execute sql immediately or dynamics.
This will reduce scalability.
Third: < function name > < some constant > = (column) will ensure that any index on < column > is not used.
Usually, this will result in catastrophic performance.-----
Sybrand Bakker
Senior Oracle DBA -
How auto search for fotos duplicate within an event
When I down load pictures from different sources on my iMac, there are opportunities to double, but the pictures do not appear next to each other, as the dates of these photos are not the same; so in a file large, they could be anywhere. I don't know there is a way to automatically search for duplicates by using images rather than dates, etc.?
iPhoto has no tools to detect duplicates.
Try third-party applications to find duplicates:
- Duplicate Annihilator
- iPhoto Library Manager
- Sweeper duplicate
are known to be safe to use with iPhoto.
-
vCAC 6.1 Catalogue right - cannot search for or select users or group?
I want to install vCAC 6.1 from scratch and try to entitle a group of users for my first article in catalog. When I'm on the Administration > catalog management > rights tab and type "domain admins" ordomain\domain admins"to search for a user I just get"no data to display.
I thought that my source of identity was the issue then I double checked that...
And the strange thing is that I can seek this identity store when I'm under Administration > groups
I don't know why I can't find users and groups above, but I can look for them in the admin > groups option. I have been stuck on this for about a day and can't get anywhere. I restarted the identity goes, the vcac goes and the iaas server. The logs will show that nothing is wrong either.
I was walking around videos from youtube by Yves Sandfort. I watched all the videos of the series and followed, but at 10:08 in the video, I get to a stopping point.
VCAC VMware 6.0 - based hypervisor Blueprint by Yves Sandfort - YouTube
I am new to vCAC so I could have done something wrong. I hope that its simple. Let me know. Thank you!
JoeJust make sure, but domain administrators have user rights in the Group of companies to ensure that this right is? If they have not so you will not be able to grant them rights to this right.
-
The search for Clob columns in interactive report
I use APEX 3.2 with 10g. I have an interactive report with a query that takes several recordings of 'comments' and the concatenates using a function that returns a Clob. In the report, there are several of these comments columns mixed with some small varchar2 columns.
Using the search bar, if I type in a Word and click OK, it does not seek my Clob columns. On the other hand, I can use the drop-down list in the search bar to indicate I want to only search one of these Clob columns, then the research works.
Is there a reason that my filters 'Text line contains' do not include my Clob columns? I have defined, as the "Standard report column" and they all "filter" checked as a permitted operation.
GregI found a work around for this problem. IR reports do not seem to automatically search for CLOB columns as a search ROWTEXT (unless you are specifically targeting this column). So, I wrapped the CLOB column with:
DBMS_LOB.substr(clob_column_name,4000,1)
This converts the CLOB Varchar2 column, which made the report «see» IR Of course, this only works if you want to search for the first 4000 bytes, but the data in my CLOB columns are never more than a few sentences long.
Maybe you are looking for
-
Download Firefox on an iPhone 5avec the last updated? If so, which would be that only
I bought an app from iTunes Store. A browser with the option of FF or Chrome. I am not one of them is compatible
-
Can I recover my settings somehow?
When I started today in Firefox I had lost my settings. The character has by default, the saved passwords where gone and my home page has been reset. My toolbars are still here but NoScript is restarted so all my NoScript exceptions have disappeared.
-
help please
-
ITunes is separating the artists and albums! ID3 are correct...
I updated my Itunes to 12.3.2.35 this week on my Windows 10. A week before I organized my music library so I know perfectly how to treat the ID3 tags and how to fix it when Itunes is separating an album or artist. But since I updated the version that
-
Printer prints multiple copies of the page
Help I have an old HP LaserJet 4250n. It works well. Recently it started to print several pages when I try to print something 1. I had teacher business at my school trouble shoot her and she has not seen anything like that. I set it up to print on