Rewrite the left join
The following code work, but I would like to rewrite them to left join on style, how to do it
select *
from 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(+)
Hi,
Here's one way:
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
LEFT OUTER JOIN v_codeNumber f ON c.discipline_num = f.discipline_num
CROSS JOIN e
WHERE a.my_numb = '5582017' Conditions involving 2 (or more) tables go in ON clauses, not in the WHERE clause.
Conditions using
(+)become part of an outer join.
Tables (like e, above) that are not connected to other tables are probably mistakes; otherwise, they become CROSS JOINs, and appear after all the other joins.
Similar Messages
-
How can I add left join in the coding
I intend to dynamic add left jion in my java class.
so I write the coding like below:
incident_id = svc_yokyu_id(+)
(Both of them have been selected in the select sentence.)
However, when I run the app, it will throw a sql error.(ora-01416)
then I copy the error sql sentence and try to test it
QRSLT WHERE (incident_id = svc_yokyu_id(+))--this is a error sql sentence.
Finding that if I delete the left join,(+), the sql will run right,
or I modify the sql like
QRSLT WHERE incident_id in (Select svc_yokyu_id from xxfm_srl_sr_jc_relation xssjr where incident_id = xssjr.svc_yokyu_id(+)) , it run right as well.
according to bussiness requestion, I can't write the coding like above,
so, i don't know how to implement it.
and i hope to your advice. thxDoes a hand come out of the screen, grabbing you by the wrist, every time you try?
Just guessing…
Peter -
Hi Everyone,
Can someone pls shed some light on the situation below
I am understanding alot of what Michael and Rod wrote.... with my prev. post of LEFT JOIN and testing for
not null and doing a double Boolean OR etc.
- but- AM NOT understanding why the IS NOT NULL works, without the double boolean OR
Pls help... have to explain what left join means to user tomm. I'm going to demo the query below and
not the one with the double boolean OR bec. maybe too much info to present at one sitting. tx, sandra
=====================
the query below is left joining the STUDENT table to
HOLD table.
The HOLD table - contains rows for students who have holds on their record.
a student can have more than one hold (health, HIPAA, basic life saving course)
BUT, for this query: I'm only interested that a hold exists, so I'm choosing MAX on hold desc.
Selecting a MAX, helps me, bec. it reduces my join to a 1 to 1 relationship, instead of
1 to many relationship.
Before I posted this thread at all, the LEFT JOIN below testing for IS NOT NULL worked w/o
me having to code IS NOT NULL twice....
Is that because, what's happening "behind the scenes" is that a temporary table containing all max rows is being
created, for which Discoverer has no predefined join instructions, so it's letting me do a LEFT JOIN and have
the IS NOT NULL condition.
I would so appreciate clarification. I have a meeting on Tues, for which I have to explain LEFT JOINS to the user
and how they should create a query. I need to come up with rules.
If I feel "clear", I asked my boss to buy Camtasia videocast software to create a training clip for user to follow.
Also, if any Banner user would like me to email the DIS query to run on their machine, I would be glad to do so.
thx sooo much, Sandra
SELECT O100384.ACADEMIC_PERIOD, O100255.ID, O100384.ID, O100255.NAME, O100384.NAME, O100255.PERSON_UID, O100384.PERSON_UID, MAX(O100255.HOLD_DESC)
FROM ODSMGR.HOLD O100255, ODSMGR.STUDENT O100384
WHERE ( ( O100384.PERSON_UID = O100255.PERSON_UID(+) ) ) AND ( O100384.ACADEMIC_PERIOD = '200820' )
GROUP BY O100384.ACADEMIC_PERIOD, O100255.ID, O100384.ID, O100255.NAME, O100384.NAME, O100255.PERSON_UID, O100384.PERSON_UID
HAVING ( ( MAX(O100255.HOLD_DESC(+)) ) IS NOT NULL )
ORDER BY O100384.NAME ASCHi,
OK, I will try to explain this. When you outer join table B to table A then the rows in table A which do not match any rows in table B will returned with NULL in the columns from the table B.
Oracle uses the syntax ( +) for outer joins. Now if you add another condition using the ( +) syntax (as shown below) the condition will be processed before the table is joined. Therefore if table A does not match any rows in table B which have col2=1 then the row from table A will be returned with NULLs for the table B columns.
SELECT A.col1, B.col1
FROM A, B
WHERE A.col1 = B.col1( +)
AND B.col2( +)=1
Now, if the condition B.col2=1 was used instead then the condition would be processed after the join and therefore the rows from table A that were joined to table B but did not meet the condition would not be returned by the query.
This applies to a WHERE clause and to the HAVING clause, but with one exception. If you use the ( +) within a group function in a HAVING clause then the ( +) will have no affect because the condition must be processed after group by and group by can only be processed after the join. Therefore MAX(B.col2( +)) = 1 is processed after the join even through it uses the ( +) syntax.
You cannot use an OR or an IN with the ( +) syntax because the meaning of the OR in this situation is ambiguous, is the OR done before or after the join. A query with an OR or IN in an outer will fail with an Oracle ORA-01719 error. Discoverer recognises this situation and removes the ( +) so that the error does not occur. However, without the ( +) the conditions are processed after the join.
Using the ( +) with IS NULL, e.g. col2( +) IS NOT NULL works in the same way. You just have to remember that the col2( +) could be NULL as a result of the outer join and therefore if the condition is processed after the query then the IS NOT NULL will remove the outer joined rows.
Hope that is clear.
Rod West -
Testing for IS NOT NULL with left join tables
Dear Experts,
The query is showing the NULL rows in the query below....
Any ideas, advice please? tx, sandra
This is the sql inspector:
SELECT O100321.FULL_NAME_LFMI, O100321.ID, O100404.ID, O100321.ID_SOURCE
, O100404.NAME, O100321.PERSON_UID, O100404.PERSON_UID, O100404.VISA_TYPE
FROM ODSMGR.PERSON O100321
, ODSMGR.VISA O100404
WHERE ( ( O100321.PERSON_UID = O100404.PERSON_UID(+) ) ) AND ( O100404.VISA_TYPE(+) IS NOT NULL )Hi Everyone,
I am understanding alot of what Michael and Rod wrote.... I am just puzzled over the following:
the query below is left joining the STUDENT table to
HOLD table.
The HOLD table - contains rows for students who have holds on their record.
a student can have more than one hold (health, HIPAA, basic life saving course)
BUT, for this query: I'm only interested that a hold exists, so I'm choosing MAX on hold desc.
Selecting a MAX, helps me, bec. it reduces my join to a 1 to 1 relationship, instead of
1 to many relationship.
Before I posted this thread at all, the LEFT JOIN below testing for IS NOT NULL worked w/o
me having to code IS NOT NULL twice....
Is that because, what's happening "behind the scenes" is that a temporary table containing all max rows is being
created, for which Discoverer has no predefined join instructions, so it's letting me do a LEFT JOIN and have
the IS NOT NULL condition.
I would so appreciate clarification. I have a meeting on Tues, for which I have to explain LEFT JOINS to the user
and how they should create a query. I need to come up with rules.
If I feel "clear", I asked my boss to buy Camtasia videocast software to create a training clip for user to follow.
Also, if any Banner user would like me to email the DIS query to run on their machine, I would be glad to do so.
thx sooo much, Sandra
SELECT O100384.ACADEMIC_PERIOD, O100255.ID, O100384.ID, O100255.NAME, O100384.NAME, O100255.PERSON_UID, O100384.PERSON_UID, MAX(O100255.HOLD_DESC)
FROM ODSMGR.HOLD O100255, ODSMGR.STUDENT O100384
WHERE ( ( O100384.PERSON_UID = O100255.PERSON_UID(+) ) ) AND ( O100384.ACADEMIC_PERIOD = '200820' )
GROUP BY O100384.ACADEMIC_PERIOD, O100255.ID, O100384.ID, O100255.NAME, O100384.NAME, O100255.PERSON_UID, O100384.PERSON_UID
HAVING ( ( MAX(O100255.HOLD_DESC(+)) ) IS NOT NULL )
ORDER BY O100384.NAME ASC -
Hi guys,
I am not sure why the left join is not working.. when I try to use the and condition out of the left join condition. The database is relational but the requirement is for reporting. I have 2 user inputs and I should get the query results as explained below..
Let me explain with the sample data.
Table 1: PRTYPE (P)
TYPEID CLASSID
T001 CLS001
T001 CLS002
T001 CLS003
T002 CLS002
T003 CLS001
Table 2: EMPLOYEE (E)
EMPID NAME
E001 Joe
E002 Mark
E003 Lucy
Table 3: DETAILS (D)
EMPID CLASSID STATUS
E001 CLS001 NEW
E001 CLS002 DONE
E002 CLS001 NEW
E002 CLS004 NEW
Report1:
Input: PRTYPE.TYPEID = T001, EMPID = E001
Output:
E.NAME E.EMPID P.TYPEID A.CLASSID D.STATUS
JOE EMP001 T001 CLS001 NEW
JOE EMP001 T001 CLS002 DONE
JOE EMP001 T001 CLS003 BLANK
Report2:
PRTYPE.TYPEID = T003, EMPID = E003
E.NAME E.EMPID P.TYPEID A.CLASSID D.STATUS
LUCY E003 T003 CLS001 BLANK
LUCY E003 T003 CLS004 BLANK
When I use and condition in left join itself, it works but when I take it to the end of the sql using where p.typeid= T001 and E.EMPID = E001 I am not getting the left join results... It looks like Oracle doesn't like the condition at the end. I hope I am clear on explaining. Please share your experience on how to get it working..
Thank youHi,
Welcome to the forum!
user12118328 wrote:
I am not sure why the left join is not working.. when I try to use the and condition out of the left join condition. The database is relational but the requirement is for reporting. I have 2 user inputs and I should get the query results as explained below..
When I use and condition in left join itself, it works but when I take it to the end of the sql using where p.typeid= T001 and E.EMPID = E001 I am not getting the left join results... It looks like Oracle doesn't like the condition at the end. I hope I am clear on explaining. Please share your experience on how to get it working..I'm not sure what you mean. Why don't you post your query? You know, it will be easier for someone to tell you what you're doing wrong if they know what you're doing.
Are you saying that you get the correct results when you have a cerain condition in an outer join, but that you get the wrong results if you put the exact same condition in the WHERE-clause?
If so, keep the condition where it does what you want.
You can usually move INNER join conditions around like that, but never OUTER join conditions.
As Alex said, if you want help, post executable statement to create and populate your tables with a little sample data, as well as the output you want from that sample data, and your best attempt at a query. -
How to make a customize form with optional parameter and left join
Hi,
I am trying to make a report with a left join. I also want ot have optional parameters in it.
I am using the Create Reports From SQL Query method:
SQL is :
select ename, sal
from scott.emp left join scott.dept
on scott.emp.deptno = scott.dept.deptno
where sal >= :lower_sal
and sal <= :higher_sal;
However, if I do it this way, I those 2 parameters are not optional.... How can I make them optional?
If I use the Create Reports From Query Wizard method, I can make the parameters optional, but then I cannot do the left join...
Thanks,
Wilsonhi,
to catch null values on both sides I use:
where nvl(sal,0) >= nvl(:lower_sal,nvl(sal,0))
and nvl(sal,0) <= nvl(:higher_sal,9999999999)
otherwise there will be no rows in the report where the value of sal is null, even if there are no selection criteria entered.
regards Michael -
Hi,
I have a webi report wherein I use the custom SQL with left join. However when I run the query against the database( SQL Server 2005) I get the left join to work . But when I run the report using the same SQL I do not see the left join implemented on the report .
Wanted to have some guidance whether we do need to change anything on the Universe level/webi level to make the webi show the values with left join. By default even after having custom sql I am seeing inner join.
The procedures I have tried :
Universe Level: In Paramters Tab: Set ANSI_SQL to yes ( but it still does not work)
Thank you,
boe userLook at http://www.dagira.com/2010/08/17/handling-conditions-on-outer-joins/.
Regards,
Kuldeep -
Left join query with join of three tables
I'm trying to build a query which has me stumped. Most of the query is fairly straightforward but I've run into an issue I'm not sure how to solve.
Background:
We have actions stored in i_action.
We have the available attributes for each type of action. The available attributes for each action are described in shared_action_attribute. Each type of action may have up to three attributes or none at all.
We have the values stored for the attributes in i_attribute_value.
A written example:
We have a transfer action (action_code B4). The entry of the B4 action into i_action records the fact that the transfer occurred and the date on which it occurred. The available attributes for a transfer action are the receiving function code, the receiving unit number, and the transfer reason code. These available attribute types and their order are stored in shared_action_attribute. The actual values of the attributes for a specific transfer action are stored in i_attribute_value.
Now i_action and i_attribute_value can be directly linked through action_seq in i_action and ia_action_seq in i_attribute_value. A left join built between these two tables provides results for all actions (including actions which have no attributes) and attribute values (see query 1 below).
There are two issues. First, I only want the first two attributes. In order to specify the first two attributes, I also have to link i_attribute_value to shared_action_attribute (which is where the order is stored). I can build a simple query (without the left join) linking the three tables but then actions with no attributes would be excluded from my result set (see query 2 below).
The second issue is that I would actually like one row returned for each action with first_attribute and second_attribute as columns instead of two rows.
The final query will be used to create a materialized view.
Here are the tables and examples of what's stored in them:
TABLE i_action
Name Type
ACTION_SEQ NUMBER(10)
ACTION_DATE DATE
ACTION_CODE VARCHAR2(3)
DELETED VARCHAR2(1)
EXAMPLE ROWS
ACTION_SEQ ACTION_DATE ACTION_CODE DELETED
45765668 09-OCT-09 B2 A
45765670 09-OCT-09 BA A
45765672 09-OCT-09 B6 A
45765673 09-OCT-09 B4 A
45765674 09-OCT-09 G1 A
45765675 09-OCT-09 M3 A
TABLE i_attribute_value
Name Type
IA_ACTION_SEQ NUMBER(10)
SACTATT_SACT_CODE VARCHAR2(3)
SACTATT_SAT_TYPE VARCHAR2(3)
VALUE VARCHAR2(50)
EXAMPLE ROWS
IA_ACTION_SEQ SACTATT_SACT_CODE SACTATT_SAT_TYPE VALUE
45765668 B2 ACO 37B
45765670 BA ROA D
45765670 BA ROR P
45765672 B6 CAT C
45765673 B4 RFC E
45765673 B4 TRC P
45765673 B4 RUN 7
45765674 G1 SS 23567
45765674 G1 ASG W
TABLE shared_action_attribute
Name Type
SACT_CODE VARCHAR2(3)
SAT_TYPE VARCHAR2(3)
ORDER NUMBER(2)
TITLE VARCHAR2(60)
EXAMPLE ROWS
SACT_CODE SAT_TYPE ORDER TITLE
B2 ACO 1 Office code
BA ROR 1 Reason for reopen
BA ROA 2 Reopen authority
B6 CAT 1 Category
B4 RFC 1 Receiving function code
B4 RUN 2 Receiving unit code
B4 TRC 3 Transfer reason code
G1 SS 1 Staff sequence
G1 ASG 2 Assignment reason
QUERY 1:
This is my current query along with its results. Most of it is straightforward select but one column is populated using the last_value analytical function (thanks to you guys). The last column in the below view stores the attribute value. What I want is to replace that single column with two columns named first_attribute and second_attribute and to eliminate any 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 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 ia LEFT JOIN i_attribute_value iav
ON iav.ia_action_seq = ia.action_seq
WHERE ia.deleted = 'A';
ACTION_SEQ ACTION_DA COD STAFF_SEQ VALUE
45765668 09-OCT-09 B2 67089 37B
45765670 09-OCT-09 BA 67089 D
45765670 09-OCT-09 BA 67089 P
45765672 09-OCT-09 B6 67089 C
45765673 09-OCT-09 B4 67089 E
45765673 09-OCT-09 B4 67089 P
45765673 09-OCT-09 B4 67089 7
45765674 09-OCT-09 G1 23567 23567
45765674 09-OCT-09 G1 23567 W
45765675 09-OCT-09 M3 23567
QUERY 2:
This query limits to the first two attributes but it also drops actions which have no attributes and it still creates multiple rows for each action instead of a single row with two columns for the 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 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 shared_action_attribute saa, ims_action ia, 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 COD VALUE
45765668 09-OCT-09 B2 67089 37B
45765670 09-OCT-09 BA 67089 D
45765670 09-OCT-09 BA 67089 P
45765672 09-OCT-09 B6 67089 C
45765673 09-OCT-09 B4 67089 E
45765673 09-OCT-09 B4 67089 7
45765674 09-OCT-09 G1 23567 23567
45765674 09-OCT-09 G1 23567 W
I found this pretty complex to try to write out - I hope I've been clear.
Thanks so much!Ok, here's more information with a simplified question. I figured out the syntax for building my query with the three tables. My final query returns multiple rows (multiple attributes per action). Instead of multiple rows, I'd like two columns (first_attribute, and second_attribute) in a single row (I only need the first two attributes).
Here's the action table:
CREATE TABLE I_ACTION
ACTION_SEQ NUMBER(10) NOT NULL,
ACTION_DATE DATE,
ACTION_CODE VARCHAR2(3 BYTE) NOT NULL,
DELETED VARCHAR2(1 BYTE),
);With the following rows added:
Insert into I_ACTION (ACTION_SEQ, ACTION_DATE, ACTION_CODE, DELETED)
Values (45765668, '09-oct-2009', 'B2', 'A');
Insert into I_ACTION (ACTION_SEQ, ACTION_DATE, ACTION_CODE, DELETED)
Values (45765670, '09-oct-2009', 'BA', 'A');
Insert into I_ACTION (ACTION_SEQ, ACTION_DATE, ACTION_CODE, DELETED)
Values (45765672, '09-oct-2009', 'B6', 'A');
Insert into I_ACTION (ACTION_SEQ, ACTION_DATE, ACTION_CODE, DELETED)
Values (45765673, '09-oct-2009', 'B4', 'A');
Insert into I_ACTION (ACTION_SEQ, ACTION_DATE, ACTION_CODE, DELETED)
Values (45765674, '09-oct-2009', 'G1', 'A');
Insert into I_ACTION (ACTION_SEQ, ACTION_DATE, ACTION_CODE, DELETED)
Values (45765675, '09-oct-2009', 'M3', 'A');
COMMIT;The attribute table is:
CREATE TABLE I_ATTRIBUTE_VALUE
IA_ACTION_SEQ NUMBER(10) NOT NULL,
SACTATT_SACT_CODE VARCHAR2(3 BYTE) NOT NULL,
SACTATT_SAT_TYPE VARCHAR2(3 BYTE) NOT NULL,
VALUE VARCHAR2(50 BYTE),
);With the following rows:
Insert into I_ATTRIBUTE_VALUE (IA_ACTION_SEQ, SACTATT_SACT_CODE, SACTATT_SAT_TYPE, VALUE)
Values (45765668, 'B2', 'ACO', '37B');
Insert into I_ATTRIBUTE_VALUE (IA_ACTION_SEQ, SACTATT_SACT_CODE, SACTATT_SAT_TYPE, VALUE)
Values (45765670, 'BA', 'ROR', 'P');
Insert into I_ATTRIBUTE_VALUE (IA_ACTION_SEQ, SACTATT_SACT_CODE, SACTATT_SAT_TYPE, VALUE)
Values (45765670, 'BA', 'ROA', 'D');
Insert into I_ATTRIBUTE_VALUE (IA_ACTION_SEQ, SACTATT_SACT_CODE, SACTATT_SAT_TYPE, VALUE)
Values (45765672, 'B6', 'CAT', 'C');
Insert into I_ATTRIBUTE_VALUE (IA_ACTION_SEQ, SACTATT_SACT_CODE, SACTATT_SAT_TYPE, VALUE)
Values (45765673, 'B4', 'RFC', 'E');
Insert into I_ATTRIBUTE_VALUE (IA_ACTION_SEQ, SACTATT_SACT_CODE, SACTATT_SAT_TYPE, VALUE)
Values (45765673, 'B4', 'RUN', '7');
Insert into I_ATTRIBUTE_VALUE (IA_ACTION_SEQ, SACTATT_SACT_CODE, SACTATT_SAT_TYPE, VALUE)
Values (45765673, 'B4', 'TRC', 'P');
Insert into I_ATTRIBUTE_VALUE (IA_ACTION_SEQ, SACTATT_SACT_CODE, SACTATT_SAT_TYPE, VALUE)
Values (45765674, 'G1', 'SS', '23567');
Insert into I_ATTRIBUTE_VALUE (IA_ACTION_SEQ, SACTATT_SACT_CODE, SACTATT_SAT_TYPE, VALUE)
Values (45765674, 'G1', 'ASG', 'W');
COMMIT;And finally, the shared table:
CREATE TABLE SHARED_ACTION_ATTRIBUTE
SACT_CODE VARCHAR2(3 BYTE) NOT NULL,
SAT_TYPE VARCHAR2(3 BYTE) NOT NULL,
TITLE VARCHAR2(25 BYTE) NOT NULL,
DISPLAY_ORDER NUMBER(2) NOT NULL
);With the following rows:
Insert into SHARED_ACTION_ATTRIBUTE (SACT_CODE, SAT_TYPE, TITLE, DISPLAY_ORDER)
Values ('B4', 'RFC', 'Y', 'Rcv. Function Code', 1);
Insert into SHARED_ACTION_ATTRIBUTE (SACT_CODE, SAT_TYPE, TITLE, DISPLAY_ORDER)
Values ('B6', 'CAT', 'Y', 'Category', 1);
Insert into SHARED_ACTION_ATTRIBUTE (SACT_CODE, SAT_TYPE, TITLE, DISPLAY_ORDER)
Values ('G1', 'SS', 'Y', 'Staff Name', 1);
Insert into SHARED_ACTION_ATTRIBUTE (SACT_CODE, SAT_TYPE, TITLE, DISPLAY_ORDER)
Values ('B2', 'ACO', 'Y', '"Other" Office Code', 1);
Insert into SHARED_ACTION_ATTRIBUTE (SACT_CODE, SAT_TYPE, TITLE, DISPLAY_ORDER)
Values ('B4', 'RUN', 'Y', 'Receiving Unit Number', 2);
Insert into SHARED_ACTION_ATTRIBUTE (SACT_CODE, SAT_TYPE, TITLE, DISPLAY_ORDER)
Values ('B6', 'LEP', 'N', 'LEP Issue/Sub Category', 2);
Insert into SHARED_ACTION_ATTRIBUTE (SACT_CODE, SAT_TYPE, TITLE, DISPLAY_ORDER)
Values ('B4', 'TRC', 'Y', 'Transfer Reason Code', 3);
Insert into SHARED_ACTION_ATTRIBUTE (SACT_CODE, SAT_TYPE, TITLE, DISPLAY_ORDER)
Values ('B6', 'NEP', 'N', 'NEP Issue', 3);
Insert into SHARED_ACTION_ATTRIBUTE (SACT_CODE, SAT_TYPE, TITLE, DISPLAY_ORDER)
Values ('G1', 'ASG', 'Y', 'Assignment Reason', 2);
Insert into SHARED_ACTION_ATTRIBUTE (SACT_CODE, SAT_TYPE, TITLE, DISPLAY_ORDER)
Values ('B2', 'MSN', 'S', 'Machine Serial Number', 3);
Insert into SHARED_ACTION_ATTRIBUTE (SACT_CODE, SAT_TYPE, TITLE, DISPLAY_ORDER)
Values ('BA', 'ROR', 'Y', 'Reopen Reason', 1);
Insert into SHARED_ACTION_ATTRIBUTE (SACT_CODE, SAT_TYPE, TITLE, DISPLAY_ORDER)
Values ('BA', 'ROA', 'Y', 'Reopen Authority', 2);
COMMIT;Now, this is my current query (it's changed from my first post):
SELECT ia.action_seq, ia.ici_charge_inquiry_seq, ia.action_date,
ia.serial_number, ia.reporting_office, ia.reporting_function,
ia.reporting_unit, ia.action_code, ia.machine_serial_number,
NVL
(LAST_VALUE (CASE
WHEN ia.action_code = 'G1'
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.serial_number, 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 ims_action ia
LEFT JOIN ims_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';Which gives me the following results:
ACTION_SEQ ACTION_DA ACT STAFF_SEQ FIRST_ATTRIBUTE SECOND_ATTRIBUTE
45765668 09-OCT-09 B2 67089 37B
45765670 09-OCT-09 BA 67089 D
45765670 09-OCT-09 BA 67089 P
45765672 09-OCT-09 B6 67089 C
45765673 09-OCT-09 B4 67089 E
45765673 09-OCT-09 B4 67089 7
45765673 09-OCT-09 B4 67089
45765674 09-OCT-09 G1 23567 W
45765674 09-OCT-09 G1 23567 23567
45765675 09-OCT-09 M3 23567 The result I WANT is similar but I want the two separate attribute columns on one row as such:
ACTION_SEQ ACTION_DA ACT STAFF_SEQ FIRST_ATTRIBUTE SECOND_ATTRIBUTE
45765668 09-OCT-09 B2 67089 37B
45765670 09-OCT-09 BA 67089 P D
45765672 09-OCT-09 B6 67089 C
45765673 09-OCT-09 B4 67089 E 7
45765674 09-OCT-09 G1 23567 23567 W
45765675 09-OCT-09 M3 23567 Thanks so much! -
Select from 2 tables with common key and than left join(unique result)
Hi,
I have 2 tables that have a common id (Customer_id) and I have a third table which contain her key consist of 3 column ( Customer_id , rms_customer_id,billind_tree_id) and have the manager_name field
I am using a left join because not all the customers have a manager.
I need to take only the manager_name field from the left join but the problem is that I am not getting a unique name results because the customer_id is not the primary key(only part of )
so I have use the following :
left join
( select * from ACCOUNT_Manager am where
rms_customer_id <= all (select rms_customer_id from ACCOUNT_Manager am2 where
am2.customer_id = am.customer_id )) am
on c.ID = am.CUSTOMER_ID (C is one of the first 2 tables with the ID as key)
Is there anyway more efficient of doing it ?
ThanksPlease consider the following when you post a question. This would help us help you better
1. New features keep coming in every oracle version so please provide Your Oracle DB Version to get the best possible answer.
You can use the following query and do a copy past of the output.
select * from v$version 2. This forum has a very good Search Feature. Please use that before posting your question. Because for most of the questions
that are asked the answer is already there.
3. We dont know your DB structure or How your Data is. So you need to let us know. The best way would be to give some sample data like this.
I have the following table called sales
with sales
as
select 1 sales_id, 1 prod_id, 1001 inv_num, 120 qty from dual
union all
select 2 sales_id, 1 prod_id, 1002 inv_num, 25 qty from dual
select *
from sales 4. Rather than telling what you want in words its more easier when you give your expected output.
For example in the above sales table, I want to know the total quantity and number of invoice for each product.
The output should look like this
Prod_id sum_qty count_inv
1 145 2 5. When ever you get an error message post the entire error message. With the Error Number, The message and the Line number.
6. Next thing is a very important thing to remember. Please post only well formatted code. Unformatted code is very hard to read.
Your code format gets lost when you post it in the Oracle Forum. So in order to preserve it you need to
use the {noformat}{noformat} tags.
The usage of the tag is like this.
<place your code here>\
7. If you are posting a *Performance Related Question*. Please read
{thread:id=501834} and {thread:id=863295}.
Following those guide will be very helpful.
8. Please keep in mind that this is a public forum. Here No question is URGENT.
So use of words like *URGENT* or *ASAP* (As Soon As Possible) are considered to be rude. -
LEFT JOINING with a string?
I'm viewing code where the person left joined a column with a string.
lu_ods.lookup_type(+) = 'FORX_ORDERPRO_ACTION'Is this just bad coding. Is there any reason why someone would do this?
Doesn't
lu_ods.lookup_type = 'FORX_ORDERPRO_ACTION'work just as well?As Sentinel said, it can change the result if you are left joining between two tables:
with a as (
select 1 num, 'a' col from dual UNION ALL
select 2 num, 'b' col from dual UNION ALL
select 3 num, 'c' col from dual
b as (
select 1 num, 'a' col from dual UNION ALL
select 4 num, 'e' col from dual UNION ALL
select 5 num, 'f' col from dual
select *
from a, b
where a.num(+) = b.num
and a.num(+) = 1;
NUM COL NUM COL
1 a 1 a
5 f
4 e
3 rows selectedWithout the left join on the number, we lose results:
with a as (
select 1 num, 'a' col from dual UNION ALL
select 2 num, 'b' col from dual UNION ALL
select 3 num, 'c' col from dual
b as (
select 1 num, 'a' col from dual UNION ALL
select 4 num, 'e' col from dual UNION ALL
select 5 num, 'f' col from dual
select *
from a, b
where a.num(+) = b.num
and a.num = 1;
NUM COL NUM COL
1 a 1 a
1 rows selected -
Hi all,
Hoping you can help me with my SQL query. I want to do a
simple LEFT JOIN between two tables...fair enough right? Yeah,
well, it works and all rows are returned from the LEFT table even
if there are nmo matching rows in the RIGHT table.
BUT, when I apply a few conditions using WHERE after this
LEFT JOIN the whole point of the LEFT JOIN seems to not work. I
only get rows from the LEFT where they match in the RIGHT.
For example...this following code works and returns all LEFT
rows despite there not being a match in the RIGHT.
<cfquery name="getSchemaFields" datasource="#request.dsn#"
username="#request.username#" password="#request.password#">
SELECT schema_#getSchemas.schema_token#_fields.*,
fields_content.*
FROM schema_#getSchemas.schema_token#_fields
LEFT JOIN fields_content
ON schema_#getSchemas.schema_token#_fields.field_type_uuid =
fields_content.field_content_field_uuid
</cfquery>
But, what I really need is to apply some conditions on the
rows I want from the right table based on an ID like as follows...
<cfquery name="getSchemaFields" datasource="#request.dsn#"
username="#request.username#" password="#request.password#">
SELECT schema_#getSchemas.schema_token#_fields.*,
fields_content.*
FROM schema_#getSchemas.schema_token#_fields
LEFT JOIN fields_content
ON schema_#getSchemas.schema_token#_fields.field_type_uuid =
fields_content.field_content_field_uuid
WHERE fields_content.field_content_item_id = <cfqueryparam
cfsqltype="cf_sql_integer" value="#getSchemas.item_id#" />
OR fields_content.field_content_item_id = NULL
AND fields_content.field_content_item_uuid = <cfqueryparam
cfsqltype="cf_sql_varchar" value="#getSchemas.item_uuid#" />
OR fields_content.field_content_item_uuid = NULL
</cfquery>
Now I don't get any errors but what I do get is rows from the
left ONLY when they match rows in the RIGHT. I still need all the
rows from the LEFT despite this. The WHERE conditions seems to stop
this happening.
I know it's probably blindingly simple but I just can't get
my head around it and I'm pulling my hair out about it!
Hope somebody can assist me in this.
Many thanks in advance!!
Mikey.> Now I don't get any errors but what I do get is rows
from the left ONLY when
> they match rows in the RIGHT. I still need all the rows
from the LEFT despite
> this. The WHERE conditions seems to stop this happening.
You need to stop to think about how the query is being
executed.
FIRST the FROM recordset is created which is the result of
the join
statement. This will have all your "left" rows, and null-data
for
unmatched "right" rows.
THEN the WHERE clause is executed, which will filter out any
rows from the
recordset create in the FROM recordset.
So if yuor data is this:
TBL_FRUIT
FRT_ID, FRT_NAME
1, Apple
2, Banana
3, Cherry
TBL_STOCK (<sung>we have no bananas, we have no bananas
today</sung>)
STK_ID, FRT_ID, STK_LEVEL
1, 1, 12
2, 3, 100
And if you FROM clause is this:
from TBL_FRUIT F left outer join TBL_STOCK S on F.FRT_ID =
S.FRT_ID
You get a record set thus:
1, Apple, 12
2, Banana, null
3, Cherry, 100
Now if you have a WHERE filter, thus:
WHERE S.STK_LVEL > 0
You're going tobe filtering out the banana row, because it
DOESN'T have a
STK_LEVEL > 0
1, Apple, 12
3, Cherry, 100
You need to get all your filtering done on the STOCK table
*before*
left-joining it to the FRUIT table, if you want to have all
the fruit rows
in the final result.
Dan's detailed how to effect this.
Make sense?
Adam -
Getting One to Many Left Join to be in One Result Row...
11G APEX 4.2
At this time I don't have access to make a view or anything like that. Just select access to the data.
BEGIN
for i in (
SELECT CAA.NODE_NAME THENODE,CNF.FEATURE_TYPE CNFFT from CMS.CMS_APP_ASSIGN "CAA"
LEFT JOIN CMS.CMS_NODE_FEATURE "CNF" on CAA.NODE_NAME=CNF.NODE_NAME
WHERE CAA.NODE_APP = :WHICHAPP and CAA.REMOVE_DT is NULL)
LOOP
dbms_output.put_line(i.THENODE||'--'||i.CNFFT||'<BR>');
END LOOP;
END;
When this returns there is a row for each of the left joins.
server1--CPUCount<BR>
server1--ContactInfo<BR>
server1--ContactInfo<BR>
server1--ContactInfo<BR>
server2--CPUCount<BR>
What I would like to do it just have one row:
NODENAME
CPUCount
Contactinfo
Contact Info
Contact Info
ETC
server1
value1
value2
value4
c
ThanksHi,
Taking 1 column from N rows, and transforming that into N columns on 1 row is called Pivoting.
The forum FAQ has a page on this subject. See https://forums.oracle.com/message/9362005#9362005
While you're in the Forum FAQ, also see https://forums.oracle.com/message/9362002 -
How to implement left join for the below sql.
i need to perform left join between inventory table and purchase table and inventory table with sales table.
please let me know the syntax.
SELECT Tbl_ProductGroupMaster.ProdName, Tbl_ProductGroupMaster.UnitNam, Tbl_Inventory.CrDate, Tbl_Inventory.QtyOpStk, Tbl_Inventory.QtyClStk, Tbl_Inventory.ExcShortage, purchase.SumOfNUnit, sales.SumOfQty
FROM Tbl_ProductGroupMaster, Tbl_Inventory left join
[SELECT Tbl_PurchaseMain.idate as p_idate, Tbl_PurchaseSub.Proid as proid,
sum(Tbl_PurchaseSub.NUnit) AS SumOfNUnit
FROM Tbl_PurchaseMain, Tbl_PurchaseSub WHERE Tbl_PurchaseMain.GID=Tbl_PurchaseSub.gid
and format(cdate(Tbl_PurchaseMain.idate),'mm-dd-yyyy') between format(cdate('13-Sep-2009'),'mm-dd-yyyy') and
format(cdate('02-Oct-2009'),'mm-dd-yyyy') GROUP BY
Tbl_PurchaseMain.idate, Tbl_PurchaseSub.Proid]. AS purchase on (Tbl_Inventory.CrDate = purchase.p_idate)
left join
[SELECT Tbl_SalesMain.idate AS s_idate, Tbl_SalesSub.Proid, sum(Tbl_SalesSub.Qty) AS SumOfQty
FROM Tbl_SalesMain, Tbl_SalesSub
WHERE Tbl_SalesMain.SAID=Tbl_SalesSub.SAID And
format(cdate(Tbl_SalesMain.idate),'mm-dd-yyyy') Between format(cdate('13-Sep-2009'),'mm-dd-yyyy') And
format(cdate('02-Oct-2009'),'mm-dd-yyyy')
GROUP BY Tbl_SalesMain.idate, Tbl_SalesSub.Proid]. AS sales on (Tbl_Inventory.CrDate = sales.s_idate)
WHERE
Tbl_ProductGroupMaster.PROID=Tbl_Inventory.proid AND
Tbl_Inventory.PROID=purchase.proid and
Tbl_Inventory.PROID=sales.proid and
Tbl_Inventory.CrDate = '13-Sep-2009';
thanks,
VinodhCompletely unstested as you haven't given much information but do you mean something like this?
WITH purchase
AS ( SELECT Tbl_PurchaseMain.idate AS p_idate,
Tbl_PurchaseSub.Proid AS proid,
SUM (Tbl_PurchaseSub.NUnit) AS SumOfNUnit
FROM Tbl_PurchaseMain, Tbl_PurchaseSub
WHERE Tbl_PurchaseMain.GID = Tbl_PurchaseSub.gid
AND format (cdate (Tbl_PurchaseMain.idate), 'mm-dd-yyyy') BETWEEN format (
cdate('13-Sep-2009'),
'mm-dd-yyyy'
AND format (
cdate('02-Oct-2009'),
'mm-dd-yyyy'
GROUP BY Tbl_PurchaseMain.idate, Tbl_PurchaseSub.Proid),
sales
AS ( SELECT Tbl_SalesMain.idate AS s_idate,
Tbl_SalesSub.Proid,
SUM (Tbl_SalesSub.Qty) AS SumOfQty
FROM Tbl_SalesMain, Tbl_SalesSub
WHERE Tbl_SalesMain.SAID = Tbl_SalesSub.SAID
AND format (cdate (Tbl_SalesMain.idate), 'mm-dd-yyyy') BETWEEN format (
cdate('13-Sep-2009'),
'mm-dd-yyyy'
AND format (
cdate('02-Oct-2009'),
'mm-dd-yyyy'
GROUP BY Tbl_SalesMain.idate, Tbl_SalesSub.Proid)
SELECT Tbl_ProductGroupMaster.ProdName,
Tbl_ProductGroupMaster.UnitNam,
Tbl_Inventory.CrDate,
Tbl_Inventory.QtyOpStk,
Tbl_Inventory.QtyClStk,
Tbl_Inventory.ExcShortage,
purchase.SumOfNUnit,
sales.SumOfQty
FROM Tbl_ProductGroupMaster,
Tbl_Inventory LEFT OUTER JOIN purchase
ON Tbl_Inventory.CrDate = purchase.p_idate
LEFT OUTER JOIN sales
ON Tbl_Inventory.CrDate = sales.s_idate
WHERE Tbl_ProductGroupMaster.PROID = Tbl_Inventory.proid
AND Tbl_Inventory.PROID = purchase.proid
AND Tbl_Inventory.PROID = sales.proid
AND Tbl_Inventory.CrDate = '13-Sep-2009';Cheers
Ben
http://www.munkyben.wordpress.com -
Does anyone know possible reasons a left join in the forge would drop the entire record?
Hi All,
I'm trying to join some data on to my records in the forge using a left join and no matter what I do the presence of a record on the right of the join drops both records. The left record set is coming from a database and being cached before the join. The right is coming from CAS and is also being cached. The join key is being indexed in both caches.
I've tried numerous things including...
Renaming the record key property
Renaming the property I'm trying to add (both source and target)
Using a different CAS adapter (delimiter and native xml)
Checked casing of EVERYTHING.
I'm at my wit's end. Any help would be much appreciated.The left record is being dropped entirely? I've not heard of that before, but it will be due to data integrity. Try adding record adapters with direction=output and format=xml before the two separate feeds into join, and then one after the join - these will be written out to ./data/forge_output (or maybe ./data/processing), and you should hopefully be able to see something in the raw data that will explain why this is happening. Also, check the forge and dgidx logs (./logs/forges/Forge/Forge.log and ./logs/dgidxs/Dgidx/Dgidx.log) to see if there are messages about duplicate record keys.
Michael -
Rewrite the query with out joins and group by
Hi,
This was an interview question.
Table Names: bookshelf_checkout
bookshelf
And the join condition between these two tables is title
We need to rewrite below query without using join condition and group by clause ?
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;When I was in college, I read that most of the SELECT statements can be replaced by basic SQL operations (SET OPERATORS). Now I am trying to rewrite the query with SET operators but not able to get the exact result.
Kindly help me on this.
Thanks,
SuriSomething like this?
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 3Lukasz
Maybe you are looking for
-
Error in adding a group in ESSCMD
<p>hi i am having this error message when i tried to add thisgroup</p><p>the ESSCMD was</p><p><span style=" color: #0000ff;"><b>IMPORT_GROUP,Australia_NZ,<br><MEMBERS><br>User name1,2,2<br>User name2</b> <b>,2,1<br>User name3</b> <b>,2,1<br>User name
-
How to find out the T-code of a program
Hi Experts, I want to find out the T-codes of some programs. For that I am passing the program name in TSTC table and I am getting the T-code. But TSTC table gives T-code for only main program and not for the includes. Is there any way to find o
-
I'm trying to demo Flash CC 2014 and have downloaded it and installed it. When I try to launch it, it quits immediately. Looking for help. Running on MBP Pro 15 Retina, Late 2013 with OS 10.9.4 I have the CC 6 Full suite of apps installed.
-
My password ror outlook works on pc but not on iphone4s
My password for outlook works on pc but not on iphone4s
-
Simulating Database Corruption
Any one have any reliable methods of simulating database corruption? (I know, use SQL Server.) I want to test various methods of monitoring for both physical and logical corruption. I have used tried using dd but I can't see a way to just corrupt a s