Insert select on the same table: possible without side effects?
I have a very large table T1 containing millions of records. I need to treat its lines and create a few new lines based on selection.
Table T1 contains events and one of them, with the code 100, is created by the further development of other events inside the table.
My code is as follows:
insert /*+append */ into T1 (code,...) values (100, c1,c2,...) select c1,c2... from T1 where (code=20 or code=10) and <other conditions>...
as you can see I'm extract T1 lines to insert again in T1 with a different code and I use the direct path in order to get good performance.
My fear is: choose is made from the same table I risk data loss? In general it is a good practice? Or is it better to create another table?
Hello
No I don't think that there may be loss of data. But that may depend on the behavior of the application and your where clause.
I will explain how it is treated, so you can see if it's ok:
1. the table is locked because of the insert add, (as)
2 lines are read by select and make compatible from the State that was at the beginning of the query - 1.
3 rows are inserted at the end, after the high-water line
4 columns for new lines are sorted to be merged in the index
5. high watermark is adjusted - visible new lines and lock is released
Note that 2. and 3. occur at the same time: rows are inserted all read.
Note that anyone can choose in the table during the operation - they see the changes committed only - if the State 1.
all other DML are waiting for the lock being released, and will see new ranks and then
If you have things that prevent the direct-path insert, append the hint will be ignored. So, if you must rely on close to 1. then the best lock explicitly with table lock. But I don't think that you need.
Kind regards
Franck.
Tags: Database
Similar Questions
-
Support for mandatory request - insert, select from the same table
Hi all
I need your help to write queries effectively.
Oracle Version: 10.2.0.3.0
OPERATING SYSTEM: UNIX
I have a METRICS_TBL that is mentioned below table.
CYCLE_DATE METRIC VALUE
08/17/2008 COST-TV 100
08/17/2008 COST-JOURNAL 50
08/17/2008 COST-POSTALMAIL 25
08/17/2008-PROD-TV 10
08/17/2008-PROD-JOURNAL 25
08/17/2008-PROD-POSTALMAIL 5
Any data above, I have to add (Insert into select METRICS_TBL METRICS_TBL) at the same table with the records as mentioned below.
2008-08-17 COSTPERPROD-TV 10
08/17/2008-COSTPERPROD-LOG 2
2008-08-17 COST PROD-POSTALMAIL 5
Basically, I need to calculate the cost per product for each category. Depending on the settings available, metric should also be changed as COSTPERPROD and values should be cost/prod under each category.
Can someone help me with the query.
Thank youSomething like this:
INSERT INTO metrics_tbl (cycle_date, metrics, value) SELECT cost.cycle_date , 'COSTPERPROD-'||cost.mtype ,cost.value / prod.value FROM ( select cycle_date,substr(metrics, 1, 4) mtype, substrmetrics,instr(metrics,'-')+1) mmetric where substr(metrics, 1, 4) = 'PROD' ) prod INNER JOIN ( select cycle_date,substr(metrics, 1, 4) mtype, substrmetrics,instr(metrics,'-')+1) mmetric where substr(metrics, 1, 4) = 'COST' ) cost on cost.cycle_date = prod.cycle_date and cost.mmetric = prod.mmetric
Ideally you would divide your METRICS column into two: one for the thing you're measuring (e.g. TV or NEWSPAPER) and the other for the metric (COST, PROD, COSTPERPROD etc.). That's what I did in the views online. Without it, it's kinda a mess and behave so, if the table becomes much more.
HTH
Nigel cordially
Edited by: nthomas on January 14, 2009 15:57 - small correction SQL
-
Insert data into the same table based on certain conditions
Hello. I'm new to this forum.
I have to write a stored procedure to insert data in a table MYTABLE say, having a structure like:
Col1 Col2 Col3... TotalInstallments CurrentInstallment PaidAmount MonthYear
I have to insert all the data that it is in the same table (MYTABLE) except change some fields based on certain conditions:
1. if PaidAmount > 0 & & CurrentInstallment < TotalInstallment then
CurrentInstallment = CurrentInstallment + 1
2. in the field MonthYear I have data ex. 01/2012, 11/2012 formate(month/year)...
So, I have to insert data by incrementing the month and year. for example:
If currentdata is 11/2012 next data will be 12/2012
But following will be 01, 2013
I have to select all records that belongs to the previous month (across the field MonthYear) and put the audit on each record selected and insert data and then turns them into table (MYTABLE) even.
How to achieve that?
Thank you.978184 wrote:
Hello. I'm new to this forum.
I have to write a stored procedure to insert data in a table MYTABLE say, having a structure like:Col1 Col2 Col3... TotalInstallments CurrentInstallment PaidAmount MonthYear
I have to insert all the data that it is in the same table (MYTABLE) except change some fields based on certain conditions:1. if PaidAmount > 0 & CurrentInstallment
CurrentInstallment = CurrentInstallment + 1
2. in the field MonthYear I have data ex. 01/2012, 11/2012 formate(month/year)...
So, I have to insert data by incrementing the month and year. for example:
If currentdata is 11/2012 next data will be 12/2012
But following will be 01, 2013
I have to select all records that belongs to the previous month (across the field MonthYear) and put the audit & on each of the selected data record and insert then turns them into table (MYTABLE).You can do this way:
This is not tested, but if you can provide the example of table structure and data (IN create table and insert scripts), it can be put to the test.
insert into your_table (col1, col2, col3...current_installment, month_field) select col1, col2, col3.., current_installment + case when paidamount > 0 and current_installment < total_installment then row_number() over ( partition by column1, column2,.. columnn -->You may choose partition if you want the --Increment of Current_installment to reset after particular combination ends order by primary_key -->Order the Increment, you may choose to add more columns to order by ) else 0 --> if condition is not met, then Add 0 end curr_installment, add_months(to_date(month_field, 'MM/YYYY'), 1) nxt_month from your_table;
-
Trigger on the nested table - possible without sight?
I have a table my_table that has a nested table column, DOC. I need a trigger that fires whenever a row in the nested table is deleted.
I had initially hoped that a status UPDATE on my_table trigger would do the trick, but it doesn't. Then I created a trigger on the nested table.
I can make it work if I create a table view and then make the trigger through this:
"INSTEAD OF DELETE
ON the TABLE IMBRIQUEE my_view DOC
'
and then to do the removal through the view:
deletion OF TABLE
(Doc SELECT FROM my_view where rowid = 1111);
HOWEVER, I need this to work when the delete statement is directly through the table itself and not through the view. The delete statement is generated by an application that I can't control. I can only make changes to the database.
deletion OF TABLE
(Doc SELECT FROM my_table where rowid = 1111);Create the trigger on the nested table.
create or replace type result_obj as object(subject varchar2(100), mark integer) / create or replace type result_tbl as table of result_obj / create table student(sno integer, sname varchar2(100), result result_tbl) nested table result store as result_tbl_tbl / insert into student values(10, 'Karthick', result_tbl(result_obj('English',85),result_obj('Hindi',70))) / insert into student values(20, 'Vimal', result_tbl(result_obj('English',75),result_obj('Hindi',90))) / select s.sno, s.sname,r.subject, r.mark from student s, table(s.result) r / create or replace trigger Result_Trigger before delete on result_tbl_tbl for each row begin raise_application_error(-20001,'You cannot delete this subject'); end; / delete from table(select s.result from student s where s.sno = 10) where subject = 'Hindi' /
-
Update, and select from the same table
Hello
I have this selection - I've tested and work
[code]
Select case When DTF_REEW_201301.KTMO = 1 then DTF_REEW_201301.KTBT of other DTF_REEW_201301.KTBT - CTF_REEW_KUM_201301.KTBT end as KTBT_ISO,
case When DTF_REEW_201301.KTMO = 1 then DTF_REEW_201301.KTZN of other DTF_REEW_201301.KTZN - CTF_REEW_KUM_201301.KTZN end as KTZN_ISO,
case When DTF_REEW_201301.KTMO = 1 then DTF_REEW_201301.KTAB of other DTF_REEW_201301.KTAB - CTF_REEW_KUM_201301.KTAB end as KTAB_ISO,
DTF_REEW_201301.brnrn, DTF_REEW_201301.ktat Of
reewcore. CTF_REEW_KUM_201301 CTF_REEW_KUM_201301, reewdq. DTF_REEW_201301 DTF_REEW_201301 where
(DTF_REEW_201301.BRNRN = CTF_REEW_KUM_201301.BRNRN and DTF_REEW_201301.KTAT = CTF_REEW_KUM_201301.KTAT) and CTF_REEW_KUM_201301.KTMO = (DTF_REEW_201301.KTMO - 1). [/ code]
With the result of KTBT_ISO, KTZN_ISO and KTAB_ISO I want to update the table 'reewdq. DTF_REEW_201301' - and this picture is in the select statement.
I think I've tried every update statement - but no update does not work.
I need something like this:
[code]
Update reewdq. DTF_REEW_201301 set t1 t1. KTBT_ISO =
(
Select case When DTF_REEW_201301.KTMO = 1 then DTF_REEW_201301.KTBT of other DTF_REEW_201301.KTBT - CTF_REEW_KUM_201301.KTBT end as KTBT_ISO Of
reewcore. CTF_REEW_KUM_201301 CTF_REEW_KUM_201301, reewdq. DTF_REEW_201301 DTF_REEW_201301 where
(DTF_REEW_201301.BRNRN = CTF_REEW_KUM_201301.BRNRN and DTF_REEW_201301.KTAT = CTF_REEW_KUM_201301.KTAT) and CTF_REEW_KUM_201301.KTMO = (DTF_REEW_201301.KTMO - 1). ),
T1. KTZN_ISO =
(
Select case When DTF_REEW_201301.KTMO = 1 then DTF_REEW_201301.KTZN of other DTF_REEW_201301.KTZN - CTF_REEW_KUM_201301.KTZN end as KTZN_ISO Of
reewcore. CTF_REEW_KUM_201301 CTF_REEW_KUM_201301, reewdq. DTF_REEW_201301 DTF_REEW_201301 where
(DTF_REEW_201301.BRNRN = CTF_REEW_KUM_201301.BRNRN and DTF_REEW_201301.KTAT = CTF_REEW_KUM_201301.KTAT) and CTF_REEW_KUM_201301.KTMO = (DTF_REEW_201301.KTMO - 1). ),
T1. KTAB_ISO =
(
Select case When DTF_REEW_201301.KTMO = 1 then DTF_REEW_201301.KTAB of other DTF_REEW_201301.KTAB - CTF_REEW_KUM_201301.KTAB end as KTAB_ISO Of
reewcore. CTF_REEW_KUM_201301 CTF_REEW_KUM_201301, reewdq. DTF_REEW_201301 DTF_REEW_201301 where
(DTF_REEW_201301.BRNRN = CTF_REEW_KUM_201301.BRNRN and DTF_REEW_201301.KTAT = CTF_REEW_KUM_201301.KTAT) and CTF_REEW_KUM_201301.KTMO = (DTF_REEW_201301.KTMO - 1). )
[/ code]
But this isn´t of the labour code. Someone an idea please? Can someone help me please.
Best regards
Heidi
Use MERGE:
Fusion
in reewdq. DTF_REEW_201301 t1
a_l'_aide_de)
Select the case sensitive option
When DTF_REEW_201301.KTMO = 1 then DTF_REEW_201301.KTBT
of other DTF_REEW_201301.KTBT - CTF_REEW_KUM_201301.KTBT
end as KTBT_ISO,
case
When DTF_REEW_201301.KTMO = 1 then DTF_REEW_201301.KTZN
of other DTF_REEW_201301.KTZN - CTF_REEW_KUM_201301.KTZN
end as KTZN_ISO,
case
When DTF_REEW_201301.KTMO = 1 then DTF_REEW_201301.KTAB
of other DTF_REEW_201301.KTAB - CTF_REEW_KUM_201301.KTAB
end as KTAB_ISO,
reewdq. ROWID that RID
of reewcore. CTF_REEW_KUM_201301 CTF_REEW_KUM_201301,
reewdq. DTF_REEW_201301 DTF_REEW_201301
where DTF_REEW_201301.BRNRN = CTF_REEW_KUM_201301.BRNRN
and DTF_REEW_201301.KTAT = CTF_REEW_KUM_201301.KTAT
and CTF_REEW_KUM_201301.KTMO = DTF_REEW_201301.KTMO - 1
) t2
on)
T1. ROWID = t2.rid
)
When Fachhochschule
then
Update
the t1 value. KTBT_ISO = t2. KTBT_ISO,
T1. KTZN_ISO = t2. KTZN_ISO,
T1. KTAB_ISO = t2. KTAB_ISO
/
SY.
-
UPDATE on the same table in the sub query
DB version: 11.2
We have a table called SHP_GC_TRACK, which has about 8 million records with partitions. In the below UPDATE, it updates a column based on the SELECT on the same table in a subquery.
This UPDATE takes a long time to run and sometimes get hung up.UPDATE shp_gc_track a SET f_tran_proc = 'Y' WHERE last_update_date < (SELECT MAX (last_update_date) FROM shp_gc_track b WHERE a.shp_trx_rowid = b.shp_trx_rowid AND a.c_shp_inst = b.c_shp_inst AND a.f_tran_proc = b.f_tran_proc AND b.f_ltr_received = 'D' AND f_rec_code IN ('G', 'W') AND b.f_rec_status = 'B' AND b.c_shp_inst = :b1 ) AND a.c_shp_inst = :b1 AND a.f_ltr_received = 'D' -----------------> part of composite index AND a.f_tran_proc = 'N' -----------------> part of composite index AND a.f_rec_code IN ('G', 'W') --------------> part of composite index AND a.f_rec_status = 'B'; -----------------> part of composite index
We have a composite index on four columns f_ltr_received, f_rec_code, f_rec_status and f_tran_proc. Explain the plan shows that this composite index is used.
Any way to rewrite this query or suggestion?Steve_74 wrote:
DB version: 11.2We have a table called SHP_GC_TRACK, which has about 8 million records with partitions. In the below UPDATE, it updates a column based on the SELECT on the same table in a subquery.
UPDATE shp_gc_track a SET f_tran_proc = 'Y' WHERE last_update_date < (SELECT MAX (last_update_date) FROM shp_gc_track b WHERE a.shp_trx_rowid = b.shp_trx_rowid AND a.c_shp_inst = b.c_shp_inst AND a.f_tran_proc = b.f_tran_proc AND b.f_ltr_received = 'D' AND f_rec_code IN ('G', 'W') AND b.f_rec_status = 'B' AND b.c_shp_inst = :b1 ) AND a.c_shp_inst = :b1 AND a.f_ltr_received = 'D' -----------------> part of composite index AND a.f_tran_proc = 'N' -----------------> part of composite index AND a.f_rec_code IN ('G', 'W') --------------> part of composite index AND a.f_rec_status = 'B'; -----------------> part of composite index
This UPDATE takes a long time to run and sometimes get hung up.
We have a composite index on four columns f_ltr_received, f_rec_code, f_rec_status and f_tran_proc. Explain the plan shows that this composite index is used.
Any way to rewrite this query or suggestion?
Setting updates with subqueries may be difficult: (.) Unfortunately my suggestions below are of the try-it-and-see-what-happens variety - nothing of certain
First of all, check the index. Is it bitmap or tree? If the tree to see if the more restrictive columns are listed first - this can help with effectiveness of b-tree indexes. Also if the tree a composite bitmap for columns with lots of repeat values instead could help
Its a correlated subquery so that you cannot run just the subquery first put the result in a scalar varaiable and using the variable in the SQL instead. You can try putting the keys w/join subuqery results in a TWG first to use TWG in SQL to see if I/O is reduced together during these two operations.
You have the license for the parallel query option? Using parallel DML (this must be activated manually) can help. Check the documentation of the ALTER SESSION command to do so. In addition, the PARALLEL_INDEX() indicator could help
Display the SQL execution plan
-
Possible to insert more than once in the same table using multi table insert?
I used multi-table insert before insert a source separate from the tables without problem.
However, when I want to insert into the same table two or more distinct lines, I encountered a problem because when I call nextval on the sequence since this is the same sequence # will try to use the same number for two inserts, which will raise a violation of PK constraint on the 2nd insert.
A way around this problem?Another option is to create the sequence with increases of 2 or however many tables, you are targeting.
When you then specify the. NEXTVAL, you must subtract the relative number of it, apart from the first table that you insert in to.Something like that;
drop table t1; create table t1 (a number, b number); drop table t2; create table t2 (a number, b number); drop sequence test_seq / create sequence test_seq increment by 2 minvalue 0 / insert all when 1 = 1 then into t1 (a, b) values (test_seq.nextval, 1) when 1 = 1 then into t2 (a, b) values (test_seq.nextval - 1, 1) select 1, 1 from dual /
-
place a select query calculation in a different column in the same table
How can I put my calculation result in a column named within the same table?
I have a table called: dgpercentagedatachart
I use the columns of this dgpercentagedatachart: totalcecrating divided by lowestfeederrating times 100 to get the percentage
In the query, I gave the result the Alias of the cal
What I want is to put this result in my application or my calculation (in percentage) in my column "percent" on my table of dgpercentagedatachart vacuum.
How can I configure this syntax?
This is the select query, I came with:
Select dgpercentagedatachart.totalcecrating, dgpercentagedatachart.lowestfeederrating,.
100.00*dgpercentagedatachart.totalcecrating/dgpercentagedatachart.lowestfeederrating as cal
of dgpercentagedatachart;
Here are the results:
CAL lowestfeederrating Totalcecrating
8,978 7.48166666666666666666666666666666666667 120
30.951 25.7925 120
5.04 120 4.2
Hello
2685870 wrote:
How can I put my calculation result in a column named within the same table?
I have a table called: dgpercentagedatachart
I use the columns of this dgpercentagedatachart: totalcecrating divided by lowestfeederrating times 100 to get the percentage
In the query, I gave the result the Alias of the cal
What I want is to put this result in my application or my calculation (in percentage) in my empty column '%' on my table of dgpercentagedatachart .
How can I configure this syntax?
This is the select query, I came with:
Select dgpercentagedatachart.totalcecrating, dgpercentagedatachart.lowestfeederrating,.
100.00*dgpercentagedatachart.totalcecrating/dgpercentagedatachart.lowestfeederrating as cal
of dgpercentagedatachart;
Here are the results:
CAL lowestfeederrating Totalcecrating
8,978 7.48166666666666666666666666666666666667 120
30.951 25.7925 120
5.04 120 4.2
To change an existing column in a table, you can use the UPDATE or MERGE, instructions like this:
UPDATE dgpercentagedatachart
Percentage of VALUE = 100,00 * totalcecrating
/ lowestfeederrating
;
Noramlly, tables is not redundant columns like this. If percent can always be calculated from other columns, then it is probably better to calculate at run time and store it in the database, where you will have to be updated each time the columns it depends on change. You can use a view to avoid having to encode them the calculation.
If you really don't want a column that can be calculated in the same lines other columns, then use a virtual column (if you use Oracle 11 or higher).
I hope that answers your question.
If this isn't the case, please post a small example of data (CREATE TABLE and only relevant columns, INSERT statements) for all of the tables involved and also publish outcomes from these data.
If you ask on a DML statement, such as UPDATE, the sample data will be the content of the or the tables before the DML, and the results will be the State of the or the tables changed when it's all over.
Explain, using specific examples, how you get these results from these data.
Always say what version of Oracle you are using (for example, 11.2.0.2.0).
See the FAQ forum: Re: 2. How can I ask a question on the forums?
-
Mr President
If I have the two LOV in the same table then how to connect when I select first and second LOV value also change?
My two fields are FLOW AND DR_NAME
FLOW = ACCT_ID
DR_NAME = ACCT_NAME
I created with success of LOV for these fields.
First LOV gives acct_id in the debit field and second LOV gives the value of acct_name to dr_name.
How can I report these lov, it's that when I change my acct_id then acct_name also change
I have these two tables
CREATE TABLE "NOM" ( "ACCT_ID" VARCHAR2(7) NOT NULL ENABLE, "ACCT_NAME" VARCHAR2(50) NOT NULL ENABLE, "O_BAL" NUMBER(13,2), CONSTRAINT NOM_PK PRIMARY KEY ("ACCT_ID")ENABLE ); CREATE TABLE "VOUCHERDET" ( "V_ID" VARCHAR2(9) NOT NULL ENABLE, "LINEITEM" NUMBER , "DEBIT" VARCHAR2(7) , "DR_NAME" VARCHAR2(50), "CREDIT" VARCHAR2(7) , "CR_NAME" VARCHAR2(50), "PARTICULARS" VARCHAR2(100), "AMOUNT" NUMBER(21,2), CONSTRAINT VOUCHERDET_PK PRIMARY KEY ("V_ID","LINEITEM")ENABLE, CONSTRAINT PUR_SAL_LINE_POD_FK FOREIGN KEY(PROD_ID) REFERENCES PRODUCTS (PROD_ID)ENABLE, CONSTRAINT VOUCHERDET_DEBIT_FK FOREIGN KEY ("DEBIT") REFERENCES "NOM" ("ACCT_ID") ENABLE, CONSTRAINT VOUCHERDET_CREDIT_FK FOREIGN KEY ("CREDIT") REFERENCES "NOM" ("ACCT_ID") ENABLE, CONSTRAINT VOUCHERDET_V_FK FOREIGN KEY ("V_ID") REFERENCES "VOUCHER" ("V_ID") ON DELETE CASCADE ENABLE );
Concerning
so, instead of this second ActId, choose ACCT_NAME:
-
Sinlge select query in the diff for the same table (same Structure) diagrams
Scenario:
Table XYZ is created in detail a.
After a year, the old data of the previous year could be moved to another schema. However in the other schema of the same table name would be used.
For example
A schema contains XYZ table with data from the year 2012
Schema B contains XYZ table with data for the year 2011
Table XYZ in the two schemas have an identical structure.
So we can draw a single select query to read the data from the tables in an effective way.
For example select * from XYZ so including date between October 15, 2011 to March 15, 2012.
However, the data resides in 2 different schema altogether.
Creating a view is an option.
But my problem, there are ORM (Hibernate or Eclipse Top Link) layer between the application and the database.
If the queries would be constituted by the ORM layer and are not generated by hand.
So I can't use the view.
So is there any option that would allow me to use only query on different scheme?970773 wrote:
Scenario:Table XYZ is created in detail a.
After a year, the old data of the previous year could be moved to another schema. However in the other schema of the same table name would be used.For example
A schema contains XYZ table with data from the year 2012
Schema B contains XYZ table with data for the year 2011
Table XYZ in the two schemas have an identical structure.So we can draw a single select query to read the data from the tables in an effective way.
For example select * from XYZ so including date between October 15, 2011 to March 15, 2012.
However, the data resides in 2 different schema altogether.Creating a view is an option.
But my problem, there are ORM (Hibernate or Eclipse Top Link) layer between the application and the database.
If the queries would be constituted by the ORM layer and are not generated by hand.
So I can't use the view.Why not make the ORM as below?
SELECT * FROM VIEW_BOTH;
-VIEW_BOTH is a real VIEW of Oracle -
comparing string values in the same table
I have a table with ID (14 digit string values) starting with "A". Ex: A21849B1020792. There may be a different ID with the same substring 'B1020792' in the same table, example: A12349B1020792. If this happens, the ID of the last creation date of must be returned in the result. In other words, comparison is done on the same table. "In the example provided, say A12349B1020792 has the date of 1 January 2015 'and A21849B1020792 has date February 1, 2015", since A21849B1020792 has the most recent date, the result must contain only A21849B1020792.
ID create_date
--- ------------------
A21849B1020792 02/01/2015
A12349 B1020792 01/01/2015
A12345B1234567 01/03/2015
A43567B1234567 01/04/2015
Here's the query I used:substr (a.id, 7) select sc, max (a.create_date) cd1
of table a, table b
where a.id! = b.id
and substr (a.id, 7) = substr (b.id, 7)
and a.id like 'A %' and b.id like 'A %' / * (I used a % because I am interested only IDs that begin with A) * /.
and a.id ('A12349B1020792', 'A21849B1020792','A12345B1234567 ','A43567B1234567')
Group of substr (a.id, 7);
Result:
---------
sc cd1
--- ------
B1020792 02/01/2015
B1234567 04/01/2015
There is one thing that I'm not able to do with my request, that is, be able to get real IDs list and not list of substrings. If I added a.id column in the select clause and the group by clause, the query includes both by substr (a.id, 7) and a.id and as a result, the query returns four lines as below:
substr (a.id, 7) select sc, max (a.create_date) cd1, a.id ID
of table a, table b
where a.id! = b.id
and substr (a.id, 7) = substr (b.id, 7)
and a.id like 'A %' and b.id like 'A %' / * (I used a % because I am interested only IDs that begin with A) * /.
and a.id ('A12349B1020792', 'A21849B1020792','A12345B1234567 ','A43567B1234567')
Group by substr (a.id, 7), a.id.
Result:
---------
sc cd1 ID
--- ------ ---------
01/01/2015 A12349B1020792 B1020792
B1020792 01/02/2015 A21849B1020792
B1234567 01/03/2015 A12345B1234567
B1234567 01/04/2015 A43567B1234567
My goal is to retrieve a list of all the unique identifiers that have the later dates.
ID:
---
A21849B1020792
A43567B1234567
Thanks for your time in advance!
Hello
user11951344 wrote:
I have a table with ID (14 digit string values) starting with "A". Ex: A21849B1020792. There may be a different ID with the same substring "B1020792" in the same table, example: A12349B1020792. If this is the case, the ID of the last creation date must be returned in the result. In other words, comparison is done on the same table. "In the example provided, say A12349B1020792 updated 1 January 2015 ' and A21849B1020792 a date February 1, 2015", as A21849B1020792 has the most recent date, the result should contain only the A21849B1020792.
ID create_date
--- ------------------
A21849B1020792 02/01/2015
A12349B1020792 01/01/2015
A12345B1234567 03/01/2015
A43567B1234567 01/04/2015
Here's the query I used:
substr (a.id, 7) select sc, max (a.create_date) cd1
of table a, table b
where a.id! = b.id
and substr (a.id, 7) = substr (b.id, 7)
and a.id like 'A %' and b.id like 'A %' / * (I used a % because I am interested only IDs that begin with A) * /.
and a.id ('A21849B1020792', 'A12345B1234567', 'A12349B1020792', 'A43567B1234567')
Group of substr (a.id, 7);
Result:
---------
sc cd1
--- ------
B1020792 02/01/2015
B1234567 01/04/2015
There is one thing that I am not able to do with my request, that is, the ability to retrieve the list of IDs real and not a list of substrings. If I added a.id column in the select clause and the group by clause, the query groups according to the two substr (a.id, 7) and a.id and as a result, the query returns four lines as below:
substr (a.id, 7) select sc, max (a.create_date) cd1, a.id ID
of table a, table b
where a.id! = b.id
and substr (a.id, 7) = substr (b.id, 7)
and a.id like 'A %' and b.id like 'A %' / * (I used a % because I am interested only IDs that begin with A) * /.
and a.id ('A21849B1020792', 'A12345B1234567', 'A12349B1020792', 'A43567B1234567')
Group by substr (a.id, 7), a.id.
Result:
---------
sc cd1 ID
--- ------ ---------
B1020792 01/01/2015 A12349B1020792
B1020792 02/01/2015 A21849B1020792
B1234567 03/01/2015 A12345B1234567
B1234567 01/04/2015 A43567B1234567
My goal is to retrieve a list of all the unique identifiers that have the later dates.
ID:
---
A21849B1020792
A43567B1234567
Thanks for your time in advance!
If it makes sense to treat the first 6 characters of the ID separately from the rest of the id in this problem, maybe it makes sense to store those two parts of the id in two different columns. Relational databases work best when each column of each row contains 1 single piece of data (at most). It is so fundamental to the design of table that he called the first normal form.
Given that the two parts are stored in column 1, so you can something like this Request Top - N:
WITH got_r_num AS
(
SELECT r.id
r.create_date AS cd1
, RANK () OVER (PARTITION OF SUBSTR (r.id, 7))
ORDER BY r.create_date DESC
) AS r_num
FROM table_x g - g for data values
JOIN table_x r - r for related values
ON SUBSTR (r.id, 7) = SUBSTR (g.id, 7)
WHERE g.id IN ('A12349B1020792'
, "A21849B1020792".
, "A12345B1234567".
, "A43567B1234567".
)
- AND g.id LIKE 'A %' - if necessary. The above condition ensures already g.id start with "A".
AND r.id LIKE 'a % '.
)
SELECT id, create_date
OF got_r_num
WHERE r_num = 1
;
If you would care to post CREATE TABLE and INSERT instructions for the sample data, and then I could test this.
-
Update of several rows of the same table using trigger
Hello
I have the following table
---------------------------------------------------------------------------------------------------------
CREATE TABLE ATT_PERMISSIONS ( DATE OF PRMONTHYR, EMPID NUMBER (10) DEFAULT VALUE 0 , FROMDATETIME DATE , TODMTFDATETIME DATE , NUMBER OF PERMMINS (3), SSLINASTRETCH NUMBER (3).
PERMATTINELIGIBLE NUMBER (1) DEFAULT VALUE 0 ); ------------------------------------------------------------------------------------------------------
I want to update the column PERMATTINELIGIBLE in the same table when each line is inserted.
I created following trigger for that
CREATE OR REPLACE TRIGGER SSL_AFTER_UPDATE AFTER UPDATE ON ATT_PERMISSIONS
FOR EACH LINE
BEGIN
Update ATT_PERMISSIONS set PERMATTINELIGIBLE =
(
SELECT THE CHECK BOX
WHEN PERMMINS < = 60
or
(
PERMMINS < = SSLINASTRETCH
AND
COUNT (CASE WHEN PERMMINS = 0 THEN NULL OTHERWISE 1 END)
COURSES (PARTITION BY empid, prmonthyr) < = 1
)
THEN 1
0 OTHERWISE
END
FROM ATT_PERMISSIONS where prmonthyr =:new.prmonthyr, empid =:new.empid
) where prmonthyr =:new.prmonthyr, empid =:new.empid;
END;
---------------------------------------------------------------------------------------------------
That is to say.
When a row is inserted the PERMATTINELIGIBLE of all records for this employee for the month is recalculated.
There would be only two or three records for an employee for a month.
PERMMINS is the number of minutes to use as short leave.
If it is less than 60 is eligible for the bonus to attendance for this employee.
If this isn't normally the date is not eligible for the attendance for this employee premium.
but an exception is that if a short leaves of DISP employee allowed a month (IE. ( SSLINASTRETCH ) in a straight line, IE. one day, that day is eligible for the bonus of presence.
That is to say. Why the number of short leaves in a month is taken. If it's one and done minutes use is lower to SSLINASTRETCH, then the day is eligible for the attendance bonus.
But when I try to create the trigger I get error, PL/SQL: ORA-00907: missing a right parenthesis.
Help, please
I use oracle 10g
I want to update the column PERMATTINELIGIBLE in the same table, when each row is inserted.
No - you do NOT want to do in a trigger.
You cannot query the same table that the trigger is activated on. -C' is a MUTANT and you will get an exception.
When a row is inserted is recalculated to the PERMATTINELIGIBLE of all the records for that employee for that month.
There would be only two or three records for an employee for a month.
PERMMINS is the number of minutes to use as short leave.
If it is less than 60 is eligible for the bonus to attendance for this employee.
If is not normally the day is not eligible for the premium of attendance for this employee.
but an exception is that if a short leaves of DISP employee allowed a month (IE. SSLINASTRETCH) in a straight line, IE. one day, that day is eligible for the bonus of presence.
That is to say. Why the number of short leaves in a month is taken. If it's one and done minutes use is less than SSLINASTRETCH, then the day is eligible for the bonus of presence.
But when I try to create the trigger I get error, PL/SQL: ORA-00907: missing a right parenthesis.
You can not do in a trigger. Even if correct you this error of syntax, you will get the exception that I mentioned above.
Oracle is a multi-user system. Other users can modify the same table that you use.
So even if you could try to interview "all the records for this employee" some of them may be locked if other users access them. Also, an insert statement could try to insert two rows for an employee and the trigger could not have seen the other lines still.
If you want to work with data SETS (all records for an employee), you must write the PL/SQL code that LOCKS all lines updated to prevent others to interfere.
Then you make your operations but NOT in a trigger. Perform inserts and then use an UPDATE query to set this value.
Then VALIDATE to unlock the lines.
-
Help with career within the same table and find the relevant rank.
HI all the gurus
Pls help me out here... I need to implement some which is dependent on SUPERIOR record following found regarding one earlier. Some examples of data.
create table SOR_OFF (OFF_ID, OFFS_ID number, REPORT number, CON_DATE number date, STATE varchar2 (20));
REM INSERTING into SOR_OFF SET DEFINE OFF; Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,CON_DATE,STATUS) values (987,349,30658,to_date('12-SEP-04','DD-MON-RR'),'Active'); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,CON_DATE,STATUS) values (988,349,30658,to_date('29-DEC-98','DD-MON-RR'),'Active'); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,CON_DATE,STATUS) values (990,349,30658,to_date('29-JUL-96','DD-MON-RR'),'Active'); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,CON_DATE,STATUS) values (991,350,30658,to_date('27-NOV-90','DD-MON-RR'),'Active'); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,CON_DATE,STATUS) values (992,352,30658,to_date('04-OCT-91','DD-MON-RR'),'Active'); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,CON_DATE,STATUS) values (993,353,30658,to_date('12-JUN-95','DD-MON-RR'),'Active'); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,CON_DATE,STATUS) values (994,353,30658,to_date('21-NOV-83','DD-MON-RR'),'Active'); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,CON_DATE,STATUS) values (995,355,30658,to_date('21-APR-94','DD-MON-RR'),'Active'); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,CON_DATE,STATUS) values (996,356,27250,to_date('23-SEP-94','DD-MON-RR'),'Active'); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,CON_DATE,STATUS) values (997,357,30658,to_date('15-MAY-95','DD-MON-RR'),'Active'); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,CON_DATE,STATUS) values (998,358,30658,to_date('13-JAN-93','DD-MON-RR'),'Active'); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,CON_DATE,STATUS) values (999,358,30658,to_date('13-MAR-93','DD-MON-RR'),'Active'); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,CON_DATE,STATUS) values (1000,359,30658,to_date('17-DEC-96','DD-MON-RR'),'Active'); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,CON_DATE,STATUS) values (1003,360,30658,to_date('22-JUN-92','DD-MON-RR'),'Active'); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,CON_DATE,STATUS) values (1004,361,30658,to_date('15-FEB-94','DD-MON-RR'),'Active'); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,CON_DATE,STATUS) values (1006,362,30658,to_date('26-JUL-94','DD-MON-RR'),'Active'); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,CON_DATE,STATUS) values (1007,364,30658,to_date('01-JUL-92','DD-MON-RR'),'Active'); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,CON_DATE,STATUS) values (1009,365,30658,to_date('20-MAY-99','DD-MON-RR'),'Active'); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,CON_DATE,STATUS) values (1010,365,30658,to_date('01-JUL-99','DD-MON-RR'),'Active'); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,CON_DATE,STATUS) values (1011,366,30658,to_date('18-JUN-84','DD-MON-RR'),'Deleted'); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,CON_DATE,STATUS) values (1012,366,30658,to_date('15-JUL-94','DD-MON-RR'),'Active'); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,CON_DATE,STATUS) values (1013,366,30658,to_date('15-JUL-94','DD-MON-RR'),'Active'); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,CON_DATE,STATUS) values (1014,367,30658,to_date('07-NOV-94','DD-MON-RR'),'Active'); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,CON_DATE,STATUS) values (1015,368,30658,to_date('29-MAY-90','DD-MON-RR'),'Active'); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,CON_DATE,STATUS) values (1016,369,30658,to_date('10-SEP-92','DD-MON-RR'),'Active'); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,CON_DATE,STATUS) values (1020,370,30658,to_date('07-JUL-92','DD-MON-RR'),'Active'); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,CON_DATE,STATUS) values (1026,372,30658,to_date('23-JUL-93','DD-MON-RR'),'Active'); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,CON_DATE,STATUS) values (1027,373,30658,to_date('30-OCT-96','DD-MON-RR'),'Active'); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,CON_DATE,STATUS) values (1030,375,30658,to_date('03-OCT-91','DD-MON-RR'),'Active'); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,CON_DATE,STATUS) values (1034,376,30658,to_date('20-APR-95','DD-MON-RR'),'Active'); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,CON_DATE,STATUS) values (1037,378,30658,to_date('07-APR-92','DD-MON-RR'),'Active'); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,CON_DATE,STATUS) values (1039,379,30658,to_date('26-JAN-94','DD-MON-RR'),'Active'); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,CON_DATE,STATUS) values (1040,380,30658,to_date('12-MAY-93','DD-MON-RR'),'Active'); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,CON_DATE,STATUS) values (1041,381,30658,to_date('08-JAN-93','DD-MON-RR'),'Active'); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,CON_DATE,STATUS) values (1042,382,13428,to_date('27-JAN-92','DD-MON-RR'),'Active'); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,CON_DATE,STATUS) values (1043,383,30658,to_date('19-APR-91','DD-MON-RR'),'Active'); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,CON_DATE,STATUS) values (1044,384,30658,to_date('17-DEC-54','DD-MON-RR'),'Active'); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,CON_DATE,STATUS) values (1045,385,27884,to_date('03-APR-90','DD-MON-RR'),'Active'); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,CON_DATE,STATUS) values (1046,386,28480,to_date('01-DEC-92','DD-MON-RR'),'Active'); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,CON_DATE,STATUS) values (1048,388,30658,to_date('18-AUG-92','DD-MON-RR'),'Active'); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,CON_DATE,STATUS) values (1050,389,3894,to_date('26-AUG-88','DD-MON-RR'),'Active'); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,CON_DATE,STATUS) values (1051,389,30658,to_date('09-APR-96','DD-MON-RR'),'Active'); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,CON_DATE,STATUS) values (1053,391,30658,to_date('30-NOV-90','DD-MON-RR'),'Active'); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,CON_DATE,STATUS) values (1054,392,30658,to_date('27-MAY-93','DD-MON-RR'),'Active'); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,CON_DATE,STATUS) values (1055,393,3894,to_date('05-JUN-91','DD-MON-RR'),'Active'); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,CON_DATE,STATUS) values (1056,394,30658,to_date('29-MAY-96','DD-MON-RR'),'Active'); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,CON_DATE,STATUS) values (1057,395,3894,to_date('15-JUL-94','DD-MON-RR'),'Active'); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,CON_DATE,STATUS) values (1058,396,30658,to_date('22-FEB-91','DD-MON-RR'),'Active'); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,CON_DATE,STATUS) values (1061,398,30658,to_date('14-MAR-90','DD-MON-RR'),'Active'); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,CON_DATE,STATUS) values (1062,398,30658,to_date('14-MAR-90','DD-MON-RR'),'Active'); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (52810,37334,27250,'ACTIVE',to_date('25-JUL-01','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (52811,37334,27250,'ACTIVE',to_date('23-NOV-08','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (21648,17315,27250,'Deleted',to_date('02-JAN-05','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (24740,17315,27250,'ACTIVE',to_date('05-JAN-05','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (8622,6410,27250,'Active',to_date('26-JUN-98','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (44079,6410,27250,'ACTIVE',to_date('27-OCT-08','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (54828,38266,13428,'ACTIVE',to_date('31-JUL-96','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (55278,38266,13428,'ACTIVE',to_date('10-SEP-98','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (55279,38266,13428,'ACTIVE',to_date('13-NOV-01','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (48172,34748,17442,'ACTIVE',to_date('03-SEP-02','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (48173,34748,17442,'ACTIVE',to_date('01-APR-05','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (45319,33194,9950,'ACTIVE',to_date('03-APR-98','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (45320,33194,9950,'ACTIVE',to_date('21-FEB-06','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (46803,34029,30862,'ACTIVE',to_date('05-MAY-06','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (46804,34029,30862,'ACTIVE',to_date('26-OCT-06','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (53886,34029,30658,'ACTIVE',to_date('18-NOV-09','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (13479,11608,14966,'Active',to_date('16-NOV-99','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (13480,11608,14966,'Active',to_date('01-JAN-01','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (60251,40687,27250,'ACTIVE',to_date('23-JAN-09','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (60252,40687,27250,'ACTIVE',to_date('14-JUL-09','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (17454,14278,604,'Active',to_date('10-AUG-99','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (17455,14278,1232,'Active',to_date('27-OCT-03','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (52438,37084,27250,'ACTIVE',to_date('23-JUL-03','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (52648,37084,27250,'ACTIVE',to_date('17-FEB-04','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (20653,16456,27250,'Deleted',to_date('02-JUN-04','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (31000,16456,0,'Deleted',null); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (38241,16456,27250,'ACTIVE',to_date('28-APR-05','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (54130,37980,1232,'ACTIVE',to_date('08-MAR-00','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (56129,37980,1232,'ACTIVE',to_date('07-AUG-08','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (55025,38388,30622,'ACTIVE',to_date('06-DEC-98','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (55032,38388,30622,'ACTIVE',to_date('01-DEC-10','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (55517,38657,27884,'ACTIVE',to_date('04-FEB-10','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (55518,38657,27884,'ACTIVE',to_date('17-AUG-09','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (55519,38657,27884,'ACTIVE',to_date('21-JUL-09','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (44764,32871,12408,'ACTIVE',to_date('18-JUN-04','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (44765,32871,12408,'ACTIVE',to_date('16-JUN-04','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (44766,32871,12408,'ACTIVE',to_date('16-JUN-04','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (19076,15174,27250,'ACTIVE',to_date('02-NOV-02','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (50173,15174,1232,'ACTIVE',to_date('16-SEP-08','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (45046,33005,27250,'ACTIVE',to_date('03-SEP-92','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (45160,33005,27250,'ACTIVE',to_date('08-FEB-01','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (45161,33005,27250,'ACTIVE',to_date('06-NOV-06','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (44118,32387,27250,'ACTIVE',to_date('22-MAR-96','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (44119,32387,9950,'ACTIVE',to_date('30-DEC-04','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (44120,32387,9950,'ACTIVE',to_date('28-SEP-05','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (48418,34888,19586,'ACTIVE',to_date('02-MAR-90','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (48419,34888,28480,'ACTIVE',to_date('18-DEC-07','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (48420,34888,28480,'ACTIVE',to_date('24-AUG-04','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (43307,31893,29598,'ACTIVE',to_date('26-APR-04','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (43437,31893,9950,'ACTIVE',to_date('20-SEP-07','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (53612,37738,13428,'ACTIVE',to_date('28-JUN-04','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (53613,37738,15208,'ACTIVE',to_date('15-DEC-08','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (21555,17241,13428,'ACTIVE',to_date('11-JUN-99','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (21556,17241,13428,'ACTIVE',to_date('02-JAN-02','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (41336,30685,14966,'ACTIVE',to_date('07-OCT-03','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (42363,30685,9950,'ACTIVE',to_date('20-NOV-06','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (50926,36355,30862,'ACTIVE',to_date('13-DEC-07','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (50928,36355,30862,'ACTIVE',to_date('13-SEP-07','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (25520,20471,27250,'ACTIVE',to_date('06-JUN-98','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (25521,20471,0,'ACTIVE',to_date('31-AUG-04','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (2035,1069,30658,'Active',to_date('29-JAN-92','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (36762,1069,9950,'ACTIVE',to_date('16-MAY-05','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (36763,1069,9950,'ACTIVE',to_date('17-AUG-05','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (29807,23548,18458,'ACTIVE',to_date('18-AUG-00','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (29826,23548,17868,'ACTIVE',to_date('13-AUG-02','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (12818,10852,1232,'Active',to_date('01-JUN-98','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (12988,10852,1232,'Active',to_date('01-JUL-00','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (44287,32502,27884,'ACTIVE',to_date('10-JAN-08','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (44917,32502,9950,'ACTIVE',to_date('09-JUL-08','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (14430,12464,15070,'Active',to_date('03-APR-02','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (14431,12464,30658,'Active',to_date('08-NOV-89','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (42691,12464,30622,'ACTIVE',to_date('18-JAN-06','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (21608,17278,14966,'ACTIVE',to_date('03-AUG-00','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (25145,17278,14966,'ACTIVE',to_date('01-NOV-00','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (54918,38323,17868,'ACTIVE',to_date('21-MAR-02','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (55607,38323,17868,'ACTIVE',to_date('22-APR-10','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (28304,22497,1232,'ACTIVE',to_date('29-NOV-01','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (40755,22497,1232,'ACTIVE',to_date('13-DEC-06','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (13239,11318,19002,'Deleted',to_date('01-APR-99','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (13240,11318,19002,'Active',to_date('01-JAN-00','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (54317,38064,14966,'ACTIVE',to_date('01-DEC-99','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (54318,38064,30862,'ACTIVE',to_date('14-OCT-05','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (60404,38064,30658,'ACTIVE',to_date('31-AUG-11','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (60405,38064,30658,'ACTIVE',to_date('31-AUG-11','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (52800,37327,27250,'ACTIVE',to_date('27-JUN-02','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (52801,37327,27250,'ACTIVE',to_date('27-JUN-02','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (52802,37327,27250,'ACTIVE',to_date('19-DEC-99','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (41154,30599,27250,'ACTIVE',to_date('02-APR-02','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (46115,30599,27250,'ACTIVE',to_date('15-SEP-04','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (48532,34983,1232,'ACTIVE',to_date('16-SEP-05','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (48533,34983,1232,'ACTIVE',to_date('16-SEP-06','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (26868,21438,1232,'ACTIVE',to_date('13-JAN-05','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (26909,21438,1232,'ACTIVE',to_date('13-DEC-05','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (54646,38177,28480,'ACTIVE',to_date('02-DEC-99','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (54647,38177,28480,'Deleted',null); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (54648,38177,28480,'ACTIVE',to_date('01-NOV-04','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (26924,21471,10672,'ACTIVE',to_date('16-JAN-97','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (27385,21471,10672,'ACTIVE',to_date('12-NOV-97','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (27404,21471,3894,'ACTIVE',to_date('02-MAR-01','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (40329,30063,28480,'ACTIVE',to_date('07-OCT-02','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (40407,30063,28480,'ACTIVE',to_date('27-OCT-05','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (27949,22235,27250,'ACTIVE',to_date('07-JUL-98','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (27950,22235,27250,'ACTIVE',to_date('07-JUL-03','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (32387,25414,19032,'ACTIVE',to_date('18-APR-00','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (46214,25414,19032,'ACTIVE',to_date('18-APR-06','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (32226,25340,30658,'ACTIVE',to_date('25-MAY-05','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (33363,25340,27884,'ACTIVE',to_date('11-SEP-99','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (33364,25340,27884,'ACTIVE',to_date('08-FEB-01','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (55789,38840,1830,'ACTIVE',to_date('22-MAR-98','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (55790,38840,1830,'ACTIVE',to_date('22-MAR-98','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (55792,38840,1830,'ACTIVE',to_date('20-MAR-06','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (1976,1026,30658,'Active',to_date('21-JUN-94','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (47881,1026,1232,'ACTIVE',to_date('25-SEP-09','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (47882,1026,1232,'ACTIVE',to_date('21-JUN-04','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (13820,11900,27250,'Active',to_date('15-MAY-98','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (35519,11900,27250,'ACTIVE',to_date('18-JAN-99','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (35520,11900,27250,'ACTIVE',to_date('14-AUG-00','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (35521,11900,30658,'ACTIVE',to_date('03-DEC-02','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (35524,11900,30658,'ACTIVE',to_date('21-MAR-06','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (35525,11900,27250,'ACTIVE',to_date('29-AUG-01','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (26168,20977,27250,'ACTIVE',to_date('24-OCT-05','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (27644,20977,27250,'Deleted',to_date('29-OCT-05','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (27764,20977,27250,'Deleted',to_date('28-OCT-05','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (50681,20977,27250,'ACTIVE',to_date('28-OCT-05','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (49856,35737,27250,'ACTIVE',to_date('03-DEC-97','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (49858,35737,27250,'ACTIVE',to_date('19-JUL-07','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (41260,30649,27250,'ACTIVE',to_date('01-JUL-98','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (41261,30649,27250,'ACTIVE',to_date('01-JUN-98','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (8290,6012,14966,'Active',to_date('16-OCT-98','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (8291,6012,14966,'Active',to_date('24-FEB-99','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (49978,35835,9950,'ACTIVE',to_date('10-SEP-01','DD-MON-RR')); Insert into SOR_OFF (OFF_ID,OFFS_ID,STATE,STATUS,CON_DATE) values (49979,35835,9950,'ACTIVE',to_date('31-MAY-06','DD-MON-RR'));
What I want is less than
-Search for a line more low which is CON_DATE > = to_date ('01-NOV-1998', ' MON-DD-YYYY "") AND STATE = 30658 and get the second row whose state = 30658 but having separate con_date
or CON_DATE = > = to_date (November 1, 1997 ',' MON-DD-YYYY "") AND STATE <>30658 & get the second row including State = 30658 but having separate con_date
and lower (status) = 'active '.
and get another line that extends beyond the found line, but having separate con_date (no operation of the same day.)
I tried to do it with and then attach it again with the same table, but I don't get different results...
Please advise.
Hello
n_shah18 wrote:
NO Frank
Thanks for trying.
First find a line that corresponds
((CON_DATE > = to_date (1er novembre 1998 ', ' MON-DD-YYYY ") AND STATE = 30658))
or (CON_DATE > = to_date (November 1, 1997 ',' MON-DD-YYYY "") AND STATE <> 30658))
8 second get/check the following line immediately for offs_id even that has different con_date, that is superior to prev con_date but State should be 3065
If offs_id found then return else throw line...
Thus, in addition to the conditions that I listed previously, there must be a line with the same offs_id, which has a con_date later and the State concerned; is this fair?
You can use a self-join to see if there is such an extra line:
WITH params AS
(
SELECT TO_DATE (November 1, 1998 ', 'DD-Mon-YYYY') AS later_threshold_date
AS target_state 30658
11900 AS target_offs_id
OF the double
)
SELECT s.offs_id
OF s sor_off
JOIN params p ON s.offs_id = p.target_offs_id
AND ((s.con_date > = p.later_threshold_date))
AND s.state = p.target_state
)
OR (s.con_date > = ADD_MONTHS (p.later_threshold_date, 12))
AND s.state <> p.target_state
)
)
JOIN sor_off ON s2.offs_id = s.offs_id s2
AND s2.con_date > s.con_date
AND s2.state = p.target_state
GROUP BY s.offs_id
;
The only difference between this and the previous solution is the last join, in other words, the 3 lines of code just before the GROUP BY clause.
-
First occurrence but not in the same table
As we know, to search for the first occurrence of a particular record, we go with the MIN function. It is however, if all of your records are in the same table.
My scenario is a little different. I have 4 tables (maybe more), but keeping this is simple for the exercise. I need to find the first occurrence of a student who appear in a table and this registration Date. The trick is to NOT include this student in the following tables. To do this, I can use SQL or PL/SQL.
Thanks in advance.
Scripts for Tables (CREATE TABLE and INSERT scripts)
create table time_period(id varchar2(1), start_date date, end_date date); insert into time_period values('0', to_date('01-DEC-2005', 'DD-MON-YYYY'), to_date('31-MAY-2006','DD-MON-YYYY')); insert into time_period values('1', to_date('01-JUN-2006', 'DD-MON-YYYY'), to_date('30-NOV-2006','DD-MON-YYYY')); insert into time_period values('2', to_date('01-DEC-2006', 'DD-MON-YYYY'), to_date('31-MAY-2007','DD-MON-YYYY')); insert into time_period values('3', to_date('01-JUN-2007', 'DD-MON-YYYY'), to_date('30-NOV-2007','DD-MON-YYYY')); insert into time_period values('4', to_date('01-DEC-2007', 'DD-MON-YYYY'), to_date('31-MAY-2008','DD-MON-YYYY')); create table student_test1(student_id varchar2(4), exam_date date); create table student_test2(student_id varchar2(4), exam_date date); create table student_test3(student_id varchar2(4), exam_date date); create table student_test4(student_id varchar2(4), exam_date date); insert into student_test1 values('1001',to_date('01-DEC-2005','DD-MON-YYYY'); insert into student_test1 values('1002',to_date('01-DEC-2005','DD-MON-YYYY'); insert into student_test2 values('1003',to_date('01-JUN-2006','DD-MON-YYYY'); insert into student_test3 values('1001',to_date('01-NOV-2007','DD-MON-YYYY'); insert into student_test3 values('1004',to_date('01-NOV-2007','DD-MON-YYYY'); insert into student_test4 values('1001',to_date('01-MAY-2008','DD-MON-YYYY'); insert into student_test4 values('1002',to_date('01-MAY-2008','DD-MON-YYYY'); insert into student_test4 values('1003',to_date('01-MAY-2008','DD-MON-YYYY'); insert into student_test4 values('1004',to_date('01-MAY-2008','DD-MON-YYYY');
Desired output should be as follows:
ID Start Date End Date County
0 1 December 05 31 May 06 2 - student ID 1001 and 1002
1 1 June 06 30 November 06 1 - Student ID 1003
2 1 December 06 31 May 07 0 - no
3 1st June 07 1st November 07 1 - Student ID 1004 (1001 should not be taken to the top)
4 1st December 07 to 31 May 08 0 - no. They have all been included in the previous periods
could be done with a UNION ALL operation student_test-tables (and a join with the time table):
with
BaseData as)
Select * from student_test1
Union of all the
Select * from student_test2
Union of all the
Select * from student_test3
Union of all the
Select * from student_test4
)
,
min_values like)
Select student_id, min (exam_date) exam_date
of basedata
Student_id group
)
Select tp.id
tp.start_date
tp.end_date
count (mv.student_id) student_id
of time_period tp
left outer join
min_values mv
on (mv.exam_date between tp.start_date and tp.end_date)
Tp.id group
tp.start_date
tp.end_date
order of tp.id
;
I HAVE START_DA STUDENT_ID END_DATE
- -------- -------- ----------
01.12.05 0 31.05.06 2
1 01.06.06 30.11.06 1
2 01.12.06 31.05.07 0
3 01.06.07 30.11.07 1
4 01.12.07 31.05.08 0
Concerning
Martin
-
parents and children of the same table records
I want to create a query that is a union such that 2nd resultset is based on the game of results 1. I have a table that has parent and child records in the same table.
Table: EVENTS
EVENT_ID
PARENT_EVENT_ID
CREATED_DATE
(other columns)
If PARENT_EVENT_ID is null, then this is a parent record, otherwise it is a book of the child. I want to select all parent records then union them with all the related child records... something like this:
Select * from EVENTS where CREATED_DATE < sysdate - 90 and PARENT_EVENT_ID is null - all parents
Union
Select * event where PARENT_EVENT_ID in (select EVENT_ID of EVENTS where CREATED_DATE < sysdate - 90 and PARENT_EVENT_ID is null)-include all children selected from the top parent
It works but it of kind of ugly, I want to avoid using the subselect in the 2nd because it is a repeat of the 1st statement, is there a way to alias the first statement and only refer to him in the 2nd query?Hello
CONNECTION is very similar to a UNION on the same table. How is this:
SELECT * FROM events WHERE created_date < SYSDATE - 90 START WITH parent_event_id IS NULL CONNECT BY parent_event_id = PRIOR event_id AND LEVEL <= 2 -- Maybe ;
?
To answer your question: Yes, you can still keep the result set of a query and the Treaty as if it were a table or vew, like this:
WITH roots AS ( SELECT * FROM events WHERE created_date < SYSDATE - 90 AND parent_event_id IS NULL ) SELECT * FROM roots -- UNION ALL -- SELECT * FROM events WHERE created_date < SYSDATE - 90 AND parent_event_id IN ( SELECT event_id FROM roots ) ;
It's always a bit ugly, however. I would use CONNECT BY or from Oracle 11.2, a recursive WITH clause (which is explicitly a UNION), or, in any version, a self-join, like this:
SELECT c.* FROM events c LEFT OUTER JOIN events p ON p.event_id = c.parent_event_id WHERE p.parent_event_id IS NULL ;
I hope that answers your question.
If not, post a small example of data (CREATE TABLE and only relevant columns, INSERT statements) for all of the tables involved and the results desired from these data.
Report where the queriy above (the one you decide to use) is producingthe bad results and explain, using specific examples, how you get the right results from these data in these places.
Always say what version of Oracle you are using (for example, 11.2.0.2.0).
See the FAQ forum {message identifier: = 9360002}
Maybe you are looking for
-
How can I delete an email stuck in Outbox of my iPhone?
I recently tried to send a video shot on my iPhone to an iPhone email using Apple's Mail application 5. The email did not go out. It is stuck in my Outbox apparently now trying 4 months actually download the video. The thing just swirly swirling g
-
Stop for the BIOS password code
I get a prompt to enter an administrator password or power on password for a HP Pavilion dv7. After 3 failed attempts, I give myself a stop code: 53452842 Help?
-
How can I hide multiple instances of VI on the taskbar?
I am a newbie to LabVIEW OOP, during the process of debugging the project I tend to use in step function to test & see Subvi & this will lead to open several sub forums vi on the taskbar That's fine if the number of under vi is limited & fits into th
-
Just bought my first Acer - V3-731-4634
Trying to user manuals of load, but I do not have the serial # or snid # still, laptop arrives on Wednesday or Thursday Is it possible to load manuals & other info. until I have the serial No. / snid #? Your help will be appreciated.
-
ITunes Update Installation error message
After seeing an alert of an iTunes update, I repeatedly received the following error message after trying to install: "The feature you are trying to use is on a network unavailable resource. "Click OK to try again or enter a different path to the fol