Inner or Outer left Join - oracle

I'm having difficulty modifying a script for this situation and wondering if someone maybe able to help:
I have an address table and a phone table both sharing the same column called id_number. So id_number = 2 on both tables refers to the same entity. Address and phone information used to be stored in one table (the address table) but it is now split into address and phone tables since we moved to Oracle 11g.
There is a 3rd table called both_ids. This table also has an id_number column in addition to an other_ids column storing SSN and some other ids.
Before the table was split into address and phone tables, I had this script:
(Written in Sybase)
INSERT INTO sometable_3 (
SELECT a.id_number, a.other_id,
NVL(a1.addr_type_code,0) home_addr_type_code,
NVL(a1.addr_status_code,0) home_addr_status_code,
NVL(a1.addr_pref_ind,0) home_addr_pref_ind,
NVL(a1.street1,0) home_street1,
NVL(a1.street2,0) home_street2,
NVL(a1.street3,0) home_street3,
NVL(a1.city,0) home_city,
NVL(a1.state_code,0) home_state_code,
NVL(a1.zipcode,0) home_zipcode,
NVL(a1.zip_suffix,0) home_zip_suffix,
NVL(a1.telephone_status_code,0) home_phone_status,
NVL(a1.area_code,0) home_area_code,
NVL(a1.telephone_number,0) home_phone_number,
NVL(a1.extension,0) home_phone_extension,
NVL(a1.date_modified,'') home_date_modified
FROM both_ids a, address a1
WHERE a.id_number = a1.id_number(+)
AND a1.addr_type_code = 'H');
Now that we moved to Oracle 11g, the address and phone information are split.
How can I modify the above script to generate the same result in Oracle 11g?
Do I have to first do INNER JOIN between address and phone tables and then do a LEFT OUTER JOIN to both_ids?
I tried the following and it did not work:
Insert Into..
select ...
FROM a1. address
INNER JOIN t.Phone ON a1.id_number = t.id_number
LEFT OUTER JOIN both_ids a ON a.id_number = a1.id_number
WHERE a1.adrr_type_code = 'H'

There is a syntax error in your query. You wrote in comment below that your query is:
..
FROM address a1 JOIN telephone t ON a1.id_number = t.id_number
AND RIGHT OUTER JOIN tt_gl_both_ids a ON a.id_number = a1.id_number
WHERE
..
Throw out AND and it will work. Also, replace right outer join with left outer join, since that's what you're trying to achieve (again, based on comments you wrote).

Related

Join to another table when the join column is null

I have a sql that is selecting many things from the database however I would like that data to only comeback which is matched to a personal table I have.
I would like to join a column [vin_code] from my table [population] however there are nulls in here and were there are nulls I would like to join another column from my table to another table in the database.
I will give an example sql below:
Select distinct v.kegal_rntity_id
From vin v
Inner join ops$dami.population pop
On v.vin_code = pop.vin_code
Then were pop.vin_code is null I would like to join pop.vis_code on a table in the database called zegal_rentity z column z.vis_code
So something like
join zegal_rentity z
On pop.vis_code = z.vis_code
But I only want to do this were pop.vin_code is null
As sample data is not available, I am unable to test the solution but try the following query with condition based outer join.
Select distinct v.kegal_rntity_id
From ops$dami.population pop
Left join vin v
On v.vin_code = pop.vin_code
Left join zegal_rentity z
On (case when pop.vin_code is null and
pop.vis_code = z.vis_code then 1 end = 1);
Cheers!!

Oracle how to use distinct command in join

Im am trying to get a list of distinct values from one column whilst getting the key column data by inner join on another table as below..table a and table b hold key column of client.
Table b holds column product which has a range of values against a range of client numbers
Table a holds only client numbet
Table b holds client number and product
Client product
1. A
1. B
2. B
3. C
I want to find the list of distinct product values where the client is in table a and table b
Any suggestions welcome
find the list of distinct product values where the client is in table
a and table b
As you will notice below "distinct" isn't applied in joins
SELECT DISTINCT
b.Product
FROM TABLEA a
INNER JOIN TABLEB b ON a.Client = b.Client
;
The inner join ensures that client exists in both A and B and then the "select distinct" removes any repetition in the list of products.
SELECT
b.Product
, COUNT(*) AS countof
FROM TABLEA a
INNER JOIN TABLEB b ON a.Client = b.Client
GROUP BY
b.Product
;
An alternative, which also makes a distinct list of products where clients are in A and B is to use group by, with the added bonus that this way you can do some extra stuff like counting how often a product is referenced.
try it at SQLFiddle.com

JOIN 4 tables in one (Oracle R11)

I need to create a query that shows the "Legal Entity", "Application Name", "Close Date" and "Period" I'm working with Oracle R11, Right now I've found the query for
"Legal Entity"
SELECT name
FROM hr_organization_information HOI
INNER JOIN hr_all_organization_units HAOU
ON HOI.ORGANIZATION_ID = Haou.Organization_Id
WHERE HOI.org_information_context LIKE 'Legal Entity Accounting'
ORDER BY NAME ASC;
and for "Application Name, Close Date, Period"
SELECT A.APPLICATION_ID,
B.APPLICATION_NAME,
TO_CHAR(A.END_DATE,'HH24:MI DD-MON-YYYYI'),
A.PERIOD_NUM
FROM GL_PERIOD_STATUSES A
INNER JOIN FND_APPLICATION_TL B ON A.APPLICATION_ID = B.APPLICATION_ID
WHERE A.Application_Id=101
AND LANGUAGE='US'
OR A.APPLICATION_ID=200
AND LANGUAGE='US'
OR A.APPLICATION_ID=222
AND LANGUAGE='US';
Separately but I haven't found the way to join them in one query, can you help me with that?
Antonio, I think Brian has given you sound advice. Posting to an EBS forum (or whatever application this is) might also be worthwhile if his advice has not lead you to the answer. I will offer that sometimes the way to join table_A and table_B is through table_C. That is, if you do not find any directly related data in the queries of one se to one of the tables in the other set then look at the FK defined on and pointing to these tables to see if you can find a table not currently part of either query that relates the sets. You figure out how to join each of your current queries to it and that is how you join the two queries together.
Thank you all!
The advices that all of you gave to me were useful, I've found the table HR_LEGAL_ENTITIES (Table C) that have two columns that allow me to join Table A with Table B, the final query was:
SELECT HAOU.NAME,
FAT.APPLICATION_NAME,
TO_CHAR(GPS.END_DATE,'HH24:MI DD-MON-YYYY'),
GPS.PERIOD_NUM
FROM HR_ALL_ORGANIZATION_UNITS HAOU
INNER JOIN HR_LEGAL_ENTITIES HLE
ON HLE.ORGANIZATION_ID = HAOU.ORGANIZATION_ID
INNER JOIN GL_PERIOD_STATUSES GPS
ON HLE.SET_OF_BOOKS_ID = GPS.SET_OF_BOOKS_ID
INNER JOIN FND_APPLICATION_TL FAT
ON GPS.APPLICATION_ID = FAT.APPLICATION_ID
WHERE GPS.Application_Id IN (101,200,222) AND LANGUAGE='US'
ORDER BY NAME ASC;
Regards!

Derived table parsed in universe designer fails in Webi

I've run into a problem I've never had before. I've built a derived table with 4 result objects that, according to Universe Designer, works correctly. The table and all 4 objects are successfully parsed, when I run the table via the Table values option it returns exactly what I expect, in full agreement with the data our frontend is reporting. So far so good.
The issue I'm having is in Webi. When I bring any object from that table into a new report and run it I get the following error:
A database error occured. The database error text is: ORA-00903: invalid table name. (WIS 10901)
How is this possible? How can an object that Universe Designer and, I assume, our Oracle database both say works fine and returns data, give me this error when I use that object in Webi? I've rebuilt the table, rechecked all the table names and shortened them just in case, there are no unusual alphanumeric characters in any table name, what else can I do? Here's the code that works in the universe and fails in WebI:
SELECT distinct
to_number(p.ID) as person_id,
e.ID as EPISODE_ID,
et.DESCRIPTION as episode_type,
nvl(rfa.date_answer,e.start_date) as LAST_REVIEW_IN_PERIOD
FROM
PEOPLE p
INNER JOIN EPISODES ref_e on (p.id=ref_e.subject)
LEFT OUTER JOIN REPORT_FORM_ANSWERS ref_rfa on (ref_e.ID = ref_rfa.episode_id and ref_rfa.section_item_tag_id ='0AA384CC-DDD8-A85F-6B68-060CCD878DE9') -- Date of referral - Response and Rehabilitiation referral
LEFT OUTER JOIN EPISODE_OUTCOMES ref_eo on (ref_e.ID=ref_eo.EPISODE_ID)
LEFT OUTER JOIN EPISODES tri_e on (ref_eo.NEW_EPISODE_ID=tri_e.ID)
INNER JOIN EPISODES e ON p.ID=e.SUBJECT
LEFT OUTER JOIN #DerivedTable("DT_CA_RRRT_SUEligible") on (e.ID="DT_CA_RRRT_SUEligible".EPISODE_ID)
INNER JOIN EPISODE_TYPES et ON e.TYPE=et.TYPE
LEFT OUTER JOIN report_form_answers rfa on e.id=rfa.episode_id and rfa.section_item_tag_id in (
'C8AEC1B5-6115-20CC-E160-EC9CB3854752', -- End Date - Response and Rehabilitiation Assessment
'6DC97CE9-CA70-042A-3894-44CA551440DB' -- Assessment end date - RRRT Assessment of Care and Support Needs
)
LEFT OUTER JOIN #DerivedTable("DT_PBLTElementsWithDates") on (p.id="DT_PBLTElementsWithDates".PERSON_ID and "DT_PBLTElementsWithDates".service_start<ref_rfa.date_answer and ("DT_PBLTElementsWithDates".service_end>=add_months(ref_rfa.date_answer,-12) or "DT_PBLTElementsWithDates".service_end IS NULL))
WHERE
(et.DESCRIPTION in('Response and Rehabilitation Assessment'))
and
ref_e.id=dbo.f_workflow_first(e.ID,'RRRT Referrals')
AND
tri_e.TYPE='RRRTHOSTRI'
and
e.end_date IS NOT NULL
and
"DT_PBLTElementsWithDates".SERVICE_TYPE_ID IS NOT NULL
and
"DT_CA_RRRT_SUEligible".TEXT_ANSWER='Yes'
and
nvl(rfa.date_answer,e.start_date) <= #Prompt('Select reviews to','D',,mono,free)
I'm at a loss. What is going on, what am I missing?

Oracle ignores hint for index with synonym and 2 views

This is the query i am running:
select /*+ index(V_AMV_PLG_ORDER_HISTORY_200_MS.orders.T0 IDX_ORDER_VERSION_3) */ *
from V_AMV_PLG_ORDER_HISTORY_200_MS
where EXCHANGE_SK = 32 and PRODUCT_SK = 1000169
And it uses a different index than the one i am ordering it to.
As you can see, I am querying from the view V_AMV_PLG_ORDER_HISTORY_200_MS, you can see its sql query here:
V_AMV_PLG_ORDER_HISTORY_200_MS view SQL Query:
SELECT AMV_PERF_PROFILES_FRONTEND.AMV_PLG_GET_SEGMENT(200, orders.ORDER_GLOBAL_DATE_TIME) AS ORDER_DATE_TIME,
SUM(orders.BASE_VOLUME) AS VOLUME,
SUM(orders.BASE_CURR_LIMIT_PRICE*orders.BASE_VOLUME)/SUM(orders.BASE_VOLUME) AS PRICE,
orders.PRODUCT_SK AS PRODUCT_SK,
orders.EXCHANGE_SK AS EXCHANGE_SK,
orders.DIRECTION_CD AS DIRECTION_CD,
orders.AGG_UNIT_CD AS AGG_UNIT_CD,
orders.TRADER_KEY AS EXECUTING_REPRESENTATIVE_KEY,
orders.ACCOUNT_KEY AS ACCOUNT_KEY,
a.BUSINESS_UNIT_CD AS BUSINESS_UNIT_CD
FROM AMV_PERF_PROFILES_FRONTEND.S_AMV_ORDER_VERSION_NEW orders
INNER JOIN AMV_PERF_PROFILES_FRONTEND.S_AMV_ACCOUNT a
ON a.ACCOUNT_KEY = orders.ACCOUNT_KEY
WHERE BASE_VOLUME > 0
GROUP BY AMV_PERF_PROFILES_FRONTEND.AMV_PLG_GET_SEGMENT(200, orders.ORDER_GLOBAL_DATE_TIME),
orders.PRODUCT_SK,
orders.EXCHANGE_SK,
orders.ACCOUNT_KEY,
a.BUSINESS_UNIT_CD,
orders.AGG_UNIT_CD,
orders.TRADER_KEY,
orders.DIRECTION_CD;
He is getting the data using the Synonym S_AMV_ORDER_VERSION_NEW, Which directs to another Scheme, to a view called V_AMV_ORDER_VERSION and refering to it as orders, its sql query here:
V_AMV_ORDER_VERSION view Sql query:
SELECT T1.ENTITY_KEY ,
T2.AGG_UNIT_CD ,
T0.BASE_CURR_LIMIT_PRICE ,
T7.DIRECTION_CD ,
T0.EXCHANGE_SK,
T0.ORDER_LOCAL_DATE_TIME ,
T0.PRODUCT_SK,
T18.ENTITY_KEY ,
T19.ENTITY_KEY ,
T0.NOTIONAL_VALUE2 ,
T0.NOTIONAL_VALUE ,
T0.ORDER_GLOBAL_DATE_TIME ,
T0.BASE_VOLUME ,
T31.TRANSACTION_STATUS_CD ,
T0.ORDER_VERSION_KEY
FROM ETS_UDM_CDS_NEW.ORDER_VERSION T0
LEFT OUTER JOIN ETS_UDM_CDS_NEW.ENTITY T1
ON T0.ACCOUNT_SK = T1.ENTITY_SK
LEFT OUTER JOIN ETS_UDM_CDS_NEW.AGG_UNIT T2
ON T0.AGG_UNIT_SK = T2.ENTITY_SK
LEFT OUTER JOIN ETS_UDM_CDS_NEW.DIRECTION T7
ON T0.DIRECTION_SK = T7.ENTITY_SK
LEFT OUTER JOIN ETS_UDM_CDS_NEW.ENTITY T18
ON T0.LOCAL_TIME_ZONE_SK = T18.ENTITY_SK
LEFT OUTER JOIN ETS_UDM_CDS_NEW.ENTITY T19
ON T0.TRADER_SK = T19.ENTITY_SK
LEFT OUTER JOIN ETS_UDM_CDS_NEW.TRANSACTION_STATUS T31
ON T0.TRANSACTION_STATUS_SK = T31.ENTITY_SK;
Which takes its data from a table called ORDER_VERSION and refers to it as T0
this table has an index called IDX_ORDER_VERSION
The problem is that oracle ignores my hint, And uses a different index, Now, I have managed to use a hint to make oracle use an index i wanted when i was querying a view that gets data from a table, But this time I am querying a view which gets his data from another view which gets his data from a table.
And also, The second view in the line is on a different Scheme and i am using a synonym, So perhaps that is why i am missing something Cuz i tried many combinations of possible solutions i found on google but nothing seems to be working...
I would say that if i go one step forward and query directly from V_AMV_ORDER_VERSION (Without the synonym) IT works and i can make oracle work with any index i want, so this query works perfect:
select /*+ index(orders.T0 IDX_ORDER_VERSION_5) */ * from V_AMV_ORDER_VERSION orders
where EXCHANGE_SK =32 and PRODUCT_SK = 1000169
Well me and our company's DBA looked at it for a while, it seems like an Oracle bug in the Global Hint manifestation, We have created the view V_AMV_PLG_ORDER_HISTORY_200_MS using a regular join rather than an ANSI join, and now it works properly:
V_AMV_PLG_ORDER_HISTORY_200_MS view SQL Query:
SELECT AMV_PERF_PROFILES_FRONTEND.AMV_PLG_GET_SEGMENT(200, orders.ORDER_GLOBAL_DATE_TIME) AS ORDER_DATE_TIME,
SUM(orders.BASE_VOLUME) AS VOLUME,
SUM(orders.BASE_CURR_LIMIT_PRICE*orders.BASE_VOLUME)/SUM(orders.BASE_VOLUME) AS PRICE,
orders.PRODUCT_SK AS PRODUCT_SK,
orders.EXCHANGE_SK AS EXCHANGE_SK,
orders.DIRECTION_CD AS DIRECTION_CD,
orders.AGG_UNIT_CD AS AGG_UNIT_CD,
orders.TRADER_KEY AS EXECUTING_REPRESENTATIVE_KEY,
orders.ACCOUNT_KEY AS ACCOUNT_KEY,
a.BUSINESS_UNIT_CD AS BUSINESS_UNIT_CD
FROM AMV_PERF_PROFILES_FRONTEND.S_AMV_ORDER_VERSION_NEW orders,
AMV_PERF_PROFILES_FRONTEND.S_AMV_ACCOUNT a
WHERE BASE_VOLUME > 0 AND a.ACCOUNT_KEY = orders.ACCOUNT_KEY
GROUP BY AMV_PERF_PROFILES_FRONTEND.AMV_PLG_GET_SEGMENT(200, orders.ORDER_GLOBAL_DATE_TIME),
orders.PRODUCT_SK,
orders.EXCHANGE_SK,
orders.ACCOUNT_KEY,
a.BUSINESS_UNIT_CD,
orders.AGG_UNIT_CD,
orders.TRADER_KEY,
orders.DIRECTION_CD;

Resources