473,665 Members | 2,774 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Outer join returns incorrect results when using sub-selects

I'm seeing a very strange problem with outer joins.

The example below replicates the problem:

create table data1 (dim1 integer, stat1 float);
create table data2 (dim1 integer, stat2 float);

insert into data1 values (1,1);
insert into data1 values (1,2);
insert into data1 values (2,2);
insert into data1 values (2,2);
insert into data1 values (3,3);
insert into data1 values (6,3);
insert into data1 values (7,3);

insert into data2 values (2,1);
insert into data2 values (2,2);
insert into data2 values (3,2);
insert into data2 values (4,3);
insert into data2 values (5,3);

I want to be able to run a query that generates the resultset:

dim1, sum(stat1), sum(stat2)
1,3,NULL
2,4,3
3,3,2
4,NULL,3
5,NULL,3
6,3,NULL
7,3,NULL

I was planning to use the following sql:

select nvl(s1.dim1, s2.dim1) dim1, stat1, stat2 from
(
select dim1, sum(stat1) stat1 from data1 group by dim1
) s1
full outer join
(
select dim1, sum(stat2) stat2 from data2 group by dim1
) s2 on s1.dim1=s2.dim1
order by dim1;

When I run this. Instead of the 4 rows I was expecting I get back:

dim1,stat1,stat 2
1,3,NULL
2,4,3
3,3,2
4,NULL,NULL
5,NULL,NULL
6,3,NULL
7,3,NULL

The values for stat2 seem to be getting lost when there is no value for stat1.

Anyone have any ideas as to what is going on? Am I doing something stupid?
Jul 19 '05 #1
9 6401
ch************@ actix.com (Chris Greening) wrote in message news:<57******* *************** ****@posting.go ogle.com>...
I'm seeing a very strange problem with outer joins.

The example below replicates the problem:

create table data1 (dim1 integer, stat1 float);
create table data2 (dim1 integer, stat2 float);

insert into data1 values (1,1);
insert into data1 values (1,2);
insert into data1 values (2,2);
insert into data1 values (2,2);
insert into data1 values (3,3);
insert into data1 values (6,3);
insert into data1 values (7,3);

insert into data2 values (2,1);
insert into data2 values (2,2);
insert into data2 values (3,2);
insert into data2 values (4,3);
insert into data2 values (5,3);

I want to be able to run a query that generates the resultset:

dim1, sum(stat1), sum(stat2)
1,3,NULL
2,4,3
3,3,2
4,NULL,3
5,NULL,3
6,3,NULL
7,3,NULL

I was planning to use the following sql:

select nvl(s1.dim1, s2.dim1) dim1, stat1, stat2 from
(
select dim1, sum(stat1) stat1 from data1 group by dim1
) s1
full outer join
(
select dim1, sum(stat2) stat2 from data2 group by dim1
) s2 on s1.dim1=s2.dim1
order by dim1;

When I run this. Instead of the 4 rows I was expecting I get back:

dim1,stat1,stat 2
1,3,NULL
2,4,3
3,3,2
4,NULL,NULL
5,NULL,NULL
6,3,NULL
7,3,NULL

The values for stat2 seem to be getting lost when there is no value for stat1.

Anyone have any ideas as to what is going on? Am I doing something stupid?


I don't see the problem, but I did notice the nvl() function. A NULL
shouldn't occur in the join column (at least not with the data you
presented) so you should just list the key:

select s1.dim1, stat1, stat2 from

I doubt that would fix the other columns, but this is going to be one
of those "intuitive obvious" solutions, i.e., once you know the
answer, you realize it was "intuitive obvious" 8^)
Jul 19 '05 #2
ch************@ actix.com (Chris Greening) wrote in message news:<57******* *************** ****@posting.go ogle.com>...
I'm seeing a very strange problem with outer joins.

The example below replicates the problem:

create table data1 (dim1 integer, stat1 float);
create table data2 (dim1 integer, stat2 float);

insert into data1 values (1,1);
insert into data1 values (1,2);
insert into data1 values (2,2);
insert into data1 values (2,2);
insert into data1 values (3,3);
insert into data1 values (6,3);
insert into data1 values (7,3);

insert into data2 values (2,1);
insert into data2 values (2,2);
insert into data2 values (3,2);
insert into data2 values (4,3);
insert into data2 values (5,3);

I want to be able to run a query that generates the resultset:

dim1, sum(stat1), sum(stat2)
1,3,NULL
2,4,3
3,3,2
4,NULL,3
5,NULL,3
6,3,NULL
7,3,NULL

I was planning to use the following sql:
select nvl(s1.dim1, s2.dim1) dim1, stat1, stat2 from
(
select dim1, sum(stat1) stat1 from data1 group by dim1
) s1
full outer join
(
select dim1, sum(stat2) stat2 from data2 group by dim1
) s2 on s1.dim1=s2.dim1
order by dim1;

When I run this. Instead of the 4 rows I was expecting I get back:

dim1,stat1,stat 2
1,3,NULL
2,4,3
3,3,2
4,NULL,NULL
5,NULL,NULL
6,3,NULL
7,3,NULL

The values for stat2 seem to be getting lost when there is no value for stat1.

Anyone have any ideas as to what is going on? Am I doing something stupid?


What's RDBMS version? I had similar problem with 8.1.6, which disappeared
after upgrade to 8.1.7. There was a workaround too, forcing hash outer
join fixed the query.
Jul 19 '05 #3
ch************@ actix.com (Chris Greening) wrote in message news:<57******* *************** ****@posting.go ogle.com>...
I'm seeing a very strange problem with outer joins.

The example below replicates the problem:

create table data1 (dim1 integer, stat1 float);
create table data2 (dim1 integer, stat2 float);

insert into data1 values (1,1);
insert into data1 values (1,2);
insert into data1 values (2,2);
insert into data1 values (2,2);
insert into data1 values (3,3);
insert into data1 values (6,3);
insert into data1 values (7,3);

insert into data2 values (2,1);
insert into data2 values (2,2);
insert into data2 values (3,2);
insert into data2 values (4,3);
insert into data2 values (5,3);

I want to be able to run a query that generates the resultset:

dim1, sum(stat1), sum(stat2)
1,3,NULL
2,4,3
3,3,2
4,NULL,3
5,NULL,3
6,3,NULL
7,3,NULL

I was planning to use the following sql:

select nvl(s1.dim1, s2.dim1) dim1, stat1, stat2 from
(
select dim1, sum(stat1) stat1 from data1 group by dim1
) s1
full outer join
(
select dim1, sum(stat2) stat2 from data2 group by dim1
) s2 on s1.dim1=s2.dim1
order by dim1;

When I run this. Instead of the 4 rows I was expecting I get back:

dim1,stat1,stat 2
1,3,NULL
2,4,3
3,3,2
4,NULL,NULL
5,NULL,NULL
6,3,NULL
7,3,NULL

The values for stat2 seem to be getting lost when there is no value for stat1.

Anyone have any ideas as to what is going on? Am I doing something stupid?


No, if you are getting that result with that SQL then you are correct
and Oracle has a serious bug. Which version?
DG
Jul 19 '05 #4
select dim1,sum(stat1) stat1,sum(stat2 ) stat2
from (select distinct dim1, stat1, stat2 from
data1 full outer join data2 using (dim1)
order by dim1)
group by dim1;

This way will work.

Lori

ch************@ actix.com (Chris Greening) wrote in message news:<57******* *************** ****@posting.go ogle.com>...
I'm seeing a very strange problem with outer joins.

The example below replicates the problem:

create table data1 (dim1 integer, stat1 float);
create table data2 (dim1 integer, stat2 float);

insert into data1 values (1,1);
insert into data1 values (1,2);
insert into data1 values (2,2);
insert into data1 values (2,2);
insert into data1 values (3,3);
insert into data1 values (6,3);
insert into data1 values (7,3);

insert into data2 values (2,1);
insert into data2 values (2,2);
insert into data2 values (3,2);
insert into data2 values (4,3);
insert into data2 values (5,3);

I want to be able to run a query that generates the resultset:

dim1, sum(stat1), sum(stat2)
1,3,NULL
2,4,3
3,3,2
4,NULL,3
5,NULL,3
6,3,NULL
7,3,NULL

I was planning to use the following sql:

select nvl(s1.dim1, s2.dim1) dim1, stat1, stat2 from
(
select dim1, sum(stat1) stat1 from data1 group by dim1
) s1
full outer join
(
select dim1, sum(stat2) stat2 from data2 group by dim1
) s2 on s1.dim1=s2.dim1
order by dim1;

When I run this. Instead of the 4 rows I was expecting I get back:

dim1,stat1,stat 2
1,3,NULL
2,4,3
3,3,2
4,NULL,NULL
5,NULL,NULL
6,3,NULL
7,3,NULL

The values for stat2 seem to be getting lost when there is no value for stat1.

Anyone have any ideas as to what is going on? Am I doing something stupid?

Jul 19 '05 #5
Hi Ed/Alex, thanks for replying

Ed, here's a simpler example that gives the same problem:

create table test1 (dim1 integer, stat1 float);
create table test2 (dim1 integer, stat2 float);

insert into test1 values (1, 1);
insert into test2 values (2, 1);
commit;

select * from
(select dim1, sum(stat1) from test1 group by dim1) s1 full outer join
(select dim1, sum(stat2) from test2 group by dim1) s2 on s1.dim1=s2.dim1 ;

DIM1 SUM(STAT1) DIM1 SUM(STAT2)
---------- ---------- ---------- ----------
1 1
2

As you can see the SUM(STAT2) column is always null.

If I remove the group by (and the sum) from the subselect I get the correct results:

select * from
(select dim1, stat1 from test1) s1 full outer join
(select dim1, stat2 from test2) s2 on s1.dim1=s2.dim1 ;

DIM1 STAT1 DIM1 STAT2
---------- ---------- ---------- ----------
1 1
2 1

Unfortunately we need the aggregation in our subselects.

Alex, we are using:

Oracle9i Enterprise Edition Release 9.2.0.1.0 - Production
With the Partitioning, OLAP and Oracle Data Mining options
JServer Release 9.2.0.1.0 - Production

Thanks
Chris.

af******@yahoo. com (Alex Filonov) wrote in message news:<33******* *************** ****@posting.go ogle.com>...
ch************@ actix.com (Chris Greening) wrote in message news:<57******* *************** ****@posting.go ogle.com>...
I'm seeing a very strange problem with outer joins.

The example below replicates the problem:

create table data1 (dim1 integer, stat1 float);
create table data2 (dim1 integer, stat2 float);

insert into data1 values (1,1);
insert into data1 values (1,2);
insert into data1 values (2,2);
insert into data1 values (2,2);

....
Jul 19 '05 #6
Try this :

select dim1,sum(stat1) stat1,sum(stat2 ) stat2
from (select distinct dim1, stat1, stat2 from
data1 full outer join data2 using (dim1)
order by dim1)
group by dim1;
Seems to give the proper output with no nvl surprises.

Lori
Jul 19 '05 #7
ch************@ actix.com (Chris Greening) wrote in message news:<57******* *************** ****@posting.go ogle.com>...
Hi Ed/Alex, thanks for replying

Ed, here's a simpler example that gives the same problem:

create table test1 (dim1 integer, stat1 float);
create table test2 (dim1 integer, stat2 float);

insert into test1 values (1, 1);
insert into test2 values (2, 1);
commit;

select * from
(select dim1, sum(stat1) from test1 group by dim1) s1 full outer join
(select dim1, sum(stat2) from test2 group by dim1) s2 on s1.dim1=s2.dim1 ;

DIM1 SUM(STAT1) DIM1 SUM(STAT2)
---------- ---------- ---------- ----------
1 1
2

As you can see the SUM(STAT2) column is always null.

If I remove the group by (and the sum) from the subselect I get the correct results:

Looks like problem was fixed in 9.2.0.3:

SQL> create table test1 (dim1 integer, stat1 float);
create table test2 (dim1 integer, stat2 float);

insert into test1 values (1, 1);
insert into test2 values (2, 1);
commit;

select * from
(select dim1, sum(stat1) from test1 group by dim1) s1 full outer join
(select dim1, sum(stat2) from test2 group by dim1) s2 on s1.dim1=s2.dim1 ;
Table created.

SQL>
Table created.

SQL> SQL>
1 row created.

SQL>
1 row created.

SQL>
Commit complete.

SQL> SQL> 2 3
DIM1 SUM(STAT1) DIM1 SUM(STAT2)
---------- ---------- ---------- ----------
1 1
2 1

You need to upgrade your server.
select * from
(select dim1, stat1 from test1) s1 full outer join
(select dim1, stat2 from test2) s2 on s1.dim1=s2.dim1 ;

DIM1 STAT1 DIM1 STAT2
---------- ---------- ---------- ----------
1 1
2 1

Unfortunately we need the aggregation in our subselects.

Alex, we are using:

Oracle9i Enterprise Edition Release 9.2.0.1.0 - Production
With the Partitioning, OLAP and Oracle Data Mining options
JServer Release 9.2.0.1.0 - Production

Thanks
Chris.

af******@yahoo. com (Alex Filonov) wrote in message news:<33******* *************** ****@posting.go ogle.com>...
ch************@ actix.com (Chris Greening) wrote in message news:<57******* *************** ****@posting.go ogle.com>...
I'm seeing a very strange problem with outer joins.

The example below replicates the problem:

create table data1 (dim1 integer, stat1 float);
create table data2 (dim1 integer, stat2 float);

insert into data1 values (1,1);
insert into data1 values (1,2);
insert into data1 values (2,2);
insert into data1 values (2,2);

...

Jul 19 '05 #8
Hi Ed/Alex, thanks for replying

Ed, here's a simpler example that gives the same problem:

create table test1 (dim1 integer, stat1 float);
create table test2 (dim1 integer, stat2 float);

insert into test1 values (1, 1);
insert into test2 values (2, 1);
commit;

select * from
(select dim1, sum(stat1) from test1 group by dim1) s1 full outer join
(select dim1, sum(stat2) from test2 group by dim1) s2 on s1.dim1=s2.dim1 ;

DIM1 SUM(STAT1) DIM1 SUM(STAT2)
---------- ---------- ---------- ----------
1 1
2

As you can see the SUM(STAT2) column is always null.

If I remove the group by (and the sum) from the subselect I get the correct results:

select * from
(select dim1, stat1 from test1) s1 full outer join
(select dim1, stat2 from test2) s2 on s1.dim1=s2.dim1 ;

DIM1 STAT1 DIM1 STAT2
---------- ---------- ---------- ----------
1 1
2 1

Unfortunately we need the aggregation in our subselects.

Alex, we are using:

Oracle9i Enterprise Edition Release 9.2.0.1.0 - Production
With the Partitioning, OLAP and Oracle Data Mining options
JServer Release 9.2.0.1.0 - Production

Thanks
Chris.

af******@yahoo. com (Alex Filonov) wrote in message news:<33******* *************** ****@posting.go ogle.com>...
ch************@ actix.com (Chris Greening) wrote in message news:<57******* *************** ****@posting.go ogle.com>...
I'm seeing a very strange problem with outer joins.

The example below replicates the problem:

create table data1 (dim1 integer, stat1 float);
create table data2 (dim1 integer, stat2 float);

insert into data1 values (1,1);
insert into data1 values (1,2);
insert into data1 values (2,2);
insert into data1 values (2,2);
....
Jun 27 '08 #9
ch************@ actix.com (Chris Greening) wrote in message news:<57******* *************** ****@posting.go ogle.com>...
Hi Ed/Alex, thanks for replying

Ed, here's a simpler example that gives the same problem:

create table test1 (dim1 integer, stat1 float);
create table test2 (dim1 integer, stat2 float);

insert into test1 values (1, 1);
insert into test2 values (2, 1);
commit;

select * from
(select dim1, sum(stat1) from test1 group by dim1) s1 full outer join
(select dim1, sum(stat2) from test2 group by dim1) s2 on s1.dim1=s2.dim1 ;

DIM1 SUM(STAT1) DIM1 SUM(STAT2)
---------- ---------- ---------- ----------
1 1
2

As you can see the SUM(STAT2) column is always null.

If I remove the group by (and the sum) from the subselect I get the correct results:
Looks like problem was fixed in 9.2.0.3:

SQLcreate table test1 (dim1 integer, stat1 float);
create table test2 (dim1 integer, stat2 float);

insert into test1 values (1, 1);
insert into test2 values (2, 1);
commit;

select * from
(select dim1, sum(stat1) from test1 group by dim1) s1 full outer join
(select dim1, sum(stat2) from test2 group by dim1) s2 on s1.dim1=s2.dim1 ;
Table created.

SQL>
Table created.

SQLSQL>
1 row created.

SQL>
1 row created.

SQL>
Commit complete.

SQLSQL 2 3
DIM1 SUM(STAT1) DIM1 SUM(STAT2)
---------- ---------- ---------- ----------
1 1
2 1

You need to upgrade your server.
select * from
(select dim1, stat1 from test1) s1 full outer join
(select dim1, stat2 from test2) s2 on s1.dim1=s2.dim1 ;

DIM1 STAT1 DIM1 STAT2
---------- ---------- ---------- ----------
1 1
2 1

Unfortunately we need the aggregation in our subselects.

Alex, we are using:

Oracle9i Enterprise Edition Release 9.2.0.1.0 - Production
With the Partitioning, OLAP and Oracle Data Mining options
JServer Release 9.2.0.1.0 - Production

Thanks
Chris.

af******@yahoo. com (Alex Filonov) wrote in message news:<33******* *************** ****@posting.go ogle.com>...
ch************@ actix.com (Chris Greening) wrote in message news:<57******* *************** ****@posting.go ogle.com>...
I'm seeing a very strange problem with outer joins.
>
The example below replicates the problem:
>
create table data1 (dim1 integer, stat1 float);
create table data2 (dim1 integer, stat2 float);
>
insert into data1 values (1,1);
insert into data1 values (1,2);
insert into data1 values (2,2);
insert into data1 values (2,2);
...
Jun 27 '08 #10

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

Similar topics

1
11682
by: Dave | last post by:
Hi I have the following 4 tables and I need to do a fully outerjoin on them. create table A (a number, b number, c char(10), primary key (a,b)) create table B (a number, b number, c char(10), primary key (a,b)) create table C (a number, b number, c char(10), priamry key (a,b)) create table D (a number, b number, c char(10), priamry key (a,b)) In oracle 9i, the following query returns correct results set, if I were to
2
2236
by: Preston Landers | last post by:
Hello all. I am trying to write a query that "just" switches some data around so it is shown in a slightly different format. I am already able to do what I want in Oracle 8i, but I am having trouble making it work in SQL Server 2000. I am not a database newbie, but I can't seem to figure this one out so I am turning to the newsgroup. I am thinking that some of the SQL Gurus out there have done this very thing a thousand times before...
8
4968
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 also display their corresponding entries in arb, but if there is NO entry in arb I still want it to show up as NULL or something, so that I can get the attention that there IS no language associated with that article.
4
4861
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
10046
by: Dam | last post by:
Using SqlServer : Query 1 : SELECT def.lID as IdDefinition, TDC_AUneValeur.VALEURDERETOUR as ValeurDeRetour FROM serveur.Data_tblDEFINITIONTABLEDECODES def, serveur.Data_tblTABLEDECODEAUNEVALEUR TDC_AUneValeur where def.TYPEDETABLEDECODES = 4
7
31549
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" slows the system down considerably. I've tried creating a temp db, but I can't figure out how to execute two select commands. (It throws the exception "The column prefix 'tempdb' does not match with a table name or alias name used in the query.")
1
1731
by: Rory Campbell-Lange | last post by:
I'm having troube doing a left outer self join on a table. The sent column shows the number of items sent to each recipient from each source. The received column (generated by the outer join) is incorrect because although it is summing the number of messages by recipient, these need to be filtered by source too. How can I do a join on two columns in the master table? Thanks for any help. Rory
3
19470
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 have the same type of operator, without using the OUTER JOIN syntax ?
3
7543
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, qryTxToQ3.Q3Total, qryTxToQ4.Q4Total FROM qryTxToQ4 OUTER JOIN (qryTxToQ3 OUTER JOIN (qryTxToQ1 OUTER JOIN qryTxToQ2 ON qryTxToQ1.TxAcct_ID = qryTxToQ2.TxAcct_ID) ON qryTxToQ3.TxAcct_ID = qryTxToQ2.TxAcct_ID) ON qryTxToQ4.TxAcct_ID = qryTxToQ3.TxAcct_ID;
0
1795
by: brunodamato | last post by:
In the example that follows, I am receiving an incorrect result set in the View. I am looking to get the FULL result set from View_LY and Table_TY. Instead, this View is returning the Common (Equi) records and records on Table_TY only. Missing the records on View_LY only. Any known problems with FULL OUTER JOIN in DB2 or am I missing something. Documentation all around is somewhat limited: View: View_LY Store (PK) Product (PK) Week...
0
8438
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8348
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8863
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8549
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7376
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5660
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4356
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2004
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1761
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.