rewrite the left join
Work according to code, but I want to rewrite them in the left join on style, howSelect *.
of a, b, c, d, e
v_codeNumber f
where a.my_numb = '5582017'
and a.username = b.username
and c.username = b.username
and c.DEPARTMENT_CODE = d.TDEPT_CODE
and c.discipline_num = f.discipline_num (+)
You mean you want the ANSI syntax?
select *
from a join b on (a.username = b.username)
join c on (c.username = b.username)
join d on (c.DEPARTMENT_CODE= d.TDEPT_CODE)
cross join e
left join v_codeNumber f on (c.discipline_num = f.discipline_num)
where a.my_numb ='5582017'
Tags: Database
Similar Questions
-
The left join - no data in the table don't want to display the data from the table one
Hello
I'm writing a query. Here is my scenario, I have two tables EMP and EMP_TYPE. I want to join both the table and even if there is no matching record in the table for EMP EMP_TYPE, I want to return the record to the EMP table and empty values of EMP_TYPE.
create table EMP
(
EMPID NUMBER(10,2),
EMPNAME VARCHAR2(100)
);
INSERT INTO emp (empid,empname) values (1, 'abc');
INSERT INTO emp (empid,empname) values (2, 'xyz');
INSERT INTO emp (empid,empname) values (3, 'UNJ');
create table EMP_Type
(
ID NUMBER(10,2),
EMPID NUMBER(10,2),
TYPE_ID NUMBER(10,2),
END_DATE DATE
);
INSERT INTO EMP_Type(id, empid, TYPE_ID, END_DATE) values (100, 2, 1, SYSDATE);
select *
from emp e
left outer join emp_Type t
on e.empid = t.empid
and t.type_id = 1
returns.
EMPID EMPNAME ID EMPID TYPE_ID END_DATE
2.00 xyz 100.00 2.00 1.00 10/9/2012 5:18:59 PM
3.00 UNJ
1.00 abc
select *
from emp e
left outer join emp_Type t
on e.empid = t.empid
and t.type_id = 1
and t.empid = 3
returns
EMPID EMPNAME ID EMPID TYPE_ID END_DATE
2.00 xyz
3.00 UNJ
1.00 abc
but instead I want
EMPID EMPNAME ID EMPID TYPE_ID END_DATE
3.00 UNJYour conditions of conflict
>
and t.type_id = 1
and t.empid = 3
>
There is a record with t.empid 3select * from empx e, empx_Type t where e.empid = 3 and e.empid = t.empid (+) EMPID,EMPNAME,ID,EMPID_1,TYPE_ID,END_DATE 3,UNJ,,,,
-
Clarification on NULL or behavior LEFT JOIN behavior
Good day ladies------Gentlemen\ gurus-Experts-and Al:
Just when I thought I got my mind wrapped around the notion of NULL value, as defined by Oracle, I met what follows below. Basically, what you are watching is how our employees of the human resources tables are set up (for the most part). We have the employees, employee contracts table and the tables of positions. When I want to make a list of active staff, I always use the filter end date, you will see below in the Super request, essentially looking for staff who do not have a contract AND a position end date entered in the DB. It works like a charm. However, today, I noticed an official on the list, who has never had a position is entered in the comic book, and he had an 'open' contract In my example below, why would "Hal Jordan" appear on the active staff list? Yes, he spends the first part of my predicate (contract_end is null), but it does not work (at least by my understanding) past the second part of the predicate which is position_end has the value NULL because there not even a record is entered in the comics! Why would he appear? Is it because of the LEFT JOIN or a NULL rule type, I'm not aware of? Thanks for your help!
Aqua
EMPLOYEES AS
(
SELECT '10' AS emp_id, 'Banner' last_name, 'Robert' NAME FROM dual
UNION ALL
SELECT '20', 'Wayne', 'Bruce' OF THE double
UNION ALL
SELECT '30', 'Kent', 'Clark' FROM dual
UNION ALL
SELECT "40", "Parker", "Peter" OF THE double
UNION ALL
SELECT '50', 'Last', 'Tony' FROM dual
UNION ALL
SELECT '60', 'Jordan', 'Hal' OF THE double
),
emp_contracts AS
(
SELECT '10' AS emp_id, to_date (January 1, 2011 ',' MON-DD-YYYY "") LIKE contract_start, to_date (1 January 2012 ',' MON-DD-YYYY') AS contract_end FROM dual
UNION ALL
To_date '10', SELECT (January 1, 2010 ',' MON-DD-YYYY ""), to_date (December 31, 2011 ',' MON-DD-YYYY') OF double
UNION ALL
To_date '10', SELECT (January 2, 2012 ',' MON-DD-YYYY ""), double NULL
UNION ALL
To_date '20', SELECT (January 1, 2011 ',' MON-DD-YYYY ""), to_date (1 January 2012 ',' MON-DD-YYYY') OF double
UNION ALL
To_date '20', SELECT (January 1, 2010 ',' MON-DD-YYYY ""), to_date (December 31, 2011 ',' MON-DD-YYYY') OF double
UNION ALL
To_date '20', SELECT (January 2, 2012 ',' MON-DD-YYYY ""), double NULL
UNION ALL
To_date '30', SELECT (January 1, 2011 ',' MON-DD-YYYY ""), to_date (1 January 2012 ',' MON-DD-YYYY') OF double
UNION ALL
To_date '30', SELECT (January 1, 2010 ',' MON-DD-YYYY ""), to_date (December 31, 2011 ',' MON-DD-YYYY') OF double
UNION ALL
To_date '30', SELECT (January 2, 2012 ',' MON-DD-YYYY ""), double NULL
UNION ALL
To_date '40', SELECT (January 1, 2011 ',' MON-DD-YYYY'), to_date (1 January 2012 ',' MON-DD-YYYY') FROM dual
UNION ALL
To_date '40', SELECT (January 1, 2010 ',' MON-DD-YYYY'), to_date (December 31, 2011 ',' MON-DD-YYYY') FROM dual
UNION ALL
To_date '40', SELECT (January 2, 2012 ',' MON-DD-YYYY ""), double NULL
UNION ALL
To_date '50', SELECT (January 1, 2011 ',' MON-DD-YYYY ""), to_date (1 January 2012 ',' MON-DD-YYYY') OF double
UNION ALL
To_date '50', SELECT (January 1, 2010 ',' MON-DD-YYYY ""), to_date (December 31, 2011 ',' MON-DD-YYYY') OF double
UNION ALL
To_date '50', SELECT (January 2, 2012 ',' MON-DD-YYYY ""), double NULL
UNION ALL
To_date '60', SELECT (January 1, 2011 ',' MON-DD-YYYY ""), to_date (1 January 2012 ',' MON-DD-YYYY') OF double
UNION ALL
To_date '60', SELECT (January 1, 2010 ',' MON-DD-YYYY ""), to_date (December 31, 2011 ',' MON-DD-YYYY') OF double
UNION ALL
To_date '60', SELECT (January 2, 2012 ',' MON-DD-YYYY ""), double NULL
),
emp_positions AS
(
SELECT '10' AS emp_id, '43XY' AS title_id, to_date (January 1, 2011 ',' MON-DD-YYYY "") LIKE position_start, to_date (1 January 2012 ',' MON-DD-YYYY') AS position_end FROM dual
UNION ALL
SELECT ' 10', '47NT', to_date (January 1, 2010 ',' MON-DD-YYYY ""), to_date (December 31, 2011 ',' MON-DD-YYYY') OF double
UNION ALL
SELECT ' 10', '41JE', to_date (January 2, 2012 ',' MON-DD-YYYY ""), double NULL
UNION ALL
SELECT ' 20', '48AW', to_date (January 1, 2011 ',' MON-DD-YYYY ""), to_date (1 January 2012 ',' MON-DD-YYYY') OF double
UNION ALL
SELECT ' 20', '42KD', to_date (January 1, 2010 ',' MON-DD-YYYY ""), to_date (December 31, 2011 ',' MON-DD-YYYY') OF double
UNION ALL
SELECT ' 20', '46LD', to_date (January 2, 2012 ',' MON-DD-YYYY ""), double NULL
UNION ALL
SELECT ' 30 ', 45 MI', to_date (January 1, 2011 ',' MON-DD-YYYY'), to_date (1 January 2012 ',' MON-DD-YYYY') FROM dual
UNION ALL
SELECT ' 30', '44KH', to_date (January 1, 2010 ',' MON-DD-YYYY ""), to_date (December 31, 2011 ',' MON-DD-YYYY') OF double
UNION ALL
SELECT ' 30', '43LL', to_date (January 2, 2012 ',' MON-DD-YYYY ""), double NULL
UNION ALL
SELECT ' 40', '41', to_date (January 1, 2011 ',' MON-DD-YYYY ""), to_date (1 January 2012 ',' MON-DD-YYYY') OF double
UNION ALL
SELECT ' 40', '40', to_date (January 1, 2010 ',' MON-DD-YYYY ""), to_date (December 31, 2011 ',' MON-DD-YYYY') OF double
UNION ALL
SELECT ' 40', '42DX', to_date (January 2, 2012 ',' MON-DD-YYYY ""), double NULL
UNION ALL
SELECT ' 50', '48IB', to_date (January 1, 2011 ',' MON-DD-YYYY ""), to_date (1 January 2012 ',' MON-DD-YYYY') OF double
UNION ALL
SELECT '50 ', 47 DL', to_date (January 1, 2010 ',' MON-DD-YYYY'), to_date (December 31, 2011 ',' MON-DD-YYYY') FROM dual
UNION ALL
SELECT ' 50', '42A', to_date (January 2, 2012 ',' MON-DD-YYYY ""), double NULL
)
SELECT
- ES.emp_id,
- ES.last_name,
- ES.first_name,
- EP.title_id,
- EP.position_start,
- EP.position_end
Employees are
LEFT JOIN emp_contracts ec
ON ec.emp_id = es.first_name
LEFT JOIN emp_positions ep
ON ep.emp_id = es.emp_id
WHERE ec.contract_end IS NULL AND ep.position_end IS NULL
Hello
AquaNX4 wrote:
Good day ladies------Gentlemen\ gurus-Experts-and Al:
Just when I thought I got my mind wrapped around the notion of NULL value, as defined by Oracle, I met what follows below. Basically, what you are watching is how our employees of the human resources tables are set up (for the most part). We have the employees, employee contracts table and the tables of positions. When I want to make a list of active staff, I always use the filter end date, you will see below in the Super request, essentially looking for staff who do not have a contract AND a position end date entered in the DB. It works like a charm. However, today, I noticed an official on the list, who has never had a position is entered in the comic book, and he had an 'open' contract In my example below, why would "Hal Jordan" appear on the active staff list? Yes, he spends the first part of my predicate (contract_end is null), but it does not work (at least by my understanding) past the second part of the predicate which is position_end has the value NULL because there not even a record is entered in the comics! Why would he appear? Is it because of the LEFT JOIN or a NULL rule type, I'm not aware of? ...
It's the outer join.
When you say
Employees are
LEFT JOIN emp_positions ON ep.emp_id = es.emp_id ep
then, at the time when the WHERE clause is evaluated, the result set contains at least 1 row for each line of employees. If there is no line in emp_postitions that meet the join condition, then all the columns that should be provided by the emp_positions table will be NULL. This is precisely what concerns the outer join.
Think about it for a minute. NULL means the absence of any value. Is there is no corresponding row in the emp_positions table, then there is no value for emp_position.position_end. If so doesn't; "have a value, then, by definition, it is NULL.
It if were not NULL, then this would mean that it had a value. Would what value? Why the request would give him one value rather than another?
Thanks for the display of the data of the sample; It's always useful!
-
Hello
I'm having a problem with the left join query, when I join table a two table based on column task1 I get duplicate in table1.task1, table1.price.
Table1. Task1 Table1. Price table2. Task1 table2. Resource table2. Price 001 100 001 A 50 001 100 001 B 250 How can I make a request to get a result as below.
Table1. Task1 Table1. Price table2.Task2 table2. Resource table2. Price 001 100 001 A 50 001 B 250 Thank you.
Note that your query uses an inner join. Your original question mentioned a join left, generally interpreted as meaning a left OUTER join.
Anyway, according to Frank, you can use the BREAK command in SQL * Plus for the goal sought through formatting. You can also use an analytical function as Roger suggests. I think ROW_NUMBER() might do the trick, but we must be clear about the criteria for partitioning and ordering the results, for example
WITH table1 AS ( SELECT '001' AS task1 , 100 AS price FROM dual ), table2 AS ( SELECT '001' AS task1 , 'A' AS resources , 50 AS price FROM dual UNION ALL SELECT '001' AS task1 , 'B' AS resources , 250 AS price FROM dual ) SELECT DECODE(ROW_NUMBER() OVER (PARTITION BY t1.task1, t1.price ORDER BY t2.resources, t2.price),1,t1.task1) AS task1_alt , DECODE(ROW_NUMBER() OVER (PARTITION BY t1.task1, t1.price ORDER BY t2.resources, t2.price),1,t1.price) AS price_alt , t2.task1 AS task_with_resource , t2.resources , t2.price FROM table1 t1 INNER JOIN table2 t2 ON t1.task1 = t2.task1 ORDER BY t1.task1, t1.price, t2.resources, t2.price;
-
Left join with three-table join query
I am trying to create a query that left me speechless. Most of the query is simple enough, but I have a problem I do not know how to solve.
Background:
We have stock stored in i_action.
We have the attributes available for each type of action. The attributes available for each action are described in shared_action_attribute. Each type of action can have three attributes or none at all.
We have the values stored for the attributes in i_attribute_value.
An example says:
We have a transfer action (action_code B4). The action of B4 entry into i_action records the fact that the transfer took place and the date at which he spoke. The attributes available for a transfer action are the function code receiver, the receiving unit number and the reason of transfer code. These types of attributes available and their order are stored in shared_action_attribute. The actual values of the attributes for a specific action of transfer are stored in i_attribute_value.
Now i_action and i_attribute_value can be connected directly in action_seq in i_action and ia_action_seq in i_attribute_value. A left join on these two tables provides results for all actions (including actions that have no attributes) and assign values (see Query 1 below).
There are two questions. First of all, I want only the first two attributes. To specify the attributes of the first two, I also i_attribute_value a link to shared_action_attribute (which is where the order is stored). I can build a simple query (without the left join) which connects the three tables, but then shares without attributes would be excluded from my result (see Query 2 below).
The second problem is that I'd actually a row returned for each action with first_attribute and second_attribute in the form of columns instead of two lines.
The final query will be used to create a materialized view.
Here are the tables and examples of what is stored in the:
TABLE i_action
Name Type
----
ACTION_SEQ NUMBER (10)
DATE OF ACTION_DATE
ACTION_CODE VARCHAR2 (3)
VARCHAR2 (1) DELETED
EXAMPLE OF LINES
ACTION_SEQ ACTION_DATE DELETED ACTION_CODE
----
45765668 9 OCTOBER 09 B2 HAS
45765670 9 OCTOBER 09 BA HAS
45765672 B6 9 OCTOBER 09A
45765673 9 OCTOBER 09 B4 HAS
45765674 9 OCTOBER 09 G1 HAS
45765675 9 OCTOBER 09 M3 HAS
TABLE i_attribute_value
Name Type
---
IA_ACTION_SEQ NUMBER (10)
SACTATT_SACT_CODE VARCHAR2 (3)
SACTATT_SAT_TYPE VARCHAR2 (3)
VARCHAR2 VALUE (50)
EXAMPLE OF LINES
IA_ACTION_SEQ SACTATT_SACT_CODE SACTATT_SAT_TYPE VALUE
----
45765668 B2 COA 37 B
45765670 BA ROA D
45765670 BA ROR P
45765672 B6 CAT C
B4 45765673 RFC E
45765673 B4 TRC P
B4 45765673 RUN 7
45765674 G1 SS 23567
G1 45765674 ASG W
TABLE shared_action_attribute
Name Type
---
SACT_CODE VARCHAR2 (3)
SAT_TYPE VARCHAR2 (3)
ORDER NUMBER (2)
TITLE VARCHAR2 (60)
EXAMPLE OF LINES
SACT_CODE SAT_TYPE UNDER THE ORDER
----
B2 ACO 1 Office code
BA ROR 1 reason to re-open
Authority of BA ROA 2 reopen
B6 CAT 1 category
B4 RFC 1 reception function code
B4 RUN 2 receives the unit code
B4 TRC 3 transfer of reason code
Sequence of G1 SS 1 personal
Reason for G1 ASG 2 assignment
QUERY 1:
It's my current query as well as its results. Most are select simple but only one column is filled using the function analytic last_value (thank you guys). The last column in the view sub stores the value of the attribute. What I want is to replace this single column with two columns named first_attribute and second_attribute and eliminate all other attributes.
SELECT ia.action_seq, ia.action_date, ia.action_code cod,
NVL
(LAST_VALUE (CASE
WHEN ia.action_code = "G1".
AND iav.sactatt_sat_type = 'SS '.
THEN THE VALUE
WHEN ia.action_code IN ('A0', 'A1')
THEN '67089'
END IGNORE NULLS
) OVER (PARTITION BY ia.ici_charge_inquiry_seq ORDER BY ia.action_date,
IA.serial_number, ia.action_seq),
'67089'
) staff_seq,.
value
From i_action LEFT JOIN i_attribute_value iav AI
ON iav.ia_action_seq = ia.action_seq
WHERE ia.deleted = 'A ';
ACTION_SEQ ACTION_DA COD STAFF_SEQ VALUE
----
45765668 9 OCTOBER 09 B2 67089 37 B
45765670 9 OCTOBER 09 BA D 67089
45765670 9 OCTOBER 09 BA 67089 P
45765672 9 OCTOBER 09 B6 67089 C
45765673 9 OCTOBER 09 B4 67089 E
45765673 9 OCTOBER 09 B4 67089 P
45765673 9 OCTOBER 09 67089 7 B4
45765674 9 OCTOBER 09 23567 23567 G1
45765674 9 OCTOBER 09 G1 23567 W
45765675 9 OCTOBER 09 M3 23567
QUERY 2:
This query is limited to the first two attributes but he also filed actions which have no attributes, and it creates still several lines for each action instead of a single line with two columns for attributes.
SELECT ia.action_seq, ia.action_date, ia.action_code cod,
NVL
(LAST_VALUE (CASE
WHEN ia.action_code = "G1".
AND iav.sactatt_sat_type = 'SS '.
THEN THE VALUE
WHEN ia.action_code IN ('A0', 'A1')
THEN '67089'
END IGNORE NULLS
) OVER (PARTITION BY ia.ici_charge_inquiry_seq ORDER BY ia.action_date,
IA.serial_number, ia.action_seq),
'67089'
) staff_seq,.
value
OF shared_action_attribute saa, ims_action AI, ims_attribute_value iav
WHERE iav.ia_action_seq = ia.action_seq
AND iav.sactatt_sact_code = saa.sact_code
AND iav.sactatt_sat_type = saa.sat_type
AND saa.display_order IN ('1 ', ' 2')
AND ia.deleted = 'A ';
ACTION_SEQ ACTION_DA VALUE OF COD
----
45765668 9 OCTOBER 09 B2 67089 37 B
45765670 9 OCTOBER 09 BA D 67089
45765670 9 OCTOBER 09 BA 67089 P
45765672 9 OCTOBER 09 B6 67089 C
45765673 9 OCTOBER 09 B4 67089 E
45765673 9 OCTOBER 09 67089 7 B4
45765674 9 OCTOBER 09 23567 23567 G1
45765674 9 OCTOBER 09 G1 23567 W
I found it quite complex to try to write - I hope that I was clear.
Thank you very much!Hello
You can use an alias for column (such as staff_seq) in the ORDER BY. Unfortunately, it's the only place where you can use it in the same query, where it was defined.
You can use it anywhere in the super-requetes, however, so you can still work around this problem in assigning the aliases in a subquery and GROUP BY (or other) in a Super query, like this:WITH ungrouped_data AS ( SELECT ia.action_seq, ia.action_date, ia.action_code, NVL (LAST_VALUE (CASE WHEN ia.action_code = 'G1' AND sactatt_sat_type = 'SS' THEN VALUE WHEN ia.action_code IN ('A0', 'A1') THEN '67089' END IGNORE NULLS ) OVER (PARTITION BY ia.ici_charge_inquiry_seq ORDER BY ia.action_date, ia.action_seq), '67089' )staff_seq, (CASE WHEN display_order = '1' THEN VALUE END) first_attribute, (CASE WHEN display_order = '2' THEN VALUE END) second_attribute FROM i_action ia LEFT JOIN i_attribute_value iav ON iav.ia_action_seq = ia.action_seq LEFT JOIN shared_action_attribute ON sactatt_sact_code = sact_code AND sactatt_sat_type = sat_type WHERE ia.deleted = 'A' ) SELECT action_seq , action_date , action_code , staff_seq , MIN (first_attribute) AS first_attribute , MIN (second_attribute) AS second_attribute FROM ungrouped_data GROUP BY action_seq , action_date , action_code , staff_seq ;
There are other alternatives for special cases, but none of them work in this particular case.
-
Hello
I wrote the following example query,
SELECT a.acc_rk
A,.
b,
c
WHERE a.acc_rk = b.acc_rk
AND b.acc_rk = c.acc_rk;
Now I need to change equi join left join
I have the left join table 'a' with table "b".
and again, I have to make tables of left join 'b' and 'c '.
Can someone tell me how to do this.
Thank you
Radha KAJ99 wrote:
SELECT a.acc_rk FROM (a LEFT JOIN b ON a.acc_rk = b.acc_rk) LEFT JOIN c ON b.acc_rk = c.acc_rk;
There is no need for media. ;)
SELECT a.acc_rk FROM a LEFT JOIN b ON a.acc_rk = b.acc_rk LEFT JOIN c ON b.acc_rk = c.acc_rk;
-
LEFT JOIN increases the number of lines
Hi guys,.
I had a problem, my left join retrieves multiple values. I know he has only 252 in there that correspond to the place where
condition. If I use the table in a left join with the same condition where my row count increases.
-1176 lines
Select count (erg_ID) of
MySchema. T_STA_ERG sta_erg
INNER JOIN T_MEN hoechst
ON sta_erg. PARAMETER = hoechst. PARAMETER
AND sta_erg. JAHR = 2014
where sta_erg. MESSERG_KNG = 'A' AND sta_erg. MESSERG_ALPHA IN ('03 ") and sta_erg. NORM_MESS is null
-252 lines
Select distinct erg_ID myschema. T_STA_ERG sta_erg where sta_erg. MESSERG_KNG = 'A' AND sta_erg. MESSERG_ALPHA IN ('03 ") and sta_erg. NORM_MESS is null
any clue´s how I can build in conditions in my join which would not increase the results of the line?
Why not just an inner join then?
-
Outer joins to the left... Please help!
Hmm. Ive had trouble with this one for a while and have read and re-read docs etc.
IM under 11.2 XE and its Apex 4.2 application...
Its a simple query with outer joins... but I can't simply to do it right after several hours of trying! ID be very grateful if anyone can lend a hand...
For the query below. I am looking for the following:
Complete list of entities, a sum of the values if they exist (and obviously empty if they don't), filtered by a table of SUM choice only the records that match a value in the lookup table.
Select
e.ENTITY as ENTITY,
Sum (p.amount) as Forecast_Income,
Sum (BR.bri_credit) as Actual_Income,
Sum (BR.bri_debit) as Actual_Expenses
Of
e entities
LEFT OUTER JOIN payments p
WE (e.entity_id = p.entity_id)
LEFT OUTER JOIN pmt_stat_lookup ps
WE (p.status_id = ps.status_id
and ps.forecast = 'Y')
LEFT OUTER JOIN bri_recon br
WE (e.entity_id = br.entity_id)
E.entity group
My problem with the join where I join p.status_id for ps.status_id and ps.forecast = 'Y '... If I run the above statement, it seems does not account for this filter criteria and summarizes all records of PAYMENTS, regardless of... If I move outside the outer join clause, it only brings back the records of PAYMENTS where there is a join. Ive tried to join in a different order etc... but in vain...
If someone could help, Id be very grateful.
Kind regards
Richard
Hello Richard
Try this query:
SELECT e.entity as ENTITY
sum (PO.amount) as Forecast_Income
sum (br.bri_credit) as Actual_Income
sum (br.bri_debit) as Actual_Expenses
Of
E ENTITIES
LEFT OUTER
JOIN (SELECT P.entity_id )
P.amount
PAYMENTS P
JOIN PMT_STAT_LOOKUP PS
ON p.status_id = ps.status_id
AND ps.forecast = 'Y '.
) IN.
WE e.entity_id = in. entity_id
LEFT OUTER
JOIN BR BRI_RECON
ON e.entity_id = br.entity_id
E.entity GROUP
;
I hope it helps.
Best regards, David
Post edited by: David Berger
-
How to avoid the union and use a left join
Hello
I have the following tables
{code}
create the table mainTable
(col1 varchar2 (2) varchar2 (2) col2, col3 varchar2 (2), col4 varchar2 (2));
create table secTable
(col1 varchar2 (2));
create the table secTable1
(col2 varchar2 (2));
create the table secTable2
(col3 varchar2 (2));
Insert in maintable
values ('a', 'b', 'this,' would be ');
Insert in maintable
values (', 'o', 'n', 'p');
insert into secTable
values ('a');
insert into secTable1
values ('b');
insert into secTable2
values ('c');
commit;
{code}
I made a request, drank, I would use join without clause union how can I change to receive the same result as the following query?
{code}
Select t1.*, 'bad value' | T1.Col1 from maintable t1 T1.Col1 left join t2 = t2.col1 sectable where t2.col1 is null Union of all the Select t1.*, 'bad value' | T1.col2 from maintable t1 Join t2 left on t1.col1 = t2.col2 sectable1 where t2.col2 is null Union of all the Select t1.*, 'bad value' | T1. COL3 from maintable t1 Join t2 left on t1.col1 = t2.col3 sectable2 where t2.col3 is null {code}
Thank you
Coco
Like this?:
SQL > SELECT col1, col2, col3, col4, xvalue
2 from (SELECT m0.*
3, NVL (t0.col1, 'bad value t0' | m0.col1) t0_col1
4, NVL (t1.col2, 'bad value t1' | m0.col2) t0_col2
5, NVL (t2.col3, 'bad value t2' | m0.col3) t0_col3
Maintable m0 6
LEFT OUTER JOIN sectable t0 7
8. WE (m0.col1 = t0.col1
9 AND t0.col1 IS NULL)
10 LEFT OUTER JOIN sectable1 t1
11. WE (m0.col1 t1.col2 =
12 AND t1.col2 IS NULL)
13 LEFT OUTER JOIN t2 sectable2
14. WE (m0.col1 = t2.col3
15 t2.col3 AND IS NULL))
16 UNPIVOT INCLUDES NULL values (xvalue wrongly IN (t0_col1, t0_col2, t0_col3))
17 ORDER BY xvalue
18.
COL1 COL2 COL3 COL4 XVALUE
------ ------ ------ ------ ---------------------------------------------------
a b c d bad value t0 a
m o n p bad value m t0
a b c d bad value t1 b
m o n p o t1 value of evil
a b c d bad value t2 c
m o n p value t2 n bad
6 selected lines.
-
Left join of the two tables and multiple values into a single value separated by commas
Hello
I have following tables with their structures and their data as below.
CREATE TABLE 'BETODI '. "" BETINFO ".
(
VARCHAR2 (8 BYTE) "CURRENTPRESS."
ENABLE 'TYPEIDCONTAINER' VARCHAR2 (30 BYTE) NOT NULL
)
INSERT INTO Betinfo (Currentpress, typeidcontainer) VALUES ('A24G', 'PMC');
INSERT INTO Betinfo (Currentpress, typeidcontainer) VALUES ('A24D', 'Pensky-MARTENS');
INSERT INTO Betinfo (Currentpress, typeidcontainer) VALUES ("A25D", "CMP");
INSERT INTO Betinfo (Currentpress, typeidcontainer) VALUES ('A25G', 'PMC');
INSERT INTO Betinfo (Currentpress, typeidcontainer) VALUES ('A26D', 'PMC');
INSERT INTO Betinfo (Currentpress, typeidcontainer) VALUES ('A26G', 'PMC');
INSERT INTO Betinfo (Currentpress, typeidcontainer) VALUES ("A32G", "V-BFC3");
INSERT INTO Betinfo (Currentpress, typeidcontainer) VALUES ('A32D', "V-BFC2");
CREATE TABLE 'BETODI '. "" BETMASTER ".
(
ACTIVATE THE "CUREPRESS" TANK (5 BYTES) NOT NULL,
ACTIVATE THE "TYPE" VARCHAR2 (5 BYTE) NOT NULL,
NUMBER (5.0) "LASTPCIRIM".
)
INSERT INTO BetMaster (Curepress, type, lastpcirim) VALUES ('A24', '45 M 8', 15);
INSERT INTO BetMaster (Curepress, type, lastpcirim) VALUES ('A25', 42 16', 15);
INSERT INTO BetMaster (Curepress, type, lastpcirim) VALUES ("A26", 16' 45, 15);
INSERT INTO BetMaster (Curepress, type, lastpcirim) VALUES ("A27", '45 M 34', 16);
INSERT INTO BetMaster (Curepress, type, lastpcirim) VALUES ('A28', '45 M 34', 16);
INSERT INTO BetMaster (Curepress, type, lastpcirim) VALUES ('A29', '45 M 34', 16);
INSERT INTO BetMaster (Curepress, type, lastpcirim) VALUES ('A30', '45MCH', 15);
INSERT INTO BetMaster (Curepress, type, lastpcirim) VALUES ("A31", "45MCH", 16);
INSERT INTO BetMaster (Curepress, type, lastpcirim) VALUES ('A32', '45MCH', 16);
INSERT INTO BetMaster (Curepress, type, lastpcirim) VALUES ('A33', '45MCH', 16);
INSERT INTO BetMaster (Curepress, type, lastpcirim) VALUES ("A34", "45MCH", 16);
These two tables have left join as
BETMASTER. CUREPRESS = substr (BETINFO. CURRENTPRESS, 1, 3)
now I want to have the data in the two tables with fields Curepress, Lastpcirim, typeidcontainer.
Also something like
Make a group of typeidcontainer if this value is greater than 1 by press separated the values of semicolon (;)
So, for example above, we should be given as
A24 PMC 15; PENSKY-MARTENS
A25 15 PMC
A26 15 PMC
A27 16 (NULL)
A28 16 (NULL)
A30 15 (NULL)
A31 16 (NULL)
A32 16 BFC2-V; V BFC3
A33 16 (NULL)
A34 16 (NULL)
How could do?
My current request is as
Select distinct Curepress, lastpcirim, typeidcontainer
BETMASTER STD left join INF BETINFO
on the trim (STD. CUREPRESS) = substr (trim (INF. CURRENTPRESS), 1, 3)
but I am unable to get the values separated by commas.
Any help would be appreciated.
Thank you
Mahesh.
Hi, Mahesh,
If you want to only 1 row of output for each distinct combination of currentpress and lastpcirim? This sounds like a job for GROUP BY.
And you want the row to contain a list of all different typidcontainers-delimited? This sounds like a job for the aggregate LISTAGG function.
WITH joined_data AS
(
SELECT DISTINCT
MST.curepress, mst.lastpcirim, inf.typeidcontainer
OF betmaster STD
LEFT JOIN betinfo ON TRIM (mst.curepress) inf = SUBSTR (TRIM (inf.currentpress)
1
3
)
)
SELECT curepress, lastpcirim
LISTAGG (typeidcontainer, ',')
THE Group (ORDER BY typeidcontainer) AS container_list
OF joined_data
Curepress GROUP, lastpcirim
;
Unfortunately, you can't say LISTAGG (DISTINCT ...), so you should always get the separate containers how you already are. (Note that the subquery is just what you posted).
Thanks for posting the CREATE TABLE and INSERT statements; It is very useful. Don't forget to tell what version of Oracle you are using. LISTAGG was new in Oracle 11.2.
Why not add CHECK constraints (and perhaps triggers) to your tables, so that curepress and currentpress are not stored with the head or trailing spaces? Then you wouldn't need to use the PAD in queries like this, and your code would be simpler and more effective.
-
Seen the lock of brain on Left Join
Always on Oracle 11.2.0.1.0
Examples of data
So, here is my SQL that does not work well:CREATE TABLE theEmails ( RID NUMBER(10) PRIMARY KEY, employee NUMBER(6), emailType VARCHAR2(4), emailAddress VARCHAR2(50), emailActive number(1) ) ; INSERT INTO theEmails VALUES(778437,231509,'OTHR','[email protected]',0); INSERT INTO theEmails VALUES(847168,231509,'WRK1','[email protected]',0); INSERT INTO theEmails VALUES(784690,231509,'OTHR','[email protected]',1); INSERT INTO theEmails VALUES(1459884,239868,'OTHR','[email protected]',0); INSERT INTO theEmails VALUES(1485781,239868,'OTHR','[email protected]',1); INSERT INTO theEmails VALUES(1485782,239868,'WRK1','[email protected]',0); INSERT INTO theEmails VALUES(1501028,231509,'WRK1','[email protected]',1); INSERT INTO theEmails VALUES(1492288,239868,'WRK1','[email protected]',1); INSERT INTO theEmails VALUES(1456785,239865,'OTHR','[email protected]',0); INSERT INTO theEmails VALUES(1489166,239865,'WRK1','[email protected]',0); INSERT INTO theEmails VALUES(1482668,239865,'WRK1','[email protected]',0); INSERT INTO theEmails VALUES(1482667,239865,'OTHR','[email protected]',1); CREATE TABLE thePeople ( employee NUMBER(6), recordNum NUMBER(2), lastName VARCHAR2(20), firstName VARCHAR2(20) ) ; INSERT INTO thePeople VALUES(231509,0,'P','Michelle'); INSERT INTO thePeople VALUES(239868,0,'K','Dan'); INSERT INTO thePeople VALUES(239865,0,'T','Mary');
AND this:SELECT p.employee, p.recordNum, p.lastName, p.firstName, e.emailaddress, e.emailactive, e.emailtype FROM thePeople p LEFT JOIN theemails e ON p.employee = e.employee WHERE (e.emailactive = 1) AND (e.emailtype = 'WRK1') AND (e.emailaddress LIKE '%school.edu') ;
Both give me this:SELECT p.employee, p.recordNum, p.lastName, p.firstName, e.emailaddress, e.emailactive, e.emailtype FROM thePeople p LEFT JOIN theemails e ON p.employee = e.employee WHERE (e.emailactive = 1 OR e.emailactive IS NULL) AND (e.emailtype = 'WRK1' OR e.emailtype IS NULL) AND (e.emailaddress LIKE '%school.edu' OR e.emailaddress IS NULL)
employee | record | lastName. firstName. emailAddress. emailActive | emailType
231509 | 0 | P | Michelle | [email protected] | 1. WRK1
But what I need is the following:
employee | record | lastName. firstName. emailAddress. emailActive | emailType
239868 | 0 | K | Dan | | |
231509 | 0 | P | Michelle | [email protected] | 1. WRK1
239865 | 0 | T | Mary | |
Email from Dan is not included, because his "wrk1" email does not stop it school.edu
E-mail Mary is not included, because his 'wrk1' email is not active.
However, I need to come back with their numbers of name registration and employee.
Once it works, I'll add employee = number in the WHERE clause, because I only need to find a person both for the actual production.
What I am doing wrong?
Thank you!
MichelleHello
WHERE is the filtering actually record the selection.
In your case, you might want to include in your LEFT OUTER JOIN ON conditions:
SELECT p.employee, p.recordnum, p.lastname, p.firstname , e.emailaddress, e.emailactive, e.emailtype FROM thepeople p LEFT JOIN theemails e ON p.employee = e.employee AND e.emailactive = 1 AND e.emailtype = 'WRK1' AND e.emailaddress LIKE '%school.edu';
If you put them in your JOIN conditions actually join Chronogram of table theemails that adapt to the conditions of membership.
As you use LEFT [OUTER] JOIN unmatched chronogram thepeople table will be displayed even when the theemail table column appears with a NULL value in the output filesI hope that I was able to unlock your brain. :-)
Kind regards.
AlPublished by: Alberto Faenza on 25 October 2012 16:58
Additional note added -
Rewrite the query with joins, and group by
Hello
It's an interview question.
Table names: bookshelf_checkout
virtual library
And the join condition between these two tables is title
We need to rewrite under request without using the join condition and group by clause?
When I was in College, I read most of SELECT statements can be replaced by operations base SQL (DEFINE the OPERATORS). Now, I am rewriting the query with SET operators, but not able to get the exact result.SELECT b.title,max(bc.returned_date - bc.checkout_date) "Most Days Out" FROM bookshelf_checkout bc,bookshelf b WHERE bc.title(+)=b.title GROUP BY b.title;
Kindly help me on this.
Thank you
SuriSomething like that?
1 WITH books AS ( 2 SELECT 'title 1' title FROM dual UNION ALL 3 SELECT 'title 2' FROM dual UNION ALL 4 SELECT 'title 3' FROM dual ), 5 bookshelf AS ( 6 SELECT 'title 1' title, DATE '2012-05-01' checkout_date, DATE '2012-05-15' returned_date FROM dual UNION ALL 7 SELECT 'title 1' title, DATE '2012-05-16' checkout_date, DATE '2012-05-20' returned_date FROM dual UNION ALL 8 SELECT 'title 2' title, DATE '2012-04-01' checkout_date, DATE '2012-05-15' returned_date FROM dual ) 9 SELECT bs.title, MAX(bs.returned_date - bs.checkout_date) OVER (PARTITION BY title) FROM bookshelf bs 10 UNION 11 (SELECT b.title, NULL FROM books b 12 MINUS 13* SELECT bs.title, NULL FROM bookshelf bs) SQL> / TITLE MAX(BS.RETURNED_DATE-BS.CHECKOUT_DATE)OVER(PARTITIONBYTITLE) ------- ------------------------------------------------------------ title 1 14 title 2 44 title 3
Lukasz
-
Get the error invalid identifier in the left outer join
I wrote a query and get the invalid identifier error during execution:
the exact error is: ORA-00904: "D". "" ACCT_NO ": invalid identifier
This query is not able to access the parent table alias in the subquery of the left outer join.
This is the query:
SELECT D.PROD_DESC_TX, BASE. ASSET_NUM, BASE. PROD_ID, BASE.NAME
OF TABLE1 D
LEFT OUTER JOIN
(
SELECT ASSET_NUM, PROD_ID, B.SID
FROM TABLE2 E
JOIN IN-HOUSE TABLE3 HAS IT E.PROD_ID = A.ROW_ID
JOIN INTERNAL TABLE 4 C ON A.PAR_PROD_INT_ID = C.ROW_ID
INTERNAL TABLE5 JOIN B ON C.ROW_ID = B.PAR_ROW_ID
AND B.TYPE = 'VALUE '.
AND B.NAME = 'VALUE '.
WHERE E.ASSET_NUM = ((CASE WHEN LTRIM (RTRIM (D.BANK_NO)) = '021 ' THEN '021' ELSE ' 020' END) |) LTRIM (RTRIM (D.APPL_CD)). LTRIM (RTRIM (D.ACCT_NO)))
) BASE ON ((CASE WHEN LTRIM (RTRIM (D.BANK_NO)) = '021 ' THEN '021' ELSE ' 020' END) |) LTRIM (RTRIM (D.APPL_CD)). LTRIM (RTRIM (D.ACCT_NO))) = BASE. ASSET_NUM
WHERE D.BANK_NO = 'VALUE '.
AND D.APPL_CD = 'VALUE '.
AND D.ACCT_NO = 'VALUE '.
Edited by: user648525 13 Sep, 2011 01:21I can easily look at your request at this time (using my iPhone on a train).
But trying out this line in the WHERE (the one with the invalid reference) clause in the main query.
Who is... in the view of inline, use only the logic of the join (the clauses).
Overflow the filtering logic (WHERE clause) to the main request. You may need to select some additional inline view columns to make these references work.
-
Must use the nested left join syntax?
I am currently an editing view. The view has one with a followed left join to an inner join such as this:
Select
T1.a,
T2.b,
T3.c
Of
(select 1 as a Union double all the)
Select 2 as double
) t1
left join
(select 1A, 11 b of all the double union)
Select 2A, 12 b of the double
) t2
on t1.a = t2.a
Join
T3 (select 11 b, 14-c double)
on t2.b = t3.b;
/ * - Try out: -.
A, B AND C
------- ------- -------
1-11-14
*/
The inner join converts left join in an inner join "de facto"? should I use nested join syntax in order to get the other ranks:
Select
T1.a,
T2.b,
T3.c
Of
(select 1 as a Union double all the)
Select 2 as double
) t1
left join
(select 1A, 11 b of all the double union)
Select 2A, 12 b of the double
) t2
on t1.a = t2.a
Join
T3 (select 11 b, 14-c double)
on t2.b = t3.b;
/ * - Try out: -.
A, B AND C
------- ------- -------
1-11-14
*/Sorry, just saw your last join condition now:
on t2.b = t3.b;
This results in a JOIN INTERNAL to the EXTERNAL table JOINED to, will certainly lead to a result of diminshed set - not quite the same that forced an INNER JOIN, but the result is the same, Yes. So there are indeed of equivalence.
This, however, would not have resulted a reduced resultset:
on t1.a = t3.a;
But of course it is not possible, since the t3 has a column "a".
-
Hello
Can anyone suggest an idea to rewrite this query? This query takes longer than expected, probably
they way the application is made is not fair? Thanks in advance! I use 11g
SELECT brand_id, region_id, sku_base_id, desc_key, (SELECT MAX (cps.product_code) FROM cat_prod_sku_vw cps WHERE cps.brand_id = srl.brand_id AND cps.region_id = srl.region_id AND cps.brand_id = 4 AND cps.region_id = 0 AND ( cps.parent_cat_id || 'PROD' || cps.prod_base_id) = srl.desc_text) AS desc_text FROM sku_rgn_lang_vw srl WHERE brand_id = 4 AND region_id = 0 AND desc_key LIKE 'www_pcode_%' AND LENGTH (desc_text) > 6 AND desc_text LIKE 'CAT%' ;
You should look at this thread {message identifier: = 9360003} to get an idea of what other information is required.
However, I'd be inclined to re - write as an (external) join instead of a scalar select statement. Something more like:
SELECT srl.brand_id, srl.region_id, srl.sku_base_id, srl.desc_key, cps.desc_text FROM sku_rgn_lang_vw srl left join (SELECT brand_id, region_id, parent_cat_id||'PROD'||prod_base_id desc_text MAX (product_code) product_code FROM cat_prod_sku_vw cps WHERE brand_id = 4 AND region_id = 0 GROUP BY brand_id, region_id, parent_cat_id||'PROD'||prod_base_id) cps on cps.brand_id = srl.brand_id AND cps.region_id = srl.region_id AND cps.parent_cat_id||'PROD'||cps.prod_base_id = srl.desc_text WHERE srl.brand_id = 4 AND srl.region_id = 0 AND srl.desc_key LIKE 'www_pcode_%' AND LENGTH (cps.desc_text) > 6 AND cps.desc_text LIKE 'CAT%' ;
Although I'm not entirely sure, I had all the correct alias.
John
Maybe you are looking for
-
I want to buy the gold medal and a few game Iron Force beads. And I tried to buy too many times, but I just received this message to get in touch with www.Apple.com/support/iTunes/wow/. . I am now of the Russia, and I can buy music ITunes using my Ap
-
Download a pirated copy of Windows XP to reinstall?
I lost my original CD for Windows XP Home Edition from my other laptop and now it has problems, so I'm wondering, can I use a pirated copy, then use a genuine key, under the laptop? Will it be legitimate? Microsoft does not provide a download for the
-
Printer Photosmart 7520 claim code
I put my connection hop except that I am out of ink so I can't read the code of the claim. If not, how can I get that he gets to this process?
-
There is no error message is displayed.
-
PC freezes too, can't do anything
My computer is not yet a year old, and recently, there has been so much gel, only a few minutes after I start it. I can't open more than one program at a time with crashing them. I can't open chrome or firefox at all because they will plant as well,