473,386 Members | 1,962 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Outer join problem

Hi,

We have a problem in our mobile calls billing software. To solve it, I
need an outer join in a complicated query. Following are the
simplified tables with sample data:

create table CONTROL1 (CTRL_NO number(2));
insert into CONTROL1 values (10);

create table CONTROL2 (CTRL_NO number(2));
-- does not have any data

create table CODE (CODE number(2), CD_TYP number(2));
insert into CODE values (20, 21);

create table PAYER (PERIOD number(2), CUST number(2), BA number(2),
CTRL_NO number(2));
insert into PAYER values (1,1,1,10);
insert into PAYER values (1,1,2,10);
insert into PAYER values (1,1,3,10);

create table CHARGE (PERIOD number(2), CUST number(2), BA number(2),
CTRL_NO number(2), SEQ1 number(2), SEQ2 number(2), CODE number(2),
RECEIVER number(2));
insert into CHARGE values (1,1,1,10,4,5,20,99);
insert into CHARGE values (1,1,2,10,4,6,20,90);

create table OUTCHARGE (PERIOD number(2), CUST number(2), SEQ1
number(2), SEQ2 number(2));
insert into OUTCHARGE values (1,1,4,5);
insert into OUTCHARGE values (1,1,4,6);

QUERY is:

SELECT PAYER.PERIOD, PAYER.CUST, PAYER.BA, PAYER.CTRL_NO,
CHARGE.CODE, CODE.CD_TYP, CHARGE.RECEIVER
FROM PAYER, CHARGE, OUTCHARGE, CODE,
(select * from CONTROL1
union select * from CONTROL2) TMP_CONTROL
WHERE PAYER.CTRL_NO=TMP_CONTROL.CTRL_NO
AND CHARGE.PERIOD(+)=PAYER.PERIOD
AND CHARGE.CUST(+)=PAYER.CUST
AND CHARGE.BA(+)=PAYER.BA
AND CHARGE.CTRL_NO(+)=PAYER.CTRL_NO
AND CHARGE.CODE=CODE.CODE
AND OUTCHARGE.PERIOD=CHARGE.PERIOD
AND OUTCHARGE.CUST=CHARGE.CUST
AND OUTCHARGE.SEQ1=CHARGE.SEQ1
AND OUTCHARGE.SEQ2=CHARGE.SEQ2;

Output should be (3 rows):
(1,1,1,10,20,21,99)
(1,1,2,10,20,21,90)
(1,1,3,10,null,null,null)

When I use the above query, I get only the first 2 rows, not the
third. What mistake am I doing?

Thanks in advance.
Jul 19 '05 #1
3 4494
Neeraj,

It would be interesting to have a more complete functional description of
these tables / fields and the functional intent of the query.

Having said that, you'll never get the 3rd row in the result with your query
since that would require CHARGE.CODE to be null and also
CHARGE.CODE=CODE.CODE to be true. Since that expression is never true,
that's the basic reason for the behavior you are seeing.

You'll similarly have the same problem with comparisons of the other columns
of table "charge".

Try this (just as a test) or use the non-standard (+) syntax if you wish:

SELECT payer.period, payer.cust, payer.ba, charge.ba,
payer.ctrl_no, charge.code, code.cd_typ, charge.receiver
FROM payer LEFT JOIN charge
ON charge.period = payer.period
AND charge.cust = payer.cust
AND charge.ba = payer.ba
AND charge.ctrl_no = payer.ctrl_no,
outcharge,
code,
(SELECT *
FROM control1
UNION
SELECT *
FROM control2) tmp_control;

Regards... Jon

Jon Armstrong

<ja*********@lycos.com> wrote in message
news:30**************************@posting.google.c om...
Hi,

We have a problem in our mobile calls billing software. To solve it, I
need an outer join in a complicated query. Following are the
simplified tables with sample data:

create table CONTROL1 (CTRL_NO number(2));
insert into CONTROL1 values (10);

create table CONTROL2 (CTRL_NO number(2));
-- does not have any data

create table CODE (CODE number(2), CD_TYP number(2));
insert into CODE values (20, 21);

create table PAYER (PERIOD number(2), CUST number(2), BA number(2),
CTRL_NO number(2));
insert into PAYER values (1,1,1,10);
insert into PAYER values (1,1,2,10);
insert into PAYER values (1,1,3,10);

create table CHARGE (PERIOD number(2), CUST number(2), BA number(2),
CTRL_NO number(2), SEQ1 number(2), SEQ2 number(2), CODE number(2),
RECEIVER number(2));
insert into CHARGE values (1,1,1,10,4,5,20,99);
insert into CHARGE values (1,1,2,10,4,6,20,90);

create table OUTCHARGE (PERIOD number(2), CUST number(2), SEQ1
number(2), SEQ2 number(2));
insert into OUTCHARGE values (1,1,4,5);
insert into OUTCHARGE values (1,1,4,6);

QUERY is:

SELECT PAYER.PERIOD, PAYER.CUST, PAYER.BA, PAYER.CTRL_NO,
CHARGE.CODE, CODE.CD_TYP, CHARGE.RECEIVER
FROM PAYER, CHARGE, OUTCHARGE, CODE,
(select * from CONTROL1
union select * from CONTROL2) TMP_CONTROL
WHERE PAYER.CTRL_NO=TMP_CONTROL.CTRL_NO
AND CHARGE.PERIOD(+)=PAYER.PERIOD
AND CHARGE.CUST(+)=PAYER.CUST
AND CHARGE.BA(+)=PAYER.BA
AND CHARGE.CTRL_NO(+)=PAYER.CTRL_NO
AND CHARGE.CODE=CODE.CODE
AND OUTCHARGE.PERIOD=CHARGE.PERIOD
AND OUTCHARGE.CUST=CHARGE.CUST
AND OUTCHARGE.SEQ1=CHARGE.SEQ1
AND OUTCHARGE.SEQ2=CHARGE.SEQ2;

Output should be (3 rows):
(1,1,1,10,20,21,99)
(1,1,2,10,20,21,90)
(1,1,3,10,null,null,null)

When I use the above query, I get only the first 2 rows, not the
third. What mistake am I doing?

Thanks in advance.



-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Jul 19 '05 #2
What exactly do you want done? We could make your output come out but
it doesn't necessarily mean that we're meeting your logical
requirements. Some more explanation would be helpful.

Also, if you are in 9i at least, consider using ANSI OUTER JOIN
syntax. Some restrictions using the (+) operator may be cirvumvented
by this.

But to get you started. Written in the traditional Oracle outer join
syntax. Again (not sure if this meets your "business" need).

SELECT PAYER.PERIOD, PAYER.CUST, PAYER.BA, PAYER.CTRL_NO,
CHARGE.CODE, CHARGE.CD_TYP, CHARGE.RECEIVER
FROM PAYER,
(select CHARGE.*, CD_TYP
from CHARGE, OUTCHARGE, CODE
where OUTCHARGE.PERIOD=CHARGE.PERIOD
AND OUTCHARGE.CUST=CHARGE.CUST
AND OUTCHARGE.SEQ1=CHARGE.SEQ1
AND OUTCHARGE.SEQ2=CHARGE.SEQ2
and CHARGE.CODE = CODE.CODE(+)) CHARGE,
(select * from CONTROL1
union select * from CONTROL2) TMP_CONTROL
WHERE PAYER.CTRL_NO=TMP_CONTROL.CTRL_NO
AND CHARGE.PERIOD(+)=PAYER.PERIOD
AND CHARGE.CUST(+)=PAYER.CUST
AND CHARGE.BA(+)=PAYER.BA
AND CHARGE.CTRL_NO(+)=PAYER.CTRL_NO

PERIOD CUST BA CTRL_NO CODE CD_TYP
RECEIVER
---------- ---------- ---------- ---------- ---------- ----------
----------
1 1 1 10 20 21
99
1 1 2 10 20 21
90
1 1 3 10


ja*********@lycos.com wrote in message news:<30**************************@posting.google. com>...
Hi,

We have a problem in our mobile calls billing software. To solve it, I
need an outer join in a complicated query. Following are the
simplified tables with sample data:

create table CONTROL1 (CTRL_NO number(2));
insert into CONTROL1 values (10);

create table CONTROL2 (CTRL_NO number(2));
-- does not have any data

create table CODE (CODE number(2), CD_TYP number(2));
insert into CODE values (20, 21);

create table PAYER (PERIOD number(2), CUST number(2), BA number(2),
CTRL_NO number(2));
insert into PAYER values (1,1,1,10);
insert into PAYER values (1,1,2,10);
insert into PAYER values (1,1,3,10);

create table CHARGE (PERIOD number(2), CUST number(2), BA number(2),
CTRL_NO number(2), SEQ1 number(2), SEQ2 number(2), CODE number(2),
RECEIVER number(2));
insert into CHARGE values (1,1,1,10,4,5,20,99);
insert into CHARGE values (1,1,2,10,4,6,20,90);

create table OUTCHARGE (PERIOD number(2), CUST number(2), SEQ1
number(2), SEQ2 number(2));
insert into OUTCHARGE values (1,1,4,5);
insert into OUTCHARGE values (1,1,4,6);

QUERY is:

SELECT PAYER.PERIOD, PAYER.CUST, PAYER.BA, PAYER.CTRL_NO,
CHARGE.CODE, CODE.CD_TYP, CHARGE.RECEIVER
FROM PAYER, CHARGE, OUTCHARGE, CODE,
(select * from CONTROL1
union select * from CONTROL2) TMP_CONTROL
WHERE PAYER.CTRL_NO=TMP_CONTROL.CTRL_NO
AND CHARGE.PERIOD(+)=PAYER.PERIOD
AND CHARGE.CUST(+)=PAYER.CUST
AND CHARGE.BA(+)=PAYER.BA
AND CHARGE.CTRL_NO(+)=PAYER.CTRL_NO
AND CHARGE.CODE=CODE.CODE
AND OUTCHARGE.PERIOD=CHARGE.PERIOD
AND OUTCHARGE.CUST=CHARGE.CUST
AND OUTCHARGE.SEQ1=CHARGE.SEQ1
AND OUTCHARGE.SEQ2=CHARGE.SEQ2;

Output should be (3 rows):
(1,1,1,10,20,21,99)
(1,1,2,10,20,21,90)
(1,1,3,10,null,null,null)

When I use the above query, I get only the first 2 rows, not the
third. What mistake am I doing?

Thanks in advance.

Jul 19 '05 #3
Thanks a lot, Jon and Romeo. Your explainations cleared the fog and I
was finally able to see the missing link (CHARGE.CODE = CODE.CODE(+)).

ro******@hotmail.com (Romeo Olympia) wrote in message news:<42**************************@posting.google. com>...
What exactly do you want done? We could make your output come out but
it doesn't necessarily mean that we're meeting your logical
requirements. Some more explanation would be helpful.

Also, if you are in 9i at least, consider using ANSI OUTER JOIN
syntax. Some restrictions using the (+) operator may be cirvumvented
by this.

But to get you started. Written in the traditional Oracle outer join
syntax. Again (not sure if this meets your "business" need).

SELECT PAYER.PERIOD, PAYER.CUST, PAYER.BA, PAYER.CTRL_NO,
CHARGE.CODE, CHARGE.CD_TYP, CHARGE.RECEIVER
FROM PAYER,
(select CHARGE.*, CD_TYP
from CHARGE, OUTCHARGE, CODE
where OUTCHARGE.PERIOD=CHARGE.PERIOD
AND OUTCHARGE.CUST=CHARGE.CUST
AND OUTCHARGE.SEQ1=CHARGE.SEQ1
AND OUTCHARGE.SEQ2=CHARGE.SEQ2
and CHARGE.CODE = CODE.CODE(+)) CHARGE,
(select * from CONTROL1
union select * from CONTROL2) TMP_CONTROL
WHERE PAYER.CTRL_NO=TMP_CONTROL.CTRL_NO
AND CHARGE.PERIOD(+)=PAYER.PERIOD
AND CHARGE.CUST(+)=PAYER.CUST
AND CHARGE.BA(+)=PAYER.BA
AND CHARGE.CTRL_NO(+)=PAYER.CTRL_NO

PERIOD CUST BA CTRL_NO CODE CD_TYP
RECEIVER
---------- ---------- ---------- ---------- ---------- ----------
----------
1 1 1 10 20 21
99
1 1 2 10 20 21
90
1 1 3 10


ja*********@lycos.com wrote in message news:<30**************************@posting.google. com>...
Hi,

We have a problem in our mobile calls billing software. To solve it, I
need an outer join in a complicated query. Following are the
simplified tables with sample data:

create table CONTROL1 (CTRL_NO number(2));
insert into CONTROL1 values (10);

create table CONTROL2 (CTRL_NO number(2));
-- does not have any data

create table CODE (CODE number(2), CD_TYP number(2));
insert into CODE values (20, 21);

create table PAYER (PERIOD number(2), CUST number(2), BA number(2),
CTRL_NO number(2));
insert into PAYER values (1,1,1,10);
insert into PAYER values (1,1,2,10);
insert into PAYER values (1,1,3,10);

create table CHARGE (PERIOD number(2), CUST number(2), BA number(2),
CTRL_NO number(2), SEQ1 number(2), SEQ2 number(2), CODE number(2),
RECEIVER number(2));
insert into CHARGE values (1,1,1,10,4,5,20,99);
insert into CHARGE values (1,1,2,10,4,6,20,90);

create table OUTCHARGE (PERIOD number(2), CUST number(2), SEQ1
number(2), SEQ2 number(2));
insert into OUTCHARGE values (1,1,4,5);
insert into OUTCHARGE values (1,1,4,6);

QUERY is:

SELECT PAYER.PERIOD, PAYER.CUST, PAYER.BA, PAYER.CTRL_NO,
CHARGE.CODE, CODE.CD_TYP, CHARGE.RECEIVER
FROM PAYER, CHARGE, OUTCHARGE, CODE,
(select * from CONTROL1
union select * from CONTROL2) TMP_CONTROL
WHERE PAYER.CTRL_NO=TMP_CONTROL.CTRL_NO
AND CHARGE.PERIOD(+)=PAYER.PERIOD
AND CHARGE.CUST(+)=PAYER.CUST
AND CHARGE.BA(+)=PAYER.BA
AND CHARGE.CTRL_NO(+)=PAYER.CTRL_NO
AND CHARGE.CODE=CODE.CODE
AND OUTCHARGE.PERIOD=CHARGE.PERIOD
AND OUTCHARGE.CUST=CHARGE.CUST
AND OUTCHARGE.SEQ1=CHARGE.SEQ1
AND OUTCHARGE.SEQ2=CHARGE.SEQ2;

Output should be (3 rows):
(1,1,1,10,20,21,99)
(1,1,2,10,20,21,90)
(1,1,3,10,null,null,null)

When I use the above query, I get only the first 2 rows, not the
third. What mistake am I doing?

Thanks in advance.

Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Martin | last post by:
I am now working on SQL Server 2000 having had previous experience on a different database. Both of the OUTER JOIN syntaxes is different from what I am used to and I am finding it slightly...
8
by: Matt | last post by:
Hello I have to tables ar and arb, ar holds articles and a swedish description, arb holds descriptions in other languages. I want to retreive all articles that match a criteria from ar and...
4
by: thilbert | last post by:
All, I have a perplexing problem that I hope someone can help me with. I have the following table struct: Permission ----------------- PermissionId Permission
3
by: Dam | last post by:
Using SqlServer : Query 1 : SELECT def.lID as IdDefinition, TDC_AUneValeur.VALEURDERETOUR as ValeurDeRetour FROM serveur.Data_tblDEFINITIONTABLEDECODES def,...
7
by: Steve | last post by:
I have a SQL query I'm invoking via VB6 & ADO 2.8, that requires three "Left Outer Joins" in order to return every transaction for a specific set of criteria. Using three "Left Outer Joins"...
6
by: Thomas Beutin | last post by:
Hi, i've a speed problem withe the following statement: SELECT DISTINCT pz.l1_id, pz.l2_id, pz.l3_id, pz.l4_id FROM ot_adresse AS a, ot_produkt AS p LEFT OUTER JOIN ot_kat_prod AS pz ON (...
3
by: Martin | last post by:
Hello everybody, I have the following question. As a join clause on Oracle we use " table1.field1 = table2.field1 (+) " On SQL Server we use " table1.field1 *= table2.field1 " Does DB2...
4
by: Anthony Robinson | last post by:
I was actually just wondering if someone could possibly take a look and tell me what I may be doing wrong in this query? I keep getting ambiguous column errors and have no idea why...? Thanks in...
3
by: deko | last post by:
From what I understand, an Inner Join narrow the rows selected to the table with the least results... and an Outer Join does the opposite... SELECT qryTxToQ1.Q1Total, qryTxToQ2.Q2Total,...
3
by: Doug | last post by:
Hi, I'm more familiar with MSSQL than Access syntax and have run into a problem with correctly putting ( )'s around the joins in a 3 table query. I want to INNER JOIN lenders and accounts and...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.