Can't compare a row with other rows within a table.. (many to many compare)
Hi all..
I am very new to PL/SQL..
I need to travers through a table for comparing it's rows with in the table with other rows. For this I am trying to use bellow Pl/sql.
create or replace compare()
Declare
VAR_HIT CHAR(1);
SEARCH_RECORD_DATA UDB.table1%ROWTYPE;
CANDIDATE_RECORD_DATA UDB.table1%ROWTYPE;
CURSOR SEARCH_RECORDS_CURSOR IS SELECT * FROM UDB.table1 order by registration_id;
CURSOR CANDIDATE_RECORDS_CURSOR IS SELECT * FROM UDB.table1 order by registration_id;
BEGIN
FOR SEARCH_RECORD_DATA IN SEARCH_RECORDS_CURSOR LOOP
FOR CANDIDATE_RECORD_DATA IN CANDIDATE_RECORDS_CURSOR LOOP
IF (CANDIDATE_RECORD_DATA.DECISION='P') THEN
VAR_HIT:='y';
IF(CANDIDATE_RECORD_DATA.FIRST_NAME!='unknown') AND (CANDIDATE_RECORD_DATA.FIRST_NAME!=SEARCH_RECORD_DATA.FIRST_NAME) THEN
VAR_HIT:='n';
ELSIF(CANDIDATE_RECORD_DATA.LAST_NAME!='unknown') AND (CANDIDATE_RECORD_DATA.LAST_NAME!=SEARCH_RECORD_DATA.LAST_NAME) THEN
VAR_HIT:='n';
ELSIF(CANDIDATE_RECORD_DATA.BIRTH_DATE!='unknown') AND (CANDIDATE_RECORD_DATA.BIRTH_DATE!=SEARCH_RECORD_DATA.BIRTH_DATE) THEN
VAR_HIT:='n';
ELSIF(CANDIDATE_RECORD_DATA.GENDER!='U') AND (CANDIDATE_RECORD_DATA.GENDER!=SEARCH_RECORD_DATA.GENDER) THEN
VAR_HIT:='n';
ELSIF(CANDIDATE_RECORD_DATA.FATHER_NAME!='unknown') AND (CANDIDATE_RECORD_DATA.FATHER_NAME!=SEARCH_RECORD_DATA.FATHER_NAME) THEN
VAR_HIT:='n';
ELSIF (CANDIDATE_RECORD_DATA.MOTHER_NAME!='unknown') AND (CANDIDATE_RECORD_DATA.MOTHER_NAME!=SEARCH_RECORD_DATA.MOTHER_NAME) THEN
VAR_HIT:='n';
END IF;
IF(VAR_HIT='y') THEN
INSERT INTO UDB.BIO_DI_HIT_RESULT(REGISTRATION_ID,SEARCH_ID,HIT_CANDIDATE_ID,SEARCH_DETAILS,CANDIDATE_DETAILS) VALUES (SEARCH_RECORD_DATA.REGISTRATION_ID,SEARCH_RECORD_DATA.EGM_NO,CANDIDATE_RECORD_DATA.EGM_NO,VAR_SEARCH_DETAILS,VAR_CANDIDATE_DETAILS);
UPDATE UDB.BIO_RECORDS_DEMOGRAPHICS SET DECISION='D';
END IF;
END IF;
END LOOP;
commit;
END LOOP;
END;
Outer loop is working fine (it is raversing throughout the table) (say for 8000 records 8000 times)
But Enner loop is not working fine e.i. it's running just for 8000 times for 8000 records. while it should run more then 8000 time..
Can any one help me..
Is the way of using two cursor on one table for comparing each row of record is correct????????? :(
Finally I come up with this:
INSERT INTO UDB.BIO_DI_HIT_RESULT(REGISTRATION_ID,SEARCH_ID,HIT_CANDIDATE_ID,SEARCH_DETAILS,CANDIDATE_DETAILS)
SELECT SEARCH.REGISTRATION_ID, SEARCH.Key_NO,CANDIDATE.Key_NO,
'First name='||SEARCH.FIRST_NAME||',Last name='||SEARCH.LAST_NAME||',Birth date='||SEARCH.BIRTH_DATE||',Gender='||SEARCH.GENDER||',Fathers name='||SEARCH.FATHER_NAME||',Mother name='||SEARCH.MOTHER_NAME,
'First name='||CANDIDATE.FIRST_NAME||',Last name='||CANDIDATE.LAST_NAME||',Birth date='||CANDIDATE.BIRTH_DATE||',Gender='||CANDIDATE.GENDER||',Fathers name='||CANDIDATE.FATHER_NAME||',Mother name='||CANDIDATE.MOTHER_NAME
FROM UDB.BIO_RECORDS_DEMOGRAPHICS SEARCH,UDB.BIO_RECORDS_DEMOGRAPHICS CANDIDATE
WHERE (SEARCH.FIRST_NAME!='unknown' OR SEARCH.LAST_NAME!='unknown' OR SEARCH.BIRTH_DATE!='unknown' OR SEARCH.GENDER!='unknown' OR SEARCH.FATHER_NAME!='unknown' OR SEARCH.MOTHER_NAME!='unknown')
AND SEARCH.REGISTRATION_ID != CANDIDATE.REGISTRATION_ID
AND SEARCH.REGISTRATION_ID < CANDIDATE.REGISTRATION_ID
AND (SEARCH.FIRST_NAME='unknown' OR CANDIDATE.FIRST_NAME IN (SEARCH.FIRST_NAME,'unknown'))
AND (SEARCH.LAST_NAME='unknown' OR CANDIDATE.LAST_NAME IN (SEARCH.LAST_NAME,'unknown'))
AND (SEARCH.BIRTH_DATE='unknown' OR CANDIDATE.BIRTH_DATE IN (SEARCH.BIRTH_DATE,'unknown'))
AND (SEARCH.GENDER='U' OR CANDIDATE.GENDER IN (SEARCH.GENDER,'U'))
AND (SEARCH.BIRTH_DATE='unknown' OR CANDIDATE.BIRTH_DATE IN (SEARCH.BIRTH_DATE,'unknown'))
AND (SEARCH.FATHER_NAME='unknown'OR CANDIDATE.FATHER_NAME IN (SEARCH.FATHER_NAME,'unknown'))
AND (SEARCH.MOTHER_NAME='unknown' OR CANDIDATE.MOTHER_NAME IN (CANDIDATE.MOTHER_NAME,'unknown'))
ORDER BY SEARCH.REGISTRATION_ID;
Now it realy meet all my final table requirement.. I am happy.. :)
But again last requirement is like..
In the final BIO_DI_HIT_RESULT table I need to add one more column named as match_count
This column will have values of total no. of exact match field (not unknown) for each records in the BIO_DI_HIT_RESULT.....
One way of duing this is--
Step1: I will use a cursor for traversing through each of the rows in BIO_DI_HIT_RESULT.
Step 2: And based on count of matches in search_details(substring) and candiate_details(substring) I update each and every rows of the table.
It will be very slow.. And for records like 2 million records.. I can't think of this idea. :(
Sooooo,, Is there ANY BETTER WAY OF DOING THIS AS WELL.. BETTER MEANS FAST WAY.. :(
Thanks in advance....
Edited by: user13367608 on Jan 8, 2011 2:13 AM
Similar Messages
-
How to join each row with other in the same table without repeating, please help
Hi,
I have a table say Adjustment having following data
Emp_Id
Adjustment_id
Date
1000101
1000300
2014-02-12 00:00:00.000
1000101
1000301
2014-02-12 00:00:00.000
1000101
1000302
2014-02-12 00:00:00.000
1000101
1000303
2014-02-12 00:00:00.000
1000102
1000302
2014-02-12 00:00:00.000
1000102
1000303
2014-02-12 00:00:00.000
1000102
1000304
2014-02-12 00:00:00.000
And I want following records:
Emp_Id
Adjustment_id Adjustment_id1
Date
1000101
1000300 1000301
2014-02-12 00:00:00.000
1000101
1000300 1000302
2014-02-12 00:00:00.000
1000101
1000300
1000303
2014-02-12 00:00:00.000
1000101
1000301 1000302
2014-02-12 00:00:00.000
1000101
1000301 1000303
2014-02-12 00:00:00.000
1000101
1000302 1000303
2014-02-12 00:00:00.000
1000102
1000302 1000303
2014-02-12 00:00:00.000
1000102
1000302 1000304
2014-02-12 00:00:00.000
1000102
1000303
1000304
2014-02-12 00:00:00.000Hi Uri,
I am using SQL Server 2008, and I am trying join each adjustment_id with all other adjustment_id having same Date and Emp_id.
so for following table records:
Emp_Id
Adjustment_id
Date
1000102
1000302
2014-02-12 00:00:00
1000102
1000303
2014-02-12 00:00:00
1000102
1000304
2014-02-12 00:00:00
Output should be:
Emp_Id
Adjustment_id Adjustment_id
Date
1000102
1000302 1000303
2014-02-12 00:00:00 (first row with 2nd)
1000102
1000302 1000304
2014-02-12 00:00:00 (first row with 3rd)
1000102
1000303 1000304
2014-02-12 00:00:00 (2nd row with 3rd) -
HT4059 CAN YOU SHARE IBOOK BOOKS WITH OTHER IBOOK USERS?
Does anyone know if you can share books in ibook with other ibook users?
No, not unless you give them access to your iTunes Store account (which would probably be a violation of the Store terms of use).
Regards. -
How can i connect iphone 4 with other mobile through bluetooth
how can i connect iphone 4 with other mobile through bluetooth
You can't. iPhone does not have this feature.
Some iOS apps will allow you to connect to other iOS devices for peer-to-peer games, or sending photos, but not to other makes of phone. -
Hello, wonder if I can edit movies in iMovie with other cameras. For example: A camera of another brand, passing it to my PC and I could play in iPadm doing editing videos? thank you
Not sure what you mean. You can import video into I Movie either via your USB slot if it is already in digital format or via your fire wire slot (you may need an adapter as the MAC slot is a mini slot, the adapters are available on line) if you are coming from tape based media.
-
How can I share my music with other account users
Hello I would like to know how I can share my Itunes library with other user accounts on my mackbook?
ThanksHelp here > iTunes: How to share music between different accounts on a single computer
-
If I buy a Tmobile contract free iPhone 6 in apple store, does that mean it is locked and can only use Tmobile network? Can it be used internationally with other carriers? I travel a lot and do not want to find out my phone does not work the moment I insert a local carrier sim card.
"The iPhone 6 will come unlocked as long as customers pay full price," a T-Mobile representative said. "If customers choose to take advantage of T-Mobile’s Equipment Installment Plan it will be locked until the device is fully paid off. This policy applies at both T-Mobile and Apple stores."
http://www.ibtimes.com/how-pre-order-iphone-6-when-where-purchase-apples-smartph one-1686108 -
Match values of 2 columns with other rows
I want to compare the values of 2 columns with all rows in the sheet.
If there is a match I want to place a character in the next column, so I can filter on it
But I need to keep one row for each unique value. (match of 2 columns)Hi,
I'm marking the reply as answer as there has been no update for a couple of days.
If you come back to find it doesn't work for you, please reply to us and unmark the answer.
Thanks
George Zhao
Forum Support
Come back and mark the replies as answers if they help and unmark them if they provide no help.
If you have any feedback on our support, please click "[email protected]" -
How can i create a grid with summary row
Hello Professionals,
I'm wondering how could i create a grid like the grid below, i want to create a grid with summary row,
i have tried to create it using collapsing but it didn't work as required.
Any suggestions?, i want to know just the starting point so i can make deep investigations.
Thanks in Advance,Hi Karem,
this can be achieved by just assigning a datatable containing the data plus some formatting of grid. Meaning there is no feature for that.
The datatable can be filled manually or by sql query. Then you have to attach some events for updating the values ( validate after for gid item ).
A small example for a sql query showing last month quotations and orders with summary :
select 1 as Sort,cast(DocNum as varchar) as DocNum,DocTotal,convert(varchar, DocDate,104) from OQUT where DocDate between DATEADD(month, -1, GETDATE()) AND GETDATE()
UNION ALL
Select 2 as Sort,'Summary ( Quotation ) : ',sum(DocTotal), convert(varchar, DATEADD(month, -1, GETDATE()),104)+' - '+convert(varchar, GETDATE(),104) from OQUT where DocDate between DATEADD(month, -1, GETDATE()) AND GETDATE()
UNION ALL
select 3 as Sort,cast(DocNum as varchar) as DocNum,DocTotal,convert(varchar, DocDate,104) from ORDR where DocDate between DATEADD(month, -1, GETDATE()) AND GETDATE()
UNION ALL
Select 4 as Sort,'Summary ( Order ) : ',sum(DocTotal), convert(varchar, DATEADD(month, -1, GETDATE()),104)+' - '+convert(varchar, GETDATE(),104) from ORDR where DocDate between DATEADD(month, -1, GETDATE()) AND GETDATE()
ORDER by Sort
regards,
Maik -
Display a column as row with same row names.
Hi,
My belwo query displaying o/p in this format.I need to conver a row with appriate colmn value (see the required o/p).
could some help.
current o/p
TEST_SESSION_ID ROSTER_DOMAIN_NAME TO_CHAR(A.SCORE) SEM
4087 AR - Integers and Fractions 5.148757270620481 1
4087 AR - Decimal Numbers 5.5597596042656825 2required o/p format
TEST_SESSION_ID AR - Integers and Fractions AR - Decimal Numbers sem-AR - sem
Integers and Fractions AR - Decimal Numbers
4087 5.148757270620481 5.5597596042656825 1 2ROSTER_DOMAIN_NAME are not staticks.I tcould be increase .some times it could have 20 names.Since i can t use decode or case with statically .can some one help on this.
query
SELECT a.test_session_id,c.roster_domain_name,to_char(a.score),a.sem FROM
test_session_detail a,test_session b ,test_detail c
WHERE a.test_session_id = b.test_session_id
AND c.test_detail_id = a.test_detail_id
AND exists (
select td.test_detail_id
from test_detail td
where domain_map_id is not null
and c.test_detail_id =td.test_detail_id
and domain_map_id in('303','306','305','304'))
AND b.test_session_id in ('4087')
AND domain_group_id NOT IN (
SELECT domain_group_id
FROM test_session_detail a,test_session b
WHERE a.test_session_id = b.test_session_id
AND a.domain_group_id is not null
AND b.test_session_status_id = 4
AND a.test_session_status_id <> 3
AND b.test_session_id IN ('4087'))Ok, the problem here is that you want a dynamic pivot of data and you also want to use some of the data as the column names.
SQL requires that the columns to be retrieved are known at query parse time, i.e. before any data is fetched, so it's not easily possible to have dynamic column names done in a single query.
The general steps in executing a query are...
1. Submit SQL query (Open Query)
2. SQL Query Parsed
3. SQL Query Executed
4. Row Fetched
5. Repeat step 4 until no more rows
6. Close QuerySo, as you can see no data is fetched until step 4, but, at Step 2, the SQL engine needs to have parsed the query and already know what columns and data types are going to be returned.
An example of how something similar can be achieved is given on this thread:
How to pipeline a function with a dynamic number of columns?
But that may be a little too complex to implement.
What you are really wanting is something at the front end reporting side of things.
Ideally, this is the task of a reporting tool, not of the SQL engine which is retrieving the data. -
Does pivotTable support rows with duplicated row names?
I am developing an application using JDeveloper 11.1.1.6.0.
Pivot table can display attribute values as row or column names. If the "row name" attribute for two records has the same/duplicated values, does pivot table show the two records in two rows with the same row name?
My experiment shows only one row with blank value for "measure" in such case. Just want to confirm this is the expected behavior.Hi,
don't quite understand the use case so answering in general: no data should be lost when using Pivot table
Frank -
Hi All,
I am using NWDS 2004
I want to have a table with multiple rows in a row.
Can any one help ?
Thankshi Akhilesh,
Use tree table for your requirement.
[Tree Table Tutorial|https://www.sdn.sap.com/irj/scn/go/portal/prtroot/docs/library/uuid/de59f7c2-0401-0010-f08d-8659cef543ce]
Hope this helps!
Monalisa -
Single Update command to update multiple rows with multiple rows
Hi Gurus!
Can I update table A with a result set of table b (multiple rows returns) where a.c1 = b.c1
regards,
SHAs Joel mentioned this update will update all rows in a table based on another one:
update tableA set seq_no = (select seqno from tableB where tableA.ID = tableB.ID)
Also, any rows that don't match the select criteria will get updated to null. This could be very bad depending on what you want to do.
To update a subset of tableA with data from tableB:
update tableA set seq_no = (select seqno from tableB where tableA.ID = tableB.ID)
where exists (select 1 from tableB where tableA.ID = tableB.ID) -
Can cross join be used with other joins
hi,
Q1) is this syntactically correct , cross join with other joins like inner outer. like following.
select * from t1 cross join t2
inner join t3 on cast(t3.c1 as varchar) = (cast(t1.id as varchar) + cast(t2.t2 as varchar))
I have seen it works but wanted to know is it syntactically correct.
yours sincerelyHi
First, Sorry, I wanted to VOTE (as I wrote) and not to Propose as answer :-)
Join are not necessarily executing in the order we write them. I can show simple example with tables that include the amount of rows is very different.
/**************************************************** DDL - Create tables */
CREATE TABLE T1 (
ID INT IDENTITY CONSTRAINT PK_T1 PRIMARY KEY,
MyValue UNIQUEIDENTIFIER DEFAULT NEWID()
CREATE TABLE T2 (
ID INT IDENTITY CONSTRAINT PK_T2 PRIMARY KEY,
MyValue UNIQUEIDENTIFIER DEFAULT NEWID()
CREATE TABLE T3 (
ID INT IDENTITY CONSTRAINT PK_T3 PRIMARY KEY,
MyValue UNIQUEIDENTIFIER DEFAULT NEWID()
GO
/**************************************************** DML - Populate Tables*/
SET NOCOUNT ON;
insert T1 (MyValue)
select top 100 null
from _ArielyAccessoriesDB.dbo.ArielyNumbers
GO
insert T2 (MyValue)
select top 500 null
from _ArielyAccessoriesDB.dbo.ArielyNumbers
GO
insert T3 (MyValue)
select top 10000 null
from _ArielyAccessoriesDB.dbo.ArielyNumbers
GO
/**************************************************** Play Time */
select T1.MyValue, T2.MyValue, T3.MyValue
from T3
join T2 on T2.ID = T3.ID
join T1 on T1.ID = T2.ID
GO
-- Check Execution Plan [EP]
-- Notice that SQL Server has changed the join order from T3-T2-T1 to T1-T2-T3 because it’s better that way.
-- You can notice that the order was by the number of rowns in tables from the smallest to the bigest SET
hope this is helpful :-)
[Personal Site] [Blog] [Facebook] -
Interchange values from one row with another row
Dear Oracle Guru's
While Migrating data from legacy system, there was a confusion and data in two columns got jumbled
Ex The data should be like this
Custcode leaseno
1034 A234
1035 A235
1036 A236
whereas the table has data like this
Custcode leaseno
1034 A235
1035 A234
1036 A237
1037 A236
How do we swap values between two rows
Kindly guide me on this
With Warm Regards
ssr>
Ex The data should be like this
Custcode leaseno
1034 A234
1035 A235
1036 A236
whereas the table has data like this
Custcode leaseno
1034 A235
1035 A234
1036 A237
1037 A236
How do we swap values between two rows And now, time for yet another guess... ;)
My guess is that your migration program interchanged "leaseno" value in pairs from the top - the top being the least value of "custcode".
So, the "leaseno" values of Row 1 and Row 2 were swapped.
The "leaseno" values of Row 3 and Row 4 were swapped.
And so on.
Which also means that if the total number of rows in your table is even, then the swap would be "complete". Otherwise, the last row would be left out. I don't know what you want to do with that.
test@XE>
test@XE> select * from t;
CUSTCODE LEAS
1034 A235
1035 A234 <= you want to swap A235 and A234
1036 A237
1037 A236 <= you want to swap A237 and A236
1038 A238 <= let's leave it hanging there...
5 rows selected.
test@XE>
test@XE> And of course, swapping from "top to bottom" means swapping from the least to highest value of "custcode".
Here's the SELECT statement for that -
test@XE>
test@XE> -- show the records in the table t
test@XE>
test@XE> select * from t;
CUSTCODE LEASENO
1034 A235
1035 A234
1036 A237
1037 A236
1038 A238
5 rows selected.
test@XE>
test@XE> -- the SELECT statement for swapping leaseno values
test@XE>
test@XE> @test7a
test@XE> --
test@XE> select custcode,
2 leaseno,
3 case
4 when mod(row_number() over (order by custcode),2) = 1 and
5 lead(leaseno) over (order by custcode) is null
6 then leaseno
7 when mod(row_number() over (order by custcode),2) = 1
8 then lead(leaseno) over (order by custcode)
9 else lag(leaseno) over (order by custcode)
10 end as new_leaseno
11 from t;
CUSTCODE LEASENO NEW_LEASENO
1034 A235 A234
1035 A234 A235
1036 A237 A236
1037 A236 A237
1038 A238 A238
5 rows selected.
test@XE>
test@XE> And here's the update statement -
test@XE>
test@XE> -- the UPDATE statement for updating leaseno values in pairs from the top
test@XE>
test@XE> @test7b
test@XE> --
test@XE> update t t1
2 set t1.leaseno = (
3 select new_leaseno
4 from (
5 select custcode,
6 leaseno,
7 case
8 when mod(row_number() over (order by custcode),2) = 1 and
9 lead(leaseno) over (order by custcode) is null
10 then leaseno
11 when mod(row_number() over (order by custcode),2) = 1
12 then lead(leaseno) over (order by custcode)
13 else lag(leaseno) over (order by custcode)
14 end as new_leaseno
15 from t
16 ) t2
17 where t2.custcode = t1.custcode
18 );
5 rows updated.
test@XE>
test@XE>
test@XE> select * from t;
CUSTCODE LEASENO
1034 A234
1035 A235
1036 A236
1037 A237
1038 A238
5 rows selected.
test@XE>
test@XE> HTH,
isotope
Maybe you are looking for
-
While running Firefox, I got a prompt saying their was an update. I did that, but then got another message saying that it was recommended I update firefox to 4.0. I downloaded it, but never received a prompt to drag the image into my applications fol
-
Using genres as Movie sub folders?
Is it possible to set up the "automatically add to itunes" watch folder to treat movies similar to tv shows / music? So for example, when you copy over an episode of a tv show (and it is properly tagged) to the automatically add to itunes, itunes bre
-
Bordering Rows in a table using CSS
HI, I'm using Dreamweaver CS3. I have a table of 2 columns and 5 rows on 1 particular page. What I would like to do is put a thin border at the bottom of each row. What I am doing is highlighting the first row by choosing the TR tag and starting to c
-
My iTunes Radio is not showing the bottom of my Music App on my iPhone. How do I get it back?
-
What happened to the book menu on the older ipods?
I bought my Dad an ipod nano to listen to books on. It used to have a menu for books so he could find what he wanted to listen to, but now the only way to get there is through search.... try explaining that to an elderly man over the phone. I told