How to match columns from two tables with
Hello:I have two tables as below:
Table1::(Base Table)
Country | Prefix | Prefix_Length
Travel | 001 | 3
CountryB. 0012 | 4
PaysC | 00443 | 5
CountryD | 0091 | 4
:(Detail Table) table2
The population | Area | Prefix
500 | AreaA | 0015921
1000 | AreaB | 00122
400. AreaC. 00443743
300. ALIS | 0091333
100. AreaA | 001
I need to match these two tables with prefix columns (whose length is not fixed in the two tables: but it starts with 00 in the two tables). Two different countries the prefix may be similar up to a certain length. Thus, Prefix_Length can be used to determine (exactly) how much time should be taken in the search of Table2.
Output:
Country | Prefix | Area | Population
Travel | 001 | AreaA | 600
CountryB. 0012 | AreaB | 1000
PaysC | 00443 | AreaC. 400
CountryD | 0091 | ALIS | 300
Please help me with your valuable comments.
-Tender
Try this
with base_table as (
select 'CountryA' country,'001' prefix,3 prefix_length from dual union all
select 'CountryB','0012',4 from dual union all
select 'CountryC','00443',5 from dual union all
select 'CountryD','0091',4 from dual
),
detail_table as (
select 10 no_of_call,'0015921' prefix from dual union all
select 3,'00122' from dual union all
select 50,'00443743' from dual union all
select 50,'00443643' from dual union all
select 300,'0091333' from dual union all
select 60,'001' from dual
)
SELECT country,
prefix,sum(no_of_call)
FROM (
select country,
b.prefix,no_of_call,
decode(no_of_call,lead(no_of_call,1,0) over(partition by no_of_call order by b.prefix,no_of_call),'y','n') y_or_no
from base_table b,
detail_table d
where b.prefix = substr(d.prefix,1,prefix_length))
where y_or_no !='y'
group by country,
prefix
order by country,
prefix;
Published by: Vi on 20 February 2012 01:07
Tags: Database
Similar Questions
-
How to export data from the table with the colouring of cells according to value.
Hi all
I use jdeveloper 11.1.1.6
I want to export data from the table with a lot of formatting. as for color cells based on value and so much. How to do this?
You can find us apache POI-http://poi.apache.org/
See this http://www.techartifact.com/blogs/2013/08/generate-excel-file-in-oracle-adf-using-apache-poi.html
-
Several lines to a format of columns from two tables
Oracle Database 11 g Enterprise Edition Release 11.1.0.7.0 - 64 bit Production
The difficulty to return several lines simple lines/columns in two tables.
Table 1:
ID BOOK_NBR
1 1001
1 2001
2 1010
3 1020
Table 2:
AUTHOR BOOK_NBR
1001 JOHN_1
2001 JOHN_2
MARY 1010
1020 JUNE
Desired output:
ID BOOK_NBR AUTHOR BOOK_NBR AUTHOR
1 1001 JOHN_1 2001 JOHN_2
2 MARY 1010
3-1020 JUNE
There are an unknown number of BOOK_NBR to one ID.
Any guidance would be appreciated.
-
Delete rows in a table when the columns from two tables match
Hello
I have following two tables.
===========================================
create the table empbooth as
(
Select 1 empid, 1 double cabin Union all the
Select option 2, Union 1 double all the
Select 3, Union 1 double all the
Select option 4, Union 2 double all the
Select option 5, 2 double
);
create the table attsht as
(
Select 1 empid, 240 reg, 0 unpaid all double union
Select option 2, reg 200, 0 unpaid of all the double union
Select 3, 240 reg, 0 unpaid all double them union
Select 4 480 reg, 0 unpaid all double union
Select 5 240 reg, unpaid double 0
);
=================================================
I want to remove rows from attsht where corresponding booth (which is stored in the empbooth table) is 1.
The condition is 'where attsht.empid = empbooth.empid and empbooth.booth = 1 '.
I use oracle 10g.
Help, please
delete from attsht where a.empid in (select b.empid from empbooth b where b.booth = 1)
or
remove from attsht a
where exists (select null
of empbooth b
where b.booth = 1
and b.empid = a.empid)
-
Find unmatched columns from two tables
Hello guys,.
I am trying to compare two tables based on a key of the indexed columns.
Here is an example of what I'm trying to achieve:
Table A: A_ID A_Name A_Income A_housecount
Jess 11 9 2
Rauny 4 6 22
Table b: B_ID B_Income B_housecount
11-5-2
22 6 4
In the example above data columns A_ID and B_Id are same in both tables. I'm trying to compare A_Income with B_Income & & A_housecount with B_housecount. The results of this series of samples would be:
11-9-2
11-5-2
as income in the same id is different.
As I have large amount of data in the two tables can you please give me some ideas on how I can get my results? Let me know if the explanation is not clear for you.
Thanks in advanceselect hshold_id,hsold_income,hshold_size, 'abc.' || case count(src1) when 0 then 'demo' else 'hshold' end the_source from (select hshold_id,hsold_income,hshold_size, 1 src1, to_number(null) src2 from abc.hshold union all select hshold_id,hsold_income,hshold_size, to_number(null) src1, 2 src2 from abc.demo ) group by hshold_id,hsold_income,hshold_size having count(src1) != count(src2)
Concerning
Etbin
Edited by: Etbin on 11.2.2011 18:00
correction of alignment -
How to remove columns from the table on the master 1-0?
I have an array of 96 columns with strings. I also have the array of int 96-elemets (mask) with 1 and 0.
What I want to do is to is to remove (or hide - but I read that it is not possible) all the columns with index corresponding to 0 in the mask table.
example:
columns in the table
1 2 3 4 5
mask
0 1 0 0 1
I want to remove the columns 1, 3 and 4 and leave only 2 and 5 in my table.
How can I do?
If I create loop for with i as the index of the column, when I do DeleteTableColumns() columns number decreases, and I get an error of range out of
Or do I have an option to hide the unnecessary columns (not set their width to 1, it's very ugly-looking)?
Please help me (())
Hello rovnyart!
1. removal of columns in the table:
I suspect that the reason why you get the out-of-range error is due to fact that in your loop, you delete the columns in the table, you'll eventually end up by referring to a column that no longer exists, because the other columns before it have been deleted. While you remove each column of your table in the loop for example, the column index number will move, because you deleted the other columns in front of her.
To resolve this, even if you delete a column in your loop, make sure that you take also into account that the index of the column is moved because of the removed columns.
2 hide columns in table:
You can use the ATTR_COLUMN_VISIBLE attribute to hide columns in the table:
http://zone.NI.com/reference/en-XX/help/370051Y-01/CVI/uiref/cviattrcolumnvisible_column/
3 alternatives:
Note that another alternative would also use a tree instead, control as the tree control also supports the hidable columns:
http://forums.NI.com/T5/LabWindows-CVI/table-hide-column/TD-p/569773
Best regards!
-Johannes
-
How to delete columns from a table
Hello
I have a 2D chart composed of 12 columns. I want to just use the first three columns within LabView. What is the best way to extract the data from these three columns?
Greetings
Kristoffer
You can watch from two different angles:
- You can remove the extra columns
- You can keep only the fist three columns
Would be easier to use "subset of the table" and select the first three columns. See photo.
(This assumes you want the three columns as a 2D array. If you want in three tables 1 d, you would use 'index array' resized to three exits.)
-
How to copy columns from one table to another
Hai All
How to copy the contents of the array to another
For example, first name of the table is T1 and I have 10 columns and data are here and I need to create a table T2
But I don't need the header, but not the values of all the 10 columns of T1 to T2, but not values
Thanks and greetings
Srikkanth.MWhy not?
create table test1 as select * from emp where 1 = 2;
Kind regards.
LOULOU.
-
How to merge data from the table with a single line
Hello
I have three tables subscription_type, the address and the person. Here are the details of the table
Person Subscription_type Address
Person_Id AdressType_id Address_id
Person_name Description Address_type_id
Person_id
Address details
There are three types of different address - home, postal and previous.
Each person can have these three different addresses.
I want to create a view that displays all addresses of three of each person in the table of the person in a single line.
Any help please
In your example, there are 2 rows of columns street1, TOWN, SUBURB etc for a single person. To convert this into a single line, with the new columns we could simply use CASES or DECODE in the select as the SQL below. To understand why consolidation function THAT MAX is used - remove the MAX and GROUP BY in SQL keyword and try.
Select full_name
, max (case when description = "Home" then end street1) Home_Street
, max (case when description = "Home" then end suburb) home_suburb
, max (case when description = end of the "Home" then City) home_city
, max (case when description = "Home" then postal code end) home_postcode
, max (case when description = "Home" then end state_name) home_state
, max (case when description = 'Postal' then end street1) Postal_Street
, max (case when description = 'Postal' and then end of suburb) Postal_suburb
, max (case when description = 'Postal' then the city) Postal_city
, max (case when description = 'Postal' and then end of CP) Postal_postcode
, max (case when description = 'Postal' then state_name end) Postal_state
, max (case when description = "Back" then end street1) Prev_Street
, max (case when description = "Back" then end suburb) Prev_suburb
, max (case when description = "Back" then city end) Prev_city
, max (case when description = "Back" then postal code end) Prev_postcode
address a
S State
No p
Subscription_type att
where a.person_id = p.person_id
and a.state_id = s.state_id
and a.address_type_id = att.address_type_id
Full_name group
-
To find common data in 2 columns from two different tables.
Hello
Could someone help me with a querry to discover common data of 2 columns from two different tables?
Thank you
RajeshTry as below.
select col1 ,col2 from tab1 intersect select col1 ,col2 from tab2;
-
How to remove a column from a table in a dell identtiymananger 7.0
How to remove a column from a table in a dell identtiymananger 7.0
Hello
In Version 7 of the removal of a single custom column can be accomplished by a stored procedure, available in the database called: QBM_PColumnDrop
Hope that helps.
Cordially Fatih
-
histogram with just a few columns from a table
I want to get a histogram of a few columns from a table (4 - 6-... 22)
-
Update multiple columns from multiple tables in a single UPDATE request
Hello
I'm trying to figure if I'm heading in the right direction.
I want to update multiple columns from multiple tables in a single UPDATE request. Also, I would like to update multiple columns in a table from the tables.
Scenario 1
UPDATE Table2, Table 3 SET T2.Column1 = T1.Column1 ,T2.Column2 = T1.Column2 ,T3.Column2 = T1.Column2 FROM Table1 T1, Table2 T2, Table3 T3 WHERE T1.id = T2.id and T1.id = T3.id
Scenario 2
UPDATE Table3 SET T3.Column1 = T1.Column1 T3.Column2 = T1.Column2 ,T3.Column3 = T2.Column3 ,T3.Column4 = T2.Column4 FROM Table1 T1, Table2 T2, Table3 T3 WHERE T3.id = T1.id and T3.id = T2.id
Hello
For scenario 1, you must write separate instructions UPDATE table2 and table3.
To guard against someone else change one of these tables while you act so you can copy all relevant data in a global temporary table and update this global temporary table table3.
ENGAGE only when two tables have been changed.
You can write a procedure or an INSTEAD OF trigger to do all this.
For scenario 2, you can reference many tables that you need when new table3. It might be more efficient and simpler to use the MERGER rather than UPDATED. For example:
MERGE INTO table3 dst
WITH THE HELP OF)
SELECT t1.id
t1.column1
t1.column2
t2.column3
t2.column4
FROM table1 t1
JOIN table2 t2 ON t1.id = t2.id
) src
WE (dst.id = src_id
WHEN MATCHED THEN UPDATE
SET dst.column1 = src.column1
dst.column2 = src.column2,
dst.column3 = src.column3,
dst.column4 = src.column4,
;
-
How to join this per_rating_levels this table with query table.
Dear all,
Guide how 2 join me per_rating_levels this table with query because, I want 2 see the per_rating_levels.name against all employees.
When I join this table with query it shows several recording/cortion against this record.
Query:
SELECT
PAPF.full_name employee_name,
papf1.full_name supervisor_name,
WOMEN'S WEAR. Employee_number,
hr_general.decode_job (PAAF.job_id) job_name,
Department of hr_general.decode_organization (PAAF.organization_id),
PC.Name, PCE.Comments EmployeeComments,
(by selecting pce1.comments in per_competence_elements pce1
where
PCE.assessment_id = pce1.assessment_id
AND pce.competence_id = pce1.competence_id
AND pce1.object_id = pce.object_id) ManagerComments;
--(sélectionnez rtl.name dans rtl où les pc.) RATING_SCALE_ID = rtl. Name RATING_SCALE_ID)
OF per_all_people_f women's wear.
per_all_people_f papf1,
per_all_assignments_f ADP,
PA per_appraisals,
pat per_appraisal_templates,
per_assessments not,
per_competence_elements pce,
per_competences pc
WHERE papf.person_id = paaf.person_id
AND paaf.supervisor_id = papf1.person_id
AND paaf.primary_flag = 'Y '.
AND pa.appraisee_person_id = papf.person_id
AND pa.appraisal_template_id = pat.appraisal_template_id
AND pa.appraisal_id = pas.appraisal_id
AND pat.assessment_type_id = pas.assessment_type_id
AND pas.assessment_id = pce.assessment_id
AND pce.object_id = papf.person_id
AND pce.competence_id = pc.competence_id
AND trunc (sysdate) BETWEEN papf.effective_start_date AND papf.effective_end_date
AND trunc (sysdate) BETWEEN papf1.effective_start_date AND papf1.effective_end_date
AND trunc (sysdate) BETWEEN paaf.effective_start_date AND paaf.effective_end_date
- AND papf.employee_number =: p_employee_number
- AND pa.appraisal_date =: p_appraisal_date
- AND papf.business_group_id =: p_bg_id
order of papf.employee_number
Concerninguser10941925 wrote:
Dear all,Guide how 2 join me per_rating_levels this table with query because, I want 2 see the per_rating_levels.name against all employees.
When I join this table with query it shows several recording/cortion against this record.'2' in your question means "to"? If so please do not use text instant message in this forum.
Now I suppose that PRE_RATING_LEVELS is a table in your application. And you are trying to include this table in an existing query. But in doing so, you have found the Cartesian product, correct?
In fact, how do you think someone a public forum without any knowledge of your table and data structure could help you?
Lets see, here's your query. I formatted.
select papf.full_name employee_name , papf1.full_name supervisor_name , papf.employee_number employee_number , hr_general.decode_job(paaf.job_id) job_name , hr_general.decode_organization(paaf.organization_id) department , pc.name name , pce.comments employeecomments , ( select pce1.comments from per_competence_elements pce1 where pce.assessment_id = pce1.assessment_id and pce.competence_id = pce1.competence_id and pce1.object_id = pce.object_id ) managercomments from per_all_people_f papf , per_all_people_f papf1 , per_all_assignments_f paaf , per_appraisals pa , per_appraisal_templates pat , per_assessments pas , per_competence_elements pce , per_competences pc where papf.person_id = paaf.person_id and paaf.supervisor_id = papf1.person_id and paaf.primary_flag = 'Y' and pa.appraisee_person_id = papf.person_id and pa.appraisal_template_id = pat.appraisal_template_id and pa.appraisal_id = pas.appraisal_id and pat.assessment_type_id = pas.assessment_type_id and pas.assessment_id = pce.assessment_id and pce.object_id = papf.person_id and pce.competence_id = pc.competence_id and trunc(sysdate) between papf.effective_start_date and papf.effective_end_date and trunc(sysdate) between papf1.effective_start_date and papf1.effective_end_date and trunc(sysdate) between paaf.effective_start_date and paaf.effective_end_date order by papf.employee_number
Now, you want to add the PRE_RATING_LEVELS in the list so that you can use the column NAME.
First thing you need to do is to determine the relationship between PRE_RATING_LEVELS and other tables. A relationship can be
1. one on one
2 one-to-many
3. - to-severalSo when you tried to join, your state of health has resulted in 2nd or 3rd type of relationship. If you arrive with someone who knows the business and the data and find the table that could uniquely identify a line of PRE_RATING_LEVELS.
-
Explicit cursor, column from several tables in a variable
Here's the situation. This DB has tables of metadata. An example of table name would be g3e_attribute. Once you insert a record in a metadata table a trigger of executions insert and insert records in the g3e_attribute_0009 table (table of language, this is what I call it). It would insert in g3e_attribute_000C is the default language is the French. There is also a delete trigger so that if you delete a record in a table of metadata it deletes the corresponding language table. Well, somehow we have entered the language tables that are not in the metadata table. A problem with the system.
There are 139 tables of language so I prefer not to do it manually.
So I'm keen to make this code
delete < table language > where < primary > not in (select < primary > in < metadata table >).
So I need to loop through the table and and the corresponding primary keys.
I have this code to get the table names
Select table_name, substr (table_name, 0, (length (table_name)-5)) in all_tables where table_name like 'G3E_ % _000% ';
This will display both the language metadata tale table names and.
The table of language with either _0009 or _000C as a suffix, I take everything but the last 5 characters to get the table metadata.
And I have the code to find the primary key of the table.
SELECT column_name FROM ALL_CONS_COLUMNS A JOIN ALL_CONSTRAINTS C ON A.CONSTRAINT_NAME = C.CONSTRAINT_NAME WHERE 'G3E_LEGENDENTRY' AND C.CONSTRAINT_TYPE = C.TABLE_NAME = 'P ';
So I just need to loop through all the tables. Put these pieces together, that's where I encountered a problem. I started to do a procedure, but I ran into a problem.
When you loop, you move the record being the explicit cursor in a variable. Well, you can set the variable as a vertain column type (%), or type (%ROWTYPE) of the line. Although the sql I use to create the cursor takes columns from multiple tables and returns three columns. So I'm not sure how ot define the variable. The only work around I found was to make a table column_name that contains the table table,. Define the variable as rowType.
Is there an easier way to do this?
Thank youA couple of points.
You should not really mix ansi joins and joins in the place where clause. Choose a style and stick to it.
You can't do the DDL directly in pl/sql. You need to do this, use dynamic sql. something like:
create or replace PROCEDURE SYNC_LANGMETATABLES AS CURSOR c_TABLE_PRIMARY IS select D.table_name, substr(D.table_name,0,(length(D.table_name)-5)), column_name from all_tables D, JOIN ALL_CONSTRAINTS C on C.TABLE_NAME = D.TABLE_NAME join ALL_CONS_COLUMNS A ON A.CONSTRAINT_NAME = C.CONSTRAINT_NAME where D.table_name like 'G3E_%_000%' AND C.CONSTRAINT_TYPE = 'P'; v_LANGTABLE ALL_TABLES.TABLE_NAME%TYPE; v_METATABLE ALL_TABLES.TABLE_NAME%TYPE; v_PRIMKEY ALL_CONS_COLUMNS.COLUMN_NAME%TYPE; BEGIN OPEN c_TABLE_PRIMARY; LOOP FETCH c_TABLE_PRIMARY INTO v_LANGTABLE,v_METATABLE,v_PRIMKEY; EXIT WHEN c_TABLE_PRIMARY%NOTFOUND; execute immediate 'delete from '||v_langtable||' where '||v_PRIMKEY|| ' NOT IN(SELECT '||v_PRIMKEY||' FROM v_METATABLE)'; END LOOP; CLOSE C_TABLE_PRIMARY; END;
John
Maybe you are looking for
-
Can I extract text from a document of numbers?
I made a list of items in a document of numbers so I could get an alphabetical sort it. Now, I would like to copy this text into a Pages document. I select the text in the document numbers, copy it, then paste it into the page document, but what appe
-
How to disable the MIDI to the logic of just my USB key
Question: Where exactly should I do to disable the MIDI to the logic of just my USB key (I am sending them to another application and do not want it also picked up in L, but don't want any logical MIDI stop). And where does logic to send any MIDI out
-
HP 15 laptop: atheros wifi causes extremely high latency peaks
Windows 10 has been the only OS I have used, so I can't speak for others... I noticed that (initially copying files via wifi lan has shown) using wifi really totrures my laptop... it's worse than if the CPU is 100% responsible... I've updated either
-
How can I play the MKV in KDL-40EX650 format.
I have KDL40EX6500. This model can play MKV video format If yes what is the procedure. Thank you
-
USRP 2920 if after joint bottom (after the MIXING table)?
Salvation _ orthe NI USRP-2920 is a good product I use it to train students on some applicationsI read manual for this product its especially usfel and covering a large part of the specs of the device, BUT "nothing mentioned about.Central frequency (