How do I extract matching records from two tables?
I'm trying to extract client records from our client database to put together a very targeted email campaign. As an example, the result I want is a list of names and email addresses of those clients who have a record in our system, have not had a visit in our clinic in the last year, and live within 200 miles of our clinic. I can capture the first two criteria in one extract, and the second in another. I can then import those as tab delimited data into Numbers. What I then need to do is create a third table that represents ONLY those records that exist in both tables.
Can someone tell me if this is possible and if so, how to do it?
I'd be very appreciative of any help, thank you.
conejo61 wrote:
I can then import those as tab delimited data into Numbers. What I then need to do is create a third table that represents ONLY those records that exist in both tables.
You can create a column that generates a serial marker on the table from which you want to transfer the data, making all the names that also appear on the other table. Not that the formula will mark only exact matches.
Here's a short example:
Table 1 on the left, is one of the two tables imported from the tab delimited data files. It has other data, but only the names in column A are used to identify records appearing on both tables.
Table 2 on the right, contains the names and other data (represented by the email addresses in column B), to be transferred to the third table.
Column C of this table contains the formula that counts off the rows containing names appearing on both tables. The version below is in C2, and is filled down to the end of the column.
=IF(COUNTIF(Table 1 :: $B,A2)>0,MAX($C$1:C1)+1,"")
Note that jane Doe is not counted (or transferred) as her name is recorded differently on the two tables.
Table 3 is the results table. It contains one formula for each column to be transferred from Table 2 to Table 3.
A2: =IF((ROW()-1>MAX(Table 2 :: $C)),"",LOOKUP(ROW()-1,Table 2 :: $C,Table 2 :: $A))
B2: =IF((ROW()-1>MAX(Table 2 :: $C)),"",LOOKUP(ROW()-1,Table 2 :: $C,Table 2 :: $B))
Regards,
Barry
EDIT: I was working in Numbers '09 when developing this, and got the following warning when I saved a copy as an iWork '08 document:
Referencing row or column ranges that include header or footer cells isn't supported. The formula references were updated to exclude header and footer cells.
The formulas in Table 2::C2 references C1. You may have to ensure that Table 2 does not include a header row.
B.
EDIT2: A check on the file in Numbers '08 showed no apparent change to the formula, and editing Jane Doe's name on table 1 resulted in her name being added to Table 3.
Message was edited by: Barry
Similar Messages
-
I have two tables - table1 and table2 - that have the exact same schema. There are three fields that can be used to compare the data of the two tables, field1, field2, and field3. When there are matching rows in the two tables (table1.field1/2/3 = table2.field1/2/3)
I want to replace table1.field4 with table2.field4 and replace table1.field5 with table2.field5.
I have worked with the query but have come up with goobly goop. I would appreciate any help. Thanks.If your field1, field2, and field3 combinations in these tables are unique, you
can do a join on them.
Select t1.field4, t2.field4 , t1.field5, t2.field5
from table1 t1 inner join table2 t2 on t1.field1 =t2.field1 and t1.field2=t2.field2 AND t1.field3=t2.field3
--You can update your table1 with following code:
Merge table1 t1
using table2 t2 on
on t1.field1 =t2.field1 and t1.field2=t2.field2 AND t3.field3=t2.field3
When matched then
Update Set
t1.field4= t2.field4
,t1.field5 = t2.field5 ; -
How to delete the matching records from two internal tables
Hi ,
I have two internal tables say A and B of the same type. If A has 10 records and B has 4 records , I want to delete the 4 records in B from A .
loop at B into wa .
delete A where key = wa - key .
endloop.
takes a long time if the table B is huge. how can I improve the performance.
Thanks.
GayathriHi Gayathri,
You could try field-symbols. It reduces the data transfer from the internal table B to the work area.
field-symbols <fs_itab_b> like line of B.
loop at B assigning <fs_itab_b>.
delete A where key = <fs_itab_b>?-key.
endloop.
Regards,
<a href="https://www.sdn.sap.com:443http://www.sdn.sap.comhttp://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/com.sap.sdn.businesscard.sdnbusinesscard?u=zwcc%2fwm4ups%3d">anand Mandalika</a>. -
Matching records from two tables even when the ID of a person is null.
I have a MySQL named Natural Systems which is a magazine account that it member's will both sell handmade items wherein the proceeds will go into the Natural Systems account and each member also give a weekly donation. All of the proceeds will help toward the printing of the subsequent volumes. both of the selling of handmade items and the weekly dues happens during the same week or the same magazine volume number. I am trying to write a query that will pull both of the sales as well as the weekly dues payments weather they have paid or not.
SELECT foiid, trim(concat(name.fname,' ',name.lname)) AS no_ns, nspayamt, DATE_FORMAT(nspaydate, '%m/%d/%Y') FROM name LEFT JOIN nspay ON nspayfoiid = foiid and volume = '27' WHERE nspayfoiid is null AND type = 'Registered' AND city = 'richmond' AND status = 'a' group by foiid ORDER BY no_ns
This code will bring up the following names of those who did not pay weekly dues during volume 2:
Christopher Gabb
Michael banks
Timothy Hardin
However there is another table I wish to call the same Individuals who sold magazine during the same week of volume 27
SELECT trim(CONCAT(name.fname,' ',name.lname))AS Sold,
round(sum(fcnsales.salesamt)) as TOTAL_Mags_SOLD FROM name, fcnsales WHERE fcnsales.salesfoiid = name.foiid
AND fcnsales.salesvolume '27' GROUP BY name.foiid ORDER BY TOTAL_mags_SOLD desc;
This above code produces the following results:
Sold TOTAL_Mags_SOLD
Christopher Gabb 50
Michael banks 36
Timothy Hardin 12
I would like to combine the two queries to produce the following
Sold TOTAL_Mags_SOLD nspayamt
Christopher Gabb 50 none
Michael banks 36 none
Timothy Hardin 12 none
can anyone give me a direction to go to?If your field1, field2, and field3 combinations in these tables are unique, you
can do a join on them.
Select t1.field4, t2.field4 , t1.field5, t2.field5
from table1 t1 inner join table2 t2 on t1.field1 =t2.field1 and t1.field2=t2.field2 AND t1.field3=t2.field3
--You can update your table1 with following code:
Merge table1 t1
using table2 t2 on
on t1.field1 =t2.field1 and t1.field2=t2.field2 AND t3.field3=t2.field3
When matched then
Update Set
t1.field4= t2.field4
,t1.field5 = t2.field5 ; -
To find matching records across two tables
Hi folks,
I have a question regarding modifying the query. I am trying to compare the data between two tables, there are matching records in two tables, but are not in the same order. How to modify the query so that it finds the matching record between the two tables?
Here is my code
loop at itab_A.
read table itab_B with key
employeenumber = itab_zmgr_training-empid binary search.
if sy-subrc = 0.
Any thoughts?
Thanks,
Sk
Here I am getting the return value as '4'I have tried all that, but why is it that while reading a record from itab_B I see no record in the header line,
Let me paste the entire code of what I am trying to achieve.
data: itab_A like A occurs 0 with header line.
data: itab_B like B occurs 0 with header line.
some validation.....
loop at itab_A.
read table itab_B with key employee number = empid binary search.
if sy-subrc =0.
per form validation.
else.
perform validation.
endif.
endloop.
My observation: while reading recrod from itab_B the header line is clear, no record is read here hence sy-subrc = 0.
How can I correct this? Since it is not reading any record from itab_B no matching record is found.
Any thoughts would be really helpful.
Thanks,
Santhosh. -
Delete records from two tables
Hi All,
I have two tables (tableA and tableB) and i have four combination primary key.
How can I delete the records from tableA that are not in tableB?
In my sample below I need to delete the records from year 2013.
I have a loop to insert the new ones.
Thanks
Johnny
create table tableA(
keyA_id number,
keyB_id number,
keyC_id number,
keyD_id number,
amount number,
CONSTRAINT "PK_TABLEA" PRIMARY KEY (keyA_id, keyB_id, keyC_id, keyD_id)
insert into tableA(keyA_id,keyB_id,keyC_id,keyD_id,amount)
values(1,1,1,2011,10);
insert into tableA(keyA_id,keyB_id,keyC_id,keyD_id,amount)
values(1,1,1,2012,40);
insert into tableA(keyA_id,keyB_id,keyC_id,keyD_id,amount)
values(1,1,1,2013,20);
insert into tableA(keyA_id,keyB_id,keyC_id,keyD_id,amount)
values(1,1,2,2011,10);
insert into tableA(keyA_id,keyB_id,keyC_id,keyD_id,amount)
values(1,1,2,2012,30);
insert into tableA(keyA_id,keyB_id,keyC_id,keyD_id,amount)
values(1,1,2,2013,20);
create table tableB(
keyA_id number,
keyB_id number,
keyC_id number,
keyD_id number,
amount number,
CONSTRAINT "PK_TABLEB" PRIMARY KEY (keyA_id, keyB_id, keyC_id, keyD_id)
insert into tableB(keyA_id,keyB_id,keyC_id,keyD_id,amount)
values(1,1,1,2011,10);
insert into tableB(keyA_id,keyB_id,keyC_id,keyD_id,amount)
values(1,1,1,2012,40);
insert into tableB(keyA_id,keyB_id,keyC_id,keyD_id,amount)
values(1,1,1,2014,40);
insert into tableB(keyA_id,keyB_id,keyC_id,keyD_id,amount)
values(1,1,2,2011,10);
insert into tableB(keyA_id,keyB_id,keyC_id,keyD_id,amount)
values(1,1,2,2012,30);
insert into tableB(keyA_id,keyB_id,keyC_id,keyD_id,amount)
values(1,1,2,2014,30);r you trying to do something like below ?
Delete from TableA A
where not exists (select 1 from TableB B where
A.keyA_id=B.keyA_id and
A.keyB_id=B.keyB_id and
A.keyC_id=B.keyC_id and
A.keyD_id=B.keyD_id) -
Insert Matching Records from Lookup Table to Main Table
First off, I want to say many thanks for all the help that I've been provided on here with my other posts. I really feel as though my SQL knowledge is much better than it was even a few short weeks ago, largely in part to this forum.
I ran into a snag, which I'm hoping someone can provide me some guidance on. I have 2 tables an import table and a lookup table. What I need to have happen is anytime there are matches between the "Types" in the 2 tables, I need a single instance
of the "Type" and all corresponding fields from the lookup table appended to the import table. There will only be a single instance of each type in the "Lookup" table. Below is an example of how the data might look and the results that
I would need appended.
tblLookup
Type Name Address City
A Dummy1 DummyAddress No City
B Dummy2 DummyAddress No City
C Dummy3 DummyAddress No City
tblImport
Type Name Address City
A John Maple Miami
A Mary Main Chicago
A Ben Pacific Eugene
B Frank Dove Boston
Data that would be appended to tblImport
Type Name Address City
A Dummy1 DummyAddress No City
B Dummy2 DummyAddress No City
As you can see only a single instance will be inserted even though there may be multiple instances in the import table. This is the part that I'm struggling on. Any assistance would be appreciated.I'm not really sure how else to explain it. With my example, the join would be on "Type" As you can see, there are 2 matching records between the tables (A and B). I would need a single instance of A and B to be inserted into the import table.
Below is a SQL statement, which I guess is what you're asking for but it will not do what I need it to do. With the example that I have below, it would insert multiple instances of type "A" into the import table.
INSERT INTO tblImport (Type, Name, Address, City)
Select tblLookup.Type, tblLookup.Name,
tblLookup.Address, tblLookup.City)
From tblLookup
Join tblImport on tblLookup.Type = tblImport.Type -
Help with listing records from two tables
Hi: I have two tables joined by the first field. The field is primary key in first table. Need help listing records from both tables with each record one line/record.
create table EVENTS (
event_key varchar2(64) primary key,
event_description varchar2(64),
create_time int
create table EVENT_UPDATES (
event_key varchar2(64) NOT NULL ,
update_description varchar2(64),
update_time int
insert into EVENTS values('Event1', 'This is event1', 1);
insert into EVENT_UPDATES values('Event1', 'Ticket created', 3);
insert into EVENT_UPDATES values('Event1', 'Event cleared', 10);
insert into EVENTS values('Event2', 'This is event2', 4);
insert into EVENT_UPDATES values('Event2', 'Ticket created', 6);
insert into EVENT_UPDATES values('Event2', 'Event cleared', 8);I want to print each record in EVENTS table as one line and corresponding records in EVENT_UPDATES as one line/record like this
Event1 1 This is event1
3 Ticket created
10 Event cleared
Event2 4 This is event2
6 Ticket created
8 Event clearedTIA
Raviselect case weight
when 1 then event_key
end key,
time_val,
description
from (
select event_key,
create_time time_val,
event_description description,
1 weight
from events
union all
select event_key,
update_time,
update_description,
2 weight
from event_updates
order by event_key,
weight
KEY TIME_VAL DESCRIPTION
Event1 1 This is event1
3 Ticket created
10 Event cleared
Event2 4 This is event2
6 Ticket created
8 Event cleared
6 rows selected.
SQL> SY. -
How to delete some of records from wf_notifications table any API Name?
Hi All,
I want to delete some of records from wf_notifications table , can any one tell API' name and Back end delete process.
Thanks,
Ramu
Edited by: Ramu on Mar 20, 2013 5:42 AMHi ,
I hv done below script, now it's working fine.
DECLARE
CURSOR csr_transaction_id IS
SELECT hat.transaction_id,
hat.item_type,
hat.item_key,
ppx.employee_number,
hat.section_display_name
FROM hr_api_transactions hat, per_people_x ppx
WHERE hat.process_name = 'HR_PERSONAL_INFO_JSP_PRC'
AND hat.selected_person_id = ppx.person_id
AND ppx.employee_number IN
('100024773',
'100024820',
'100024859',
'100024879',
'100024902',
'100024937',
'100025137',
'100026470',
'610014755',
'610017039')
order by ppx.employee_number;
BEGIN
dbms_output.put_line('***Deleted all Transactions and Notifications of below Employee Personals Tranactions ***');
FOR my_cur_v IN csr_transaction_id
LOOP
/*Delete all Transaction_id's in hr_api_transactions,hr_api_transaction_steps,hr_api_transaction_values and hr_api_transaction_steps_bk tables */
hr_transaction_swi.delete_transaction
p_transaction_id =>my_cur_v.transaction_id,
p_validate => hr_api.g_false_num
wf_engine.abortprocess (
itemtype => my_cur_v.item_type,
itemkey => my_cur_v.item_key
/* Deleted all Notification_id's and item_key's in wf_item_activity_statuses,wf_items,wf_item_attribute_values,wf_notifications Table */
wf_purge.items (
itemtype => my_cur_v.item_type,
itemkey => my_cur_v.item_key
dbms_output.put_line('Emp No --'||my_cur_v.employee_number||'Transaction_id :'||my_cur_v.transaction_id||'Emp Personal Info :'||my_cur_v.section_display_name||
'Item Type :'||my_cur_v.item_type|| 'Item Key :'||my_cur_v.item_key);
END LOOP;
commit;
EXCEPTION
WHEN OTHERS THEN
dbms_output.put_line('hr_transaction_swi.delete_transaction api goest to exception block' ||sqlcode|| ' '||sqlerrm);
END;
/thanks,
Ramu -
Extracting unique records from two different tables record
Hello,
In the following code of two different tables www.testing.com exists in both tables. I want to compare two different columns of the two different tables to get unique records.
SQL> select unique(videoLinks) from saVideos where sa_id=21;
VIDEOLINKS
www.testing.com
SQL> ed
Wrote file afiedt.buf
1* select unique(picLinks) from saImages where sa_id=21
SQL> /
PICLINKS
test
test14
www.hello.com
www.testing.comThanks & best regardsUnfortunatly you didn't mention the expected output. I guess it would be the one line
"www.testing.com"
in that case simply join the two tables.
select *
from saVideos v
join saImages i on i.sa_id = v.sa_id and i.picLinks = v.videoLinks
where v.sa_id=21;If needed then you could change the select list to retrieve only distinct values.
select unique v.sa_id, v.videolinks
from saVideos v
join saImages i on i.sa_id = v.sa_id and i.picLinks = v.videoLinks
where v.sa_id=21;I usually avoid distinct/unique whereever possible. This requires the database to do a sort and makes the query slow.
Edited by: Sven W. on Feb 10, 2011 1:55 PM -
How to access records from two tables which have no relation
Hi,
I trying to generate a report where i need to print the company details at the top of the page and invoice details of that down the page.There is no relation between these two tables.I am not able to write two queries for one report.Pls some one assist me in getting this thing done.
Regards,
Tulacenath.Hi Tulacenath
So your invoice table does not have a reference to the customers (companys) that the invoices belong to?
Tim -
Records from two tables in different DB's
I have two tables (in different DB's) that I need to build
results from. I want to return results from all records in table A
and when a matching record in table B is found, retrieve the value
of a sqlbit field. The catch is that there may or may not be any
records in Table B that match Table A, and there may be more than
one record in Table B, but always only one record for Table A.
Table A is [Order] (yes I know, a reserved word - not my
design) and Table B is tblOrderFollowup - here is my current SQL
statement:
****************BEGIN SQL*************************
SELECT o.StoreID AS xStore
, o.Time AS dCreated
, o.ID AS xPurchase
, c.FirstName AS sFirstName
, c.LastName AS sLastName
, c.Company AS sCompany
, c.ID AS xCustomer
, o.Type AS xType
, o.Closed AS bClosed
, o.Tax AS nTax
, fu.OrderID AS xFUOrder
, fu.bComplete AS xFUComplete
FROM [Order] o
INNER JOIN Customer c
ON o.CustomerID = c.ID
AND o.StoreID = c.StoreID
LEFT OUTER JOIN db2.dbo.tblOrderFollowup fu
ON o.ID = fu.OrderID
AND o.StoreID = fu.StoreID
WHERE o.StoreID IN ( #allowableStoreIDs# )
AND o.StoreID = <cfqueryparam cfsqltype="cf_sql_integer"
value="#FORM.StoreID#" />
AND o.Time >= <cfqueryparam
cfsqltype="cf_sql_timestamp"
value="#CreateODBCDateTime(StartOfDay(FORM.dFrom))#" />
AND o.Time <= <cfqueryparam
cfsqltype="cf_sql_timestamp"
value="#CreateODBCDateTime(EndOfDay(FORM.dTo))#" />
AND o.Closed = <cfqueryparam cfsqltype="cf_sql_bit"
value="1" />
ORDER BY o.Time DESC
***********END SQL**********************
The problem here is that when I have more than 1 record per
OrderID/StoreID pair in Table B, I get more than one result - I
only want one. I am guessing I need a sub query, but don't know how
to go about doing that at all ...You say you want only one result. Do you know which one you
want?
By the way, your query is probably not giving you the right
answer. My experience is that if you do this:
select stuff
from table1 left join table2 on something
where table2.somefield = somevalue
you get the wrong answer. What I do instead is,
select stuff
from table1 left join
(select stuff from table2
where whatever) need_an_alias on somthing -
Can i select Records from two tables into an itab.
Hi,
Suppose that i have two tables spfli and sflight.
Now i want the records from both the tables into a single internal table.
If so please let me know how this can be done.
itab should consist of all the columns in sflight and spfli.
Regards,
SaiSai,
Yes u can do it using JOINs.
Use this link for ref
http://www.sap-img.com/abap/inner-joins.htm
INNER JOIN results are an intersection of the tables being joined where in only if both the tables havethe data the result is pused onto the result set.
WHERE as in LEFT OUTR JOIN you can push the data of the LEF T table on the resultset even when the join condition is not met.
The use is that you wantto have all the data that is there in left table and also the right table if the join condition is a success then right table fileds will have data else they are initial.
OUTJOIN's are used in MAINTENANNCE VIEWS, HELP VIEWS .
INNER JOINS are used DATABSE VIEWS.
Inner Join:-
SELECT pcarrid pconnid ffldate bbookid
INTO CORRESPONDING FIELDS OF TABLE itab
FROM ( ( spfli AS p
INNER JOIN sflight AS f ON pcarrid = fcarrid AND
pconnid = fconnid )
INNER JOIN sbook AS b ON bcarrid = fcarrid AND
bconnid = fconnid AND
bfldate = ffldate )
WHERE p~cityfrom = 'FRANKFURT' AND
p~cityto = 'NEW YORK' AND
fseatsmax > fseatsocc.
Left Outer Join
SELECT scarrid scarrname p~connid
INTO CORRESPONDING FIELDS OF TABLE itab
FROM scarr AS s
LEFT OUTER JOIN spfli AS p ON scarrid = pcarrid AND
p~cityfrom = 'FRANKFURT'.
Reward if helpful,
Karthik -
How to handle the failed records from the table when using DB Adapter
Hi,
I am reading some records from table using DB Adapter inside my synchronous BPEL process. Say like reading 100 records from table in between after successful reading of 90 records an error occured in 91st record due some various reasons(like DB down, Connection interrupted etc.). Then how to handle this situation, whether i have to read all the records from the begining and is there any option to continue from where it stopped reading.
Can please anybody help me out in the regard?
Thanks in advance
Regards,
Aejazwe had the same requirement some time ago and had two option:
1. ask the R/3 development team add a deletion indicator in the table (and thus not actually deleting the record). this deletion indicator could then be used like for any other standard datasource
this option was however refused, due to huge data volume after a while
2. at the end of the load we copied the ZTABLE1 to ZTABLE2. then in the begin of the load (day after) we compare the data of table1 to table2. entries available in table2 but not in table1 are deleted, and we put a 'D'. in deletion indicator; as we only keep the deleted entries for one day, the volume of the new table is acceptable.
M. -
Delete matching records from internal table
I have two internal tables.
say :
1)zsd80
2)i_vbrk
i want to delete records from i_vbrk which are not in zsd80.
this is my code. but it shows dump error after executing. please help.
LOOP AT i_zsd80 INTO zsd80.
LOOP AT i_konv INTO w_konv FROM w_index.
IF w_konv-kschl NE zsd80-kschl.
DELETE i_konv FROM w_konv.
ENDIF.
ENDLOOP.
ENDLOOP.Hi,
Do like this...
1. Loop at the where u want to delete..
2. store the sy-index...
3. read the other table....
4. if nothing found in read then delete that index...
LOOP AT i_vbrk.
TABIX = SY-TABIX.
READ TABLE zsd80 WITH KEY .......
IF SY-SUBRC NE 0.
DELETE IT_VBRK INDEX TABIX.
ENDIF.
ENDLOOP.
regards
Sukriti.....
Edited by: Sukriti Saha on Nov 3, 2008 1:19 PM
Maybe you are looking for
-
Stopping Quicktime opening MP3 files in IE8 on Vista
When I updated to iTunes version 9, Quicktime was also updated to version 7.6.4 Now, whenever I click on a link to an mp3 file in IE8, it opens in a new browser in Quicktime, and, as I do not have and do not want Quicktime pro, I am unable to save th
-
I'm a new iPhoto user and get an error message about insufficent disk space when I try to drag photos in from an inserted CD of .jpgs. New iMac... shouldn't get this message. Tried the different library approach outlined in some of your threads with
-
ISQL * Plus..Need help
Hello, I am very new to Oracle 9i. I have completed the installation and I am trying to access the ISQL*Plus interface but cannot locate it(don't know how). Can someone help and give me the steps? Thanks for your assistance.
-
Unable to deploy .EAR file to PI 7.1
/people/vijayakhanna.raman/blog/2007/06/08/cool-way-to-create-your-own-scas Note 696084 - EP 6.0: How to create SDA files for EPA or PAR files
-
While I tried to open I-photos on my mac it says " can't open application not supported by this mac"?