Cross Join in SQL
From relational algebra, cross join or cross product or cartesion product is the set of all combinations of tuples in R and S where R and S are relations. Is there anyway I can do the same in SQL?
Thanks,
Mustafa
in oracle 9i u can also say
select <column list>
from <table 1> cross join <table 2>
where <condition>
here where clause is optional....
Similar Messages
-
Hi guys!
I need to implement a CROSS JOIN (cartesian product) based on two tables. To my understanding there is no construct to implement a cross join in OPEN SQL. So the question is first is this correct? And secondly in which way would you then implement a cross join of two tables with the constructs at hand in ABAP?
I have come up with the following solution of cross joining two tables, but would like to know if there is a better (not so ugly way).
Example:
SELECT * FROM tab1 AS t1 JOIN tab2 AS t2 ON t1mandt = t2mandt INTO CORRESPONDING FIELDS OF itab.
Note that this only works if the tables tab1 and tab2 is client dependent.
Regards,
ChristianChristian,
There's no cross join construct in abap. Join statement results in a cartesin product rarely because of completely obsolete statistics missing selection or join conditions.
Try the following join to get result similar to cartesian product
select tab1location tab2matnr into itab
from tab1 join tab2 on tab1location ne tab2matnr.
if you really want to implement cross join, use native sql, try the following code,
beware of cartesian products, Basis folks doesn't like em....
data: begin of itab occurs 0,
loc like tab1-sloc
matnr like tab2-matnr,
end of itab.
exec sql performing append_itab.
select a.loc, b.matnr
into :itab
from tab1 a
cross join tab2 b
endexec.
form append_itab.
append itab.
endform.
Regards
Sridhar. -
Illegal cross join within the same dimension caused by incorrect subject ar
hi!
Imagine the following BMM:
There is one Logical Dimension Table "Service Account" with the following LTS:
- LTS "D_SERVICE"
- LTS "D_SERVICE_CLASS"
- LTS "D_SERVICE_STATUS"
This Logical Tabe "Service Account" joins (One-to-many) with several Fact Tables (other Logical Tables).
In the Physical Layer the joins for the "Service Account" LTS are:
- One-to-many between D_SERVICE_CLASS and D_SERVICE
- One-to-many between D_SERVICE_STATUS and D_SERVICE
The problem is that when I build a report in Answers using only the following columns of the Logical Table "Service Account":
- Service Class Desc (which exists in the Logical Table "Service Account" and in the Physical Table D_SERVICE_CLASS)
- Service Status Desc (which exists in the Logical Table "Service Account" and in the Physical Tabl D_SERVICE_STATUS)
- MSISDN (which existis in the Logical Table "Service Account" and in the Phsyical Table D_SERVICE)
the following error appears:
Estado: HY000. Código: 10058. [NQODBC] [SQL_STATE: HY000] [nQSError: 10058] A general error has occurred. [nQSError: 14065] Illegal cross join within the same dimension caused by incorrect subject area setup: [ (select * from prd.D_SERVICE where SOURCE_SYS in ('ARBOR','PPB') and DW_SERV_ST_ID in (100000003,100000009)) as T1836, D_SERVICE_CLASS T1916] with [ D_SERVICE_STATUS T1948] (HY000)
SQL emitido: SELECT "SERVICE ACCOUNT"."TLC MSISDN9" saw_0, "SERVICE ACCOUNT"."IWS Service Class Desc" saw_1, "SERVICE ACCOUNT"."TLC Service Status Desc" saw_2 FROM "VFPT - Upgrade Siebel" WHERE "SERVICE ACCOUNT"."TLC MSISDN9" = '917330340' ORDER BY saw_0, saw_1, saw_2
Help, please!
Thanks.Physically, only D_SERVICE is joined to the facts. Then D_SERVICE_CLASS is joined to D_SERVICE (one-to-many) and D_SERVICE_STATUS is joined also to D_SERVICE (one-to-many).
In the BMM, there is only one Logical Dimension Table for those 3 physical tables. This Logical Dimension Table is called "Service Account" and has 3 LTS: D_SERVICE, D_SERVICE_CLASS, D_SERVICE_STATUS. The Logical Dimension Table has several logical columns that are associated to those 3 LTS: SERVICE_KEY, SERVICE_CLASS_KEY, SERVICE_STATUS_KEY, SERVICE_MSISDN, SERVICE_CLASS_DESC, SERVICE_STATUS_DESC.
The Logical Dimension Table "Service Account" is then joined to a fact table (one-to-many).
I didn't understand the suggestion about the 3 LTS... Aren't we doing that already? Can you explain it better, please?
thanks. -
Hi,
For example: two dimension tables and one fact table.
dim1: branch
branch1
branch2
dim2: time
day1
day2
fact: sales
branch1, day2, 100
what I want in answers when using branch and time and sales without any filters:
Branch1, day1, 0
branch1, day 2, 100
Branch 2, day1, 0
Branch 2, day 2, 0
so basically I want to keep all dimensions combinations and related facts or 0 if there is no related facts. How to do it?
I know how to do it for just one dimension. That is to set up the relationship between the dimension and the fact as outer join. But how about two or more dimensions?
In SQL it will be something like dimension 1 cross join dimension 2 then left outer join the fact. How to do it in OBIEE 10 G?And I have tried set branch - fact and time - fact as outer join. In answers,
1. select time and sales
day1, null
day2, 100
2. select branch and sales
branch1, 100
branch2, null
3. select branch, time , and sales
branch 1, day 2, 100
branch 2, null, null -
[nQSError: 14065] Illegal cross join within the same dimension
Hey guys,
I'm stumped. I have two dimension tables that are joined 1:N (there is NOT an M:N relationship between them) and I have them joined in the Physical Layer and the Business Model and Mapping Layer. The two tables are F4101 (the "1") and F4102 (the "N") in the 1:N relationship. F4102 then joins to a fact table, and F4101 joins to NOTHING else. So I don't believe I have a circular condition or a need for a bridge table. Both tables are published to the Presentation Layer for reporting.
The error occurs in Answers when I want to do something as trivial as display the three primary key columns together from F4101: F4101.col1, F4101.col2, F4101.col3 (all three make up the PK). When I do that, the following error occurs:
"nQSError: 14065] Illegal cross join within the same dimension caused by incorrect subject area setup: [ F4101 T28761] with [ F4102 T1805] "
What I can't figure out is WHY the F4102 table is listed in this error. I didn't try to report on it at all. See the logical SQL below from my query:
"SQL Issued: SELECT "Item Master (F4101)".IMITM saw_0, "Item Master (F4101)".IMLITM saw_1, "Item Master (F4101)".IMAITM saw_2 FROM "Sales Analysis" ORDER BY saw_0, saw_1, saw_2"
As soon as I take out one of the three PK columns and add in another non-PK column from F4101, it works just fine. And reporting on each of the three PK columns individually works as well in Answers.
Any ideas? I would greatly appreciate it.
Thanks.Try this;
1. In the logical layer, create one folder called F4101_F4102.
2. Map both F4101 and F4102 as logical table sources in that folder.
3. Join from the folder F4101_F4102 to the fact using a Logical (new complex join) join.
Chris. -
CONNECT BY with CROSS JOIN and WHERE not executing as described in doc
Hello all,
please see these two statements. They are the same, but the 1st uses t1 INNER JOIN t2 while the snd uses a CROSS JOIN with WHERE.
The 2nd statement shows one row more than the first, but the CROSS JOIN with WHERE is the same as the INNER JOIN.
The result would be OK if Oracle did:
JOIN -> CONNECT BY PRIOR -> WHERE
But according to the docs it does:
JOIN (and WHEREs for JOINS) -> CONNECT BY PRIOR -> Rest of WHERE
See http://docs.oracle.com/cd/E11882_01/server.112/e26088/queries003.htm#SQLRF52332 for details. There it says:
Oracle processes hierarchical queries as follows:
A join, if present, is evaluated first, whether the join is specified in the FROM clause or with WHERE clause predicates.
The CONNECT BY condition is evaluated.
Any remaining WHERE clause predicates are evaluated.
+.....+
Is this a bug? I'd say yes, because it differs from the docs and it also is not what people are used to ("a,b WHERE" is the same as "a INNER JOIN b").
Thanks,
Blama
--Statement 1:
WITH t1
AS
(SELECT 1 a, 2 b FROM DUAL UNION ALL
SELECT 2 a, 3 b FROM DUAL UNION ALL
SELECT 3 a, 4 b FROM DUAL UNION ALL
SELECT 4 a, 5 b FROM DUAL UNION ALL
SELECT 5 a, 6 b FROM DUAL),
t2 AS
(SELECT 1 c FROM DUAL UNION ALL
SELECT 2 c FROM DUAL UNION ALL
SELECT 3 c FROM DUAL UNION ALL
SELECT 5 c FROM DUAL)
SELECT DISTINCT t1.a, t2.c, t1.b, level, SYS_CONNECT_BY_PATH(t1.a, '/') Path
FROM t1 INNER JOIN t2
ON t1.a = t2.c
CONNECT BY t1.a = PRIOR t1.b
START WITH t1.a = 1
ORDER BY
1,2,3;
--Result:
--1 1 2 1 /1
--2 2 3 2 /1/2
--3 3 4 3 /1/2/3
--Statement 2:
WITH t1
AS
(SELECT 1 a, 2 b FROM DUAL UNION ALL
SELECT 2 a, 3 b FROM DUAL UNION ALL
SELECT 3 a, 4 b FROM DUAL UNION ALL
SELECT 4 a, 5 b FROM DUAL UNION ALL
SELECT 5 a, 6 b FROM DUAL),
t2 AS
(SELECT 1 c FROM DUAL UNION ALL
SELECT 2 c FROM DUAL UNION ALL
SELECT 3 c FROM DUAL UNION ALL
SELECT 5 c FROM DUAL)
SELECT DISTINCT t1.a, t2.c, t1.b, level, SYS_CONNECT_BY_PATH(t1.a, '/') Path
FROM t1 CROSS JOIN t2
WHERE t1.a = t2.c
CONNECT BY t1.a = PRIOR t1.b
START WITH t1.a = 1
ORDER BY
1,2,3;
--Result:
--1 1 2 1 /1
--2 2 3 2 /1/2
--3 3 4 3 /1/2/3
--5 5 6 5 /1/2/3/4/5My details:
Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64bit Production
PL/SQL Release 11.2.0.2.0 - Production
CORE 11.2.0.2.0 Production
TNS for Solaris: Version 11.2.0.2.0 - Production
NLSRTL Version 11.2.0.2.0 - Productionblama wrote:
Hello Paul,
that means that "a,b" isn't the same as "a CROSS JOIN b".
I don't think that that is really the case.
Do you have docs on this one?No explicit docs just (my) logic - having said that, I suppose it is implied that if you are doing ANSI style joins, it's not the where clauses that
are specifying the join.
If you do t1,t2 where t1.a=t2.a then Oracle figures out the kind of join from the (relevant) where clauses and treats the other where clauses after the CONNECT BY.
If you do t1 cross join t2 where t1.a=t2.a then you are saying the join is completely specified by the CROSS JOIN - i.e it's Cartesian. Oracle doesn't look at the where clauses for this. Therefore
all where clauses are treated as 'other' and are evaluated after the CONNECT BY.
So, in my mind it's 10g that has the bug. -
Fetch multiple rows using cross-join technique
Hello.
Can anyone suggest a method to return 3 rows from a query when only one row would otherwise be returned?
I'm trying to achieve the Logical SQL analog to this -
SELECT x.foo, p.id, p.name FROM people p CROSS JOIN (SELECT 1 AS foo UNION ALL SELECT 2 UNION ALL SELECT 3) x;Reason: I want to force a three (n) row result and use GROUP BY with case statements to create 3-level summary.
Here is a simple Logical SQL expression that works for my OBI setup -
SELECT
"- Nx_CSDG0_Repair_Orders (Depot Repair Views)".Repair_Number saw_0,
"- Nx_CSDG0_Repair_Orders (Depot Repair Views)".SR_Operating_Unit_Name saw_1
FROM
"[Noetix-NoetixGlobalRepository] NoetixViews for Oracle Service"
WHERE
("- Nx_CSDG0_Repair_Orders (Depot Repair Views)".Repair_Number = '338246')But I think I cannot do this -
SELECT
"- Nx_CSDG0_Repair_Orders (Depot Repair Views)".Repair_Number saw_0,
"- Nx_CSDG0_Repair_Orders (Depot Repair Views)".SR_Operating_Unit_Name saw_1
FROM
"[Noetix-NoetixGlobalRepository] NoetixViews for Oracle Service"
CROSS JOIN (SELECT 1 AS foo UNION ALL SELECT 2 UNION ALL SELECT 3)
WHERE
("- Nx_CSDG0_Repair_Orders (Depot Repair Views)".Repair_Number = '338246')But what can I do?
Thank you.
-csHi CSeelig ,
The BI Server uses the ANSI SQL standard. Then all SQL that follows this specification will in OBIEE work.
I have made an example with a densification:
http://gerardnico.com/wiki/dat/obiee/logical_sql/obiee_sql_densification
You will see a cross join to make a densification.
Cheers
Nico -
Formula :using filter with cross join issue
Hi alll,
Ihave one scenario like calclate the ytd values using cross join like
IIF(is ([Flow].CurrentMember,[YTD]),
(case
when
is ([PERIOD].CurrentMember,[2008.01])
Then (Sum (crossjoin({[DI-3]},CrossJoin({[FLOW].[MtD]},{[PERIOD].[2008.01],[PERIOD].[2008.01] })))/2)
when
is ([PERIOD].CurrentMember,[2008.02])
Then (Sum (crossjoin({[DI-3]},crossjoin({[FLOW].[MtD]},{[PERIOD].[2008.01],[PERIOD].[2008.02]}))))
when
is ([PERIOD].CurrentMember,[2008.03])
Then (Sum (crossjoin({[DI-3]},CrossJoin({[FLOW].[MtD]},{[PERIOD].[2008.01],[PERIOD].[2008.02],[PERIOD].[2008.03]))))
etc,othercase)
but I am trying other oneto decrese the size of formula
IIF(is ([Flow].CurrentMember,[YTD]),
(Sum (crossjoin({[DI-3]},CrossJoin({[FLOW].[MtD]},
{filter([PERIOD].Generations(5).members,StrToNum(Right([PERIOD].CurrentMember.MEMBER_NAME,2)) <= StrToNum(Right([PERIOD].CurrentMember.MEMBER_NAME,2)) AND StrToNum(Left([PERIOD].CurrentMember.MEMBER_NAME,4))=StrToNum(left([PERIOD].CurrentMember.MEMBER_NAME,4)) ) }))))
, othercase )
but its result is same value for all the YTDs for every month, something is going wrong some where
plz any help would be appriociated
Thanks in advance
.macearl wrote:
SQL View does not display unless data is returned by the query. Is that normal?
Also, none of these options shows the literal result of the expression we built, i.e.:
expression: CAST(YEAR(TIMESTAMPADD(SQL_TSI_MONTH, -24, CURRENT_DATE)) as CHAR)
result: *2008*
Having the ability to test expressions and see their results would be very helpful in debugging. If anyone knows how to do that please share!
Thanks!
MacOk, Probably shoud have figured this out before, but in response to my own question, the way to view the result of an expression is to add the expression as a column and include it in the Table Presentation.
- Mac (he can be taught) -
Why is a cross join classified as an inner join?
hi guys,
searching the web is just confusing me, so maybe someone here could help.
On wikipedia (for join) it says:
An outer join does not require each record in the two joined tables to have a matching record
Now, a cross join does not require any of the records of the table to have a matching record, therefore I would classify it as an outer join. Why then is it classified as an inner join?
thanksCross join produces the cross-product. You can not specify cross join condition since there is none - it always implies every row in table1 joined with every row in table2:
SQL> select ename,dname from emp cross join dept on(1=1)
2 /
select ename,dname from emp cross join dept on(1=1)
ERROR at line 1:
ORA-00933: SQL command not properly ended
SQL> With outer join you must specify join condition:
SQL> select ename,dname from emp left join dept
2 /
select ename,dname from emp left join dept
ERROR at line 1:
ORA-00905: missing keyword
SQL> And join result rules are completely different - matching rows or NULL row.
SY. -
Hello,
I've written the following query, which works fine:
select * from
select
to_char(SCB_OPENTIME, 'YYYY-MM') as curr_date,
SCB_TASK
from EM_DATA_MSR_SC
pivot
count(SCB_TASK) for curr_date in
'2011-01',
'2011-02',
'2011-03',
'2011-04',
'2011-05',
'2011-06',
'2011-07',
'2011-08',
'2011-09',
'2011-10',
'2011-11',
'2011-12'
);Now I need to apply a cross join on it:
select * from
select
to_char(SCB_OPENTIME, 'YYYY-MM') as curr_date,
SCB_TASK
from EM_DATA_MSR_SC
where
EMSCG_STATUS = status.name
pivot
count(SCB_TASK) for curr_date in
'2011-01',
'2011-02',
'2011-03',
'2011-04',
'2011-05',
'2011-06',
'2011-07',
'2011-08',
'2011-09',
'2011-10',
'2011-11',
'2011-12'
cross join
select 0, 'Closed' as name from DUAL union
select 1, 'Denied' from DUAL union
select 2, 'Open' from DUAL
) status;When I try to execute this query, I get this error message:
"STATUS"."NAME": invalid identifierHowever, SQL is supposed to have table references scoped to one level deep.
Am I doing something wrong?Unfortunately, your snippet doesn't work either.
Here is a simple test case:
CREATE TABLE TEST
NUM NUMBER NOT NULL,
DAT DATE NOT NULL
insert into TEST (NUM, DAT) values (32, SYSDATE - 43);
insert into TEST (NUM, DAT) values (9, SYSDATE - 143);
insert into TEST (NUM, DAT) values (90, SYSDATE - 13);
insert into TEST (NUM, DAT) values (24, SYSDATE - 3);
insert into TEST (NUM, DAT) values (244, SYSDATE - 39);
insert into TEST (NUM, DAT) values (44, SYSDATE - 9);
insert into TEST (NUM, DAT) values (44, SYSDATE - 9);
insert into TEST (NUM, DAT) values (44, SYSDATE - 9);
insert into TEST (NUM, DAT) values (60, SYSDATE - 9);
insert into TEST (NUM, DAT) values (51, SYSDATE - 9);
insert into TEST (NUM, DAT) values (44, SYSDATE - 9);
insert into TEST (NUM, DAT) values (0, SYSDATE - 3);
insert into TEST (NUM, DAT) values (16, SYSDATE - 11);
insert into TEST (NUM, DAT) values (24, SYSDATE - 9);
insert into TEST (NUM, DAT) values (59, SYSDATE - 27);
insert into TEST (NUM, DAT) values (29, SYSDATE - 16);
insert into TEST (NUM, DAT) values (20, SYSDATE - 47);
insert into TEST (NUM, DAT) values (103, SYSDATE - 1);
insert into TEST (NUM, DAT) values (41, SYSDATE - 92);
insert into TEST (NUM, DAT) values (42, SYSDATE - 40);Here's the corresponding working query:
select * from
select
to_char(DAT, 'YYYY-MM') as curr_date,
NUM
from TEST
pivot
count(NUM) for curr_date in
'2011-01',
'2011-02',
'2011-03',
'2011-04',
'2011-05',
'2011-06',
'2011-07',
'2011-08',
'2011-09',
'2011-10',
'2011-11',
'2011-12'
;Now, say I want to display the number of NUMs grouped by range values and by DAT. I'd like to have this kind of output (each # is a different number):
-------- 2011-01 2011-02 2011-03 2011-04 2011-05 2011-06 2011-07 2011-08 2011-09 2011-10 2011-11 2011-12
0 - 20 # # # # # # # # # # # #
21 - 50 # # # # # # # # # # # #
51 - 300 # # # # # # # # # # # #To do so, I'd like to write something like this:
select * from
select
to_char(DAT, 'YYYY-MM') as curr_date,
NUM
from TEST
where NUM >= ranges.mini and NUM <= ranges.maxi
pivot
count(NUM) for curr_date in
'2011-01',
'2011-02',
'2011-03',
'2011-04',
'2011-05',
'2011-06',
'2011-07',
'2011-08',
'2011-09',
'2011-10',
'2011-11',
'2011-12'
cross join
select 0, 0 as mini, 20 as maxi from DUAL union
select 1, 21, 50 from DUAL union
select 2, 51, 300 from DUAL
) ranges
;Alas, this isn't a valid query:
"RANGES"."MAXI": invalid identifierUsing a WITH clause doesn't help (same error message):
with ranges as
select 0, 0 as mini, 20 as maxi from DUAL union
select 1, 21, 50 from DUAL union
select 2, 51, 300 from DUAL
select * from
select
to_char(DAT, 'YYYY-MM') as curr_date,
NUM
from TEST
where NUM >= ranges.mini and NUM <= ranges.maxi
pivot
count(NUM) for curr_date in
'2011-01',
'2011-02',
'2011-03',
'2011-04',
'2011-05',
'2011-06',
'2011-07',
'2011-08',
'2011-09',
'2011-10',
'2011-11',
'2011-12'
cross join ranges
;EDIT:
Always say which version of Oracle you're using.Oracle Database 11g Enterprise Edition Release 11.1.0.7.0 - 64bit Production
PL/SQL Release 11.1.0.7.0 - Production
"CORE 11.1.0.7.0 Production"
TNS for Solaris: Version 11.1.0.7.0 - Production
NLSRTL Version 11.1.0.7.0 - Production
EDIT 2:
SCB_TASK replaced by NUM in queries
Edited by: 895536 on 7 nov. 2011 09:01 -
Illegal cross join within the same dimension
Hi,
When certain fields are selected within the presentation table an "illegal cross join" error is returned by the BI Server. However if a FACT is added from one of the other presentation tables the "illegal cross join" error goes away. we need to query without fact column.
We are getting following error
State: HY000. Code: 10058. [NQODBC] [SQL_STATE: HY000] [nQSError: 10058] A general error has occurred. [nQSError: 14065] Illegal cross join within the same dimension caused by incorrect subject area setup: [ CALL_CENTER.COUNSELOR_MANAGER T782130] with [ CALL_CENTER.COUNSELOR_HR T781594 On CALL_CENTER.COUNSELOR_HR.MASTER_STAFF_COUNSELOR_ID = CALL_CENTER.MASTER_STAFF_COUNSELOR.MASTER_STAFF_COUNSELOR_ID, CALL_CENTER.MASTER_STAFF_COUNSELOR T781739] (HY000)
Can anybody help me solving this issue.
Thanks,
KS.Please give us an example of what you need.
OBIEE perform a query in the dimension or through the fact table.
You can't join two dimensions in the repository without going through a fact table.
If you need to query without fact column, it's because you have design two dimensions where
normally you can do one.
You have then two solutions :
* change the design of your logical model to make only one dimension.
* use the OBIEE logical SQL in answer.
http://gerardnico.com/wiki/dat/obiee/bi_server/design/obiee_logical_sql
Success
Nico -
Can cross join be used with other joins
hi,
Q1) is this syntactically correct , cross join with other joins like inner outer. like following.
select * from t1 cross join t2
inner join t3 on cast(t3.c1 as varchar) = (cast(t1.id as varchar) + cast(t2.t2 as varchar))
I have seen it works but wanted to know is it syntactically correct.
yours sincerelyHi
First, Sorry, I wanted to VOTE (as I wrote) and not to Propose as answer :-)
Join are not necessarily executing in the order we write them. I can show simple example with tables that include the amount of rows is very different.
/**************************************************** DDL - Create tables */
CREATE TABLE T1 (
ID INT IDENTITY CONSTRAINT PK_T1 PRIMARY KEY,
MyValue UNIQUEIDENTIFIER DEFAULT NEWID()
CREATE TABLE T2 (
ID INT IDENTITY CONSTRAINT PK_T2 PRIMARY KEY,
MyValue UNIQUEIDENTIFIER DEFAULT NEWID()
CREATE TABLE T3 (
ID INT IDENTITY CONSTRAINT PK_T3 PRIMARY KEY,
MyValue UNIQUEIDENTIFIER DEFAULT NEWID()
GO
/**************************************************** DML - Populate Tables*/
SET NOCOUNT ON;
insert T1 (MyValue)
select top 100 null
from _ArielyAccessoriesDB.dbo.ArielyNumbers
GO
insert T2 (MyValue)
select top 500 null
from _ArielyAccessoriesDB.dbo.ArielyNumbers
GO
insert T3 (MyValue)
select top 10000 null
from _ArielyAccessoriesDB.dbo.ArielyNumbers
GO
/**************************************************** Play Time */
select T1.MyValue, T2.MyValue, T3.MyValue
from T3
join T2 on T2.ID = T3.ID
join T1 on T1.ID = T2.ID
GO
-- Check Execution Plan [EP]
-- Notice that SQL Server has changed the join order from T3-T2-T1 to T1-T2-T3 because it’s better that way.
-- You can notice that the order was by the number of rowns in tables from the smallest to the bigest SET
hope this is helpful :-)
[Personal Site] [Blog] [Facebook] -
I can't figure out how to combine a full outer join with another type of join ... is this possible?
Here's some create table and insert statements for some basic sample data:
CREATE TABLE my_tab1
( record_id NUMBER NOT NULL
, workstation VARCHAR2(4)
, my_value NUMBER
CONSTRAINT my_tab1_pk PRIMARY KEY (record_id)
INSERT INTO my_tab1
VALUES(1,'ABCD',10);
INSERT INTO my_tab1
VALUES(2,'ABCD',15);
INSERT INTO my_tab1
VALUES(3,'ABCD',5);
INSERT INTO my_tab1
VALUES(4,'A123',5);
INSERT INTO my_tab1
VALUES(5,'A123',10);
INSERT INTO my_tab1
VALUES(6,'A123',20);
INSERT INTO my_tab1
VALUES(7,'????',5);
CREATE TABLE my_tab2
( workstation VARCHAR2(4)
, wkstn_name VARCHAR2(20)
CONSTRAINT my_tab2_pk PRIMARY KEY (workstation)
INSERT INTO my_tab2
VALUES('ABCD','WKSTN 1');
INSERT INTO my_tab2
VALUES('A123','WKSTN 2');
INSERT INTO my_tab2
VALUES('B456','WKSTN 3');
CREATE TABLE my_tab3
( my_nbr1 NUMBER
, my_nbr2 NUMBER
INSERT INTO my_tab3
VALUES(1,2);
INSERT INTO my_tab3
VALUES(2,3);
INSERT INTO my_tab3
VALUES(3,4);And, the results I want to get:
workstation sum(my_value) wkstn_name my_nbr1 my_nbr2
ABCD 30 WKSTN 1 1 2
ABCD 30 WKSTN 1 2 3
ABCD 30 WKSTN 1 3 4
A123 35 WKSTN 2 1 2
A123 35 WKSTN 2 2 3
A123 35 WKSTN 2 3 4
B456 0 WKSTN 3 1 2
B456 0 WKSTN 3 2 3
B456 0 WKSTN 3 3 4
???? 5 NULL 1 2
???? 5 NULL 2 3
???? 5 NULL 3 4I've tried a number of different things, googled my problem, and no luck yet...
SELECT t1.workstation
, SUM(t1.my_value)
, t2.wkstn_name
, t3.my_nbr1
, t3.my_nbr2
FROM my_tab1 t1
, my_tab2 t2
, my_tab3 t3
...So, what I want is a full outer join of t1 and t2 on workstation, and a cross-join of that with t3. I'm wondering if I can't find any examples of this online because it's not possible....
Note: I'm stuck dealing with Oracle 8i
Thanks!!Hi,
The query I posted yesterday is a little more complicated than it needs to be.
Since my_tab2.workstation is unique, there's no reason to do a separate sub-query like mt1; we can join my_tab1 to my_tab2 and get the SUM all in one sub-query.
SELECT foj.workstation
, foj.sum_my_value
, foj.wkstn_name
, mt3.my_nbr1
, mt3.my_nbr2
FROM ( -- Begin in-line view foj for full outer join
SELECT mt1.workstation
, SUM (mt1.my_value) AS sum_my_value
, mt2.wkstn_name
FROM my_tab1 mt1
, my_tab2 mt2
WHERE mt1.workstation = mt2.workstation (+)
GROUP BY mt1.workstation
, mt2.wkstn_name
UNION ALL
SELECT workstation
, 0 AS sum_my_value
, wkstn_name
FROM my_tab2
WHERE workstation NOT IN ( -- Begin NOT IN sub-query
SELECT workstation
FROM my_tab1
WHERE workstation IS NOT NULL
) -- End NOT IN sub-query
) foj -- End in-line view foj for full outer join
, my_tab3 mt3
ORDER BY foj.wkstn_name
, foj.workstation
, mt3.my_nbr1
, mt3.my_nbr2
;Thanks for posting the CREATE TABLE and INSERT statements, as well as the very clear desired results!
user11033437 wrote:
... So, what I want is a full outer join of t1 and t2 on workstation, and a cross-join of that with t3. That it, exactly!
The tricky part is how and when to get SUM (my_value). You might approach this by figuring out exactly what my_tab3 has to be cross-joined to; that is, exactly what should the result set of the full outer join between my_tab1 and my_tab2 look like. To do that, take your desired results, remove the columns that do not come from the full outer join, and remove the duplicate rows. You'll get:
workstation sum(my_value) wkstn_name
ABCD 30 WKSTN 1
A123 35 WKSTN 2
B456 0 WKSTN 3
???? 5 NULL So the core of the problem is how to get these results from my_tab1 and my_tab2, which is done in sub-query foj above.
I tried to use self-documenting names in my code. I hope you can understand it.
I could spend hours explaining different parts of this query in more detail, but I'm sure I'd waste some of that time explaining things you already understand. If you want an explanation of somthing(s) specific, let me know. -
OBIEE: non empty cross join function problem with some reports
Hi all,
I am getting some problem ,when we are excuting some requests in OBIEE its taking long time , Actually our OBIEE is connected with Essbase when we are generating some reports its taking long time so i captured the Query in OBIEE and Iam excuting that query in Essbase MDX editor
its having the the function " NON EMPTy CROSS JOIN"
which reports having non empty crossjoin function its taking long time so how can i disable this function in OBIEEfor that reports
I dont want to use that function for my reports so how can i do this in OBIEE
your help would be appriciated.
Thanks
Edited by: user8815661 on 26 mai 2010 08:44Any Help
-
How to avoid duplicates in CROSS JOIN Query
Hi,
I am using CROSS JOIN to get all the subset of a table col values as shown below:
PRODUCT (Col Header)
Bag
Plate
Biscuit
While doing cross join we will get as
Bag Bag
Bag Plate
Bag Biscuit
Plate Bag
Plate Plate
Plate Biscuit ..... like this
By placing where condition prod1 <> prod2 to avoid Bag Bag and Plate Plate values. So the output will be like below
Bag Plate
Bag Biscuit
Plate Bag
Plate Biscuit
Now "Bag Plate" and "Plage Bag" are same combination how to avoid these records. My expected result is
Bag Biscuit
Plate Biscuit
How to derive this ?
SridharHi,
This is the the solution that I found as fit to the OP question, but
Visakh16 already posted the same idea (assuming the names are unique) from the start and I don't think that anyone notice it!
Sridhar.DPM did
you check Visakh16's response
(the second response received)?!?
I will mark his response as an answer. If this is not what you need pls clarify and you can unmark it :-)
[Personal Site] [Blog] [Facebook]
Maybe you are looking for
-
Only some of my photo's are syncing from iPhoto to my iPad, iPhone and Apple TV.
Only some of my photo's are syncing from iPhoto to my iPad, iPhone and Apple TV. It may be my own fault due to learning Aperture and playing around with the locations of the Master Files. The thing I don't understand is that when in iPhoto all thumbn
-
How do I load data from R/3 into /BI0/TCUST_SALES
Hi, There is a SAP supplied characteristic called 0CUST_SALES which has a text table called /BI0/TCUST_SALES. How do go about populating this table from the R/3 system? My version is BW 3.5 Thanks Ram
-
Hello Guys, I made this post in my Blog and I want you to review it and correct it if you please. http://cisco-discussions.blogspot.com/2013/08/private-vlans-and-its-different-types.html PVLAN types of Trunk Links The different types of PVLAN trunk l
-
ADF-BPM integration problem.
Hi, 1.I have a small scenario like integrate oracle ADF screen with Oracle BPM Human Task. we didn't use auto generate task form(we are using one adf application which is already developed in Oracle Adf technology). 2.Those adf screen is populated in
-
How to hide or disable the create attachment option from GOS tool bar..? User shouldnt able to do this using create attachment, please suggest any solution.