cross join?
I was asked if the veiw (select what is wrttien to make the view) has a cross join and follwing some ANSI compliance. So my question is... as table 3 below is used by alias tp and same table3 is used by alias tp2... commandeer that be considered as a cross join?Sorry, I'm very new to SQL...
select col1, col2, col3 from table1 t, table2 t1, table3 tp
where
---
---
---
AND tp.phs_id =
(SELECT tp2.phs_id
FROM table3 tp2
WHERE tp2.id = tsp.id
AND ROWNUM < 2)
Published by: user11168115 on May 14, 2009 12:43
Hello (and welcome)
No, would not be a CROSS JOIN, since your alias tp2 JOINED tp (I guess you mean tp even if you have a TSP). A CROSS JOIN is where no JOIN condition at all is specified for a table (also known as the Cartesian product ), and if it's compatible ANSI SQL will actually be the words CROSS JOIN specified in the JOIN clause. For example:
SELECT *
FROM table1
CROSS JOIN table2;
Otherwise, non-compliant ANSI:
SELECT *
FROM table1, table2;
Tags: Database
Similar Questions
-
Hello.
Why Cross join on a table without specifying alias does not work properly ?
with t1 as ( select 1 as q1, 2 as q2 from dual union select 3, 4 from dual union select 5, 6 from dual ) select * from t1 cross join t1 order by 1 Q1 Q2 Q1 Q2 ---------- ---------- ---------- ---------- 1 2 1 2 1 2 1 2 1 2 1 2 3 4 3 4 3 4 3 4 3 4 3 4 5 6 5 6 5 6 5 6 5 6 5 6 9 rows selected
11.2.0.3.0 - 64 bit
2837285 wrote:
Ok.
Why 10 gr 2 we have error ORA-00918 (same self-join) and 11 g works, but the wrong result?
Why don't you ask Oracle via a support request, it's a bug.
You can't say he is working on 11g "but which gives bad result." Clearly if it is to give a wrong result then it doesn't work.
What happens on 11g is the fact that it runs without the exception of ambiguous column. There are a few known bugs in the way SQL ANSI has implemented compared to the regular SQL Oracle syntax. Oracle worked to correct.
I don't have 12 c to test, but it may have already been corrected in this version.
-
Hi all
Two tables which have no relationship with each other. For example, an Employees table and a systemparameter with a startworktime table.
We need a query with the data in the two tables:
Get all employees and the startworktime (which is the same for everyone)
Which is cheaper: an inline query or a product Cartesian or crossjoin?
Inine:
Select name, function
(by selecting startworktime in systemparameter)
employees;
Cartesian product:
SELECT name, function, startwoime
rktfrom used
Cross join systemparameter;
Your opinion about this.
Both these do the same thing. I seriously doubt if we would have the benefits of performance on the other.
Kind regards
-
Difference between a CROSS JOIN and a Cartesian product of the noted comma?
Hello everyone,
Oracle version: Oracle Database 11 g Enterprise Edition Release 11.2.0.1.0 - 64 bit
OS: Linux Fedora Core 17 (x86_64)
I was practicing on recursive subquery factoring based on oracle examples available in the documentation
http://docs.Oracle.com/CD/E11882_01/server.112/e26088/statements_10002.htm#i2129904
I was working on an example that displays the hierarchy of each manager with related employees. Here's how
Which gives the desired resultWITH tmptab(empId, mgrId, lvl) AS ( SELECT employee_id, manager_id, 0 lvl FROM employees WHERE manager_id IS NULL UNION ALL SELECT employee_id, manager_id, lvl+1 FROM employees, tmptab WHERE (manager_id = empId) ) SEARCH DEPTH FIRST BY mgrId SET order1 SELECT LPAD(' ', lvl * 3, ' ') || empId AS empId FROM tmptab;
However, by chance, I noticed that if I put CROSS JOIN instead of put a comma between table names, the same query behaves differently.EMPID --------------------- 100 101 108 109 110 111 112 113 200 203 204 205 206 102 103 104 105 106 107 114 115 116 117 118 119 120 125 126 127 128 180 181 182 183 121 129 130 131 132 184 185 186 187 122 133 134 135 136 188 189 190 191 123 137 138 139 140 192 193 194 195 124 141 142 143 144 196 197 198 199 145 150 151 152 153 154 155 146 156 157 158 159 160 161 147 162 163 164 165 166 167 148 168 169 170 171 172 173 149 174 175 176 177 178 179 201 202 107 rows selected. SQL>
In other words, if instead of writing
I am writing. . . UNION ALL SELECT employee_id, manager_id, lvl+1 FROM employees, tmptab WHERE (manager_id = empId)
I get the following error message. . . UNION ALL SELECT employee_id, manager_id, lvl+1 FROM employees CROSS JOIN tmptab WHERE (manager_id = empId)
Any idea?ERROR at line 4: ORA-32044: cycle detected while executing recursive WITH query
Correct me if I'm wrong, but I remember, oracle supports as many JOIN CROSSROADS notation for Cartesian product (vector product =). For example
So if the two rated commas and CROSS JOIN have the same semantics, why do I get a cycle mentioned above cites recursive subquery factoring while the same query works pretty well with comma between table instead of CROSS JOIN names? Because if a cycle is detected (= current element ancestor) it means that the product with the CROSS JOIN notation produces duplicates which are absent in the result of the Cartesian product rated comma.SQL> WITH tmptab1 AS 2 ( 3 SELECT 'a1' AS colval FROM DUAL UNION ALL 4 SELECT 'a2' AS colval FROM DUAL UNION ALL 5 SELECT 'a3' AS colval FROM DUAL 6 ), 7 tmptab2 AS 8 ( 9 SELECT 'b1' AS colval FROM DUAL UNION ALL 10 SELECT 'b2' AS colval FROM DUAL 11 ) 12 SELECT t1.colval, t2.colval 13 FROM tmptab1 t1 CROSS JOIN tmptab2 t2; CO CO -- -- a1 b1 a2 b1 a3 b1 a1 b2 a2 b2 a3 b2 6 rows selected. SQL> LIST 13 13* FROM tmptab1 t1 CROSS JOIN tmptab2 t2 SQL> SQL> SQL> CHANGE /CROSS JOIN/, 13* FROM tmptab1 t1 , tmptab2 t2 SQL> SQL> SQL> LIST 1 WITH tmptab1 AS 2 ( 3 SELECT 'a1' AS colval FROM DUAL UNION ALL 4 SELECT 'a2' AS colval FROM DUAL UNION ALL 5 SELECT 'a3' AS colval FROM DUAL 6 ), 7 tmptab2 AS 8 ( 9 SELECT 'b1' AS colval FROM DUAL UNION ALL 10 SELECT 'b2' AS colval FROM DUAL 11 ) 12 SELECT t1.colval, t2.colval 13* FROM tmptab1 t1 , tmptab2 t2 SQL> SQL> / CO CO -- -- a1 b1 a2 b1 a3 b1 a1 b2 a2 b2 a3 b2 6 rows selected. SQL>
I would appreciate it if you could kindly shed some light.
Thanks in advance,
Kind regards
DariyooshHello
dariyoosh wrote:
... Oracle terminology could become really confusing. But once again, according to the online glossary, a Cartesian product is apparently regarded as a join
http://docs.Oracle.com/CD/E11882_01/server.112/e25789/glossary.htm?type=popup#CNCPT44493
>There is no doubt that a Cartesian product (also called cross join) is a join. If loops in a WITH recursive clause are detected after completing the joins, but before other conditions apply, the relevant question here is "what are the requirements to join?
In the ANSI syntax, the distinction is always clear. Join conditions occur in the... Clause WESELECT employee_id, manager_id, lvl + 1 FROM employees JOIN tmptab ON (manager_id = empId) -- Join condition ;
and other conditions occur in the WHERE (or HAVING or CONNECT BY) clause.
SELECT employee_id, manager_id, lvl + 1 FROM employees CROSS JOIN tmptab WHERE (manager_id = empId) -- NOT a join condition ;
In the joins of the former, it seems to be the case that any condition involving 2 or more tables (or the + indicator of outer join) is a condtion of join:
SELECT employee_id, manager_id, lvl + 1 FROM employees , tmptab WHERE (manager_id = empId) -- Join condition ;
-
Hello
I use OBIEE version 11.1.1.6.0. I created made dummy table and column in the physical layer. and join all the dimension table with this fact. but I don't know how the business model to deal with it. can someone help me to create a cross join?
Thank you
Published by: 968086 on October 30, 2012 12:59 AMFollow this link
http://www.rittmanmead.com/2009/08/Oracle-BI-EE-10-1-3-4-1-reporting-on-non-transactional-dimension-values-equivalence-of-outer-joins/Score pls help if
-
Extract multiple lines using the technique of the cross join
Hello.
Can someone suggest a method to return 3 lines of a query when otherwise would return only one line?
I am trying to reach the analog SQL logic to this topic-
Reason: I want a result three rows (n) force and use GROUP BY with case statements to create 3 levels summary.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;
Here is a simple SQL logical expression that works for my setup - OBI
But I think that I can't do--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 what peut do?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')
Thank you.
-csHi CSeelig,
The BI server uses the ANSI SQL standard. Then all the SQL that follows this specification in OBIEE will work.
I did an example with intensification:
http://gerardnico.com/wiki/dat/OBIEE/logical_sql/obiee_sql_densificationYou will see a cross join to make a densification.
See you soon
Nico -
Hello
I wrote the following query, which works very well:
Now, I need to apply a cross thereon join: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' ) );
When I try to run this query, I get this error message: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;
However, SQL is supposed to have brought on a deep level table references."STATUS"."NAME": invalid identifier
I'm doing something wrong?Hello
If this is what you want, then you don't want a cross join. The inner join in the view online with no name is the only reference to beaches you need.
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 r.mini, r.maxi, to_char (t.DAT, 'YYYY-MM') as curr_date, t.NUM from TEST t join RANGES r on t.NUM >= r.mini and t.NUM <= r.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' ) ) ORDER BY mini ;
Depending on your data and your needs, you might want an outer join, not an inner join, like this:
... from RANGES r LEFT OUTER JOIN TEST t on t.NUM >= r.mini and t.NUM <= r.maxi
Published by: Frank Kulash, November 7, 2011 12:20
Alternative ADED of outer join -
[8i] need help with full outer join combined with a cross join...
I can't understand how to combine a full outer join with a different type of join... is it possible?
Here are some create table and insert for examples of database:
And, the results that I want to get: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);
I tried a number of different things, google my problem and no luck yet...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 4
So, what I want, it's a full outer join of t1 and t2 on workstation and a cross join of one with the t3. I wonder if I can't find examples of it online because it is not possible...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 ...
Note: I'm stuck dealing with Oracle 8i
Thank you!!Hello
The query I posted yesterday is a little more complex that it should be.
My_tab2.workstation is unique, there is no reason to make a separate subquery as mt1. We can join my_tab1 to my_tab2 and get the SUM in a subquery.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, and very clear expected results!
user11033437 wrote:
... So, what I want, it's a full outer join of t1 and t2 on workstation and a cross join of one with the t3.She, exactly!
The trickiest part is when and how get SUM (my_value). You could address the question of exactly what my_tab3 must be attached to a cross that's exactly what should look like the result set of the full outer join between my_tab1 and my_tab2 to. To do this, take your desired results, remove columns that do not come from the outer join complete and delete duplicate rows. You will get:workstation sum(my_value) wkstn_name ----------- ------------- ---------- ABCD 30 WKSTN 1 A123 35 WKSTN 2 B456 0 WKSTN 3 ???? 5 NULL
So the heart of the problem is how to get these results of my_tab1 and my_tab2, which is done in the subquery FOJ above.
I tried to use auto-documenté in my code names. I hope you can understand.
I could spend hours explaining the different parts of this query more in detail, but I don't know that I would lose some of that time, explain things that you already understand. If you want an explanation of the specific element (s), let me know. -
Hello
I have below the text values.
Platimum
Silver
VIP
Bronze
I want to get cross-from above like four
Platimum Silver VIP Bronze Platimum VIP Silver Bronze Platimum Bronze VIP Silver Platimum Silver VIP Bronze And so on. It should give me 16 combinations that I guess.
How can I achieve this?
Kind regards
Mahesh
Another way to generate permutations (if nocycle seems suspicious)
with
data in the form of
(select "Platimum" val of union double all the)
Select 'Cash' of all the double union
Select 'VIP' of all the double union
Select "Bronze" double
),
permutator (swap) as
(select val
from the data
Union of all the
Select swap. «, » || Val
data,.
permutator
where instr (permutation, val) = 0
)
Select the permutation
of the permutator
where regexp_count (permutation, ',') = 3
order by swapping
PERMUTATION Bronze, Platimum, silver, VIP Bronze, Platimum, VIP, Silver Bronze, Silver, Platimum, VIP Bronze, Silver, VIP, Platimum Bronze, VIP, Platimum, Silver Bronze, Silver, VIP, Platimum Platimum, Bronze, silver, VIP Platimum, Bronze, Silver, VIP Platimum, silver, Bronze, VIP Platimum, silver, VIP, Bronze Platimum, VIP, Bronze, silver Platimum, VIP, silver, Bronze Silver, Bronze, Platimum, VIP Silver, Bronze, VIP, Platimum Money, Platimum, Bronze, VIP Silver, Platimum, VIP, Bronze Silver, VIP, Bronze, Platimum Silver, VIP, Platimum, Bronze VIP, Bronze, Platimum, silver VIP Bronze, silver, Platimum VIP, Platimum, Bronze, silver VIP, Platimum, silver, Bronze VIP, silver, Bronze, Platimum VIP, money, Platimum, Bronze -
Expected to a join behaviours?
Hello
formalities:
Select * from version of v$.
Oracle Database 11 g Enterprise Edition Release 11.2.0.3.0 - 64 bit Production
PL/SQL Release 11.2.0.3.0 - Production
CORE Production 11.2.0.3.0
AMT for 64-bit Windows: Version 11.2.0.3.0 - Production
NLSRTL Version 11.2.0.3.0 - Production
the question:
I was just wondering why this "join" between A and B do not work. A has no lines, B a 1 row
> my first answer... it should give 1 rank...
but alas... it doesn't!
the original request:
SELECT A.*, B.*, CASE WHEN A.METH = 'PAL' THEN '1' ELSE A.CFGNR_OFRESTPAL END VWNR_OFRESTPAL FROM CDCROBOTVERDICHTLOCSTATVW A, CDCVERDICHTSETTINGSVW B
> No rows returned
so I checked the counts:
Select count (*) in the CDCROBOTVERDICHTLOCSTATVW A;
0
Select count (*) in the CDCVERDICHTSETTINGSVW B;
1
Hmmm... very strange
so I ran a few tests more:
SELECT A.*, B.*, CASE WHEN A.METH = 'PAL' THEN '1' ELSE A.CFGNR_OFRESTPAL END VWNR_OFRESTPAL FROM CDCVERDICHTSETTINGSVW B , CDCROBOTVERDICHTLOCSTATVW A
> no line
SELECT A.*, B.*, CASE WHEN A.METH = 'PAL' THEN '1' ELSE A.CFGNR_OFRESTPAL END VWNR_OFRESTPAL FROM CDCVERDICHTSETTINGSVW B cross join CDCROBOTVERDICHTLOCSTATVW A
> no line
SELECT A.*, B.*, CASE WHEN A.METH = 'PAL' THEN '1' ELSE A.CFGNR_OFRESTPAL END VWNR_OFRESTPAL FROM CDCVERDICHTSETTINGSVW B full outer join CDCROBOTVERDICHTLOCSTATVW A on 1 =1
> This returns the line from B correctly
ARTNR PROJECTNR VARIANT PROVIDER QMAX PRIO METH PALLETS SOMQTY FULLPALS RESTQTY RESTPALS CFGNR_OFRESTPAL FREELOCS DUMMYRECIP GEEN_VERDICHTING MAXSRCPALS MAXOPENPICKSETS NR_OF_RESTPAL ORDTYPE PRIO_ARTNR VERD_INRGRP VWNR_OFRESTPAL VERDICHT 11 4 3 VERDICHT QSE00918CLL80 CDCROB SELECT A.*, B.*, CASE WHEN A.METH = 'PAL' THEN '1' ELSE A.CFGNR_OFRESTPAL END VWNR_OFRESTPAL FROM CDCVERDICHTSETTINGSVW B left join CDCROBOTVERDICHTLOCSTATVW A on 1 = 1
> it is also returns the same rank B
Does anyone have an idea on how to fix this... or where to look to fix this?
This is a cross join. It is a Cartesian product. If there are no rows in a table, then you get no rows in the result.
SQL> with a as (select 1 x from dual), b as (select 1 y from dual where 1=0) 2 select a.*, b.* 3 from a, b; no rows selected
A left or right, or full outer join is apparently what you wanted.
-
Hello
I have the following data
create or replace view offdays as
(
Select 1 empid, to_date('23/01/2015','dd/mm/yyyy') dt, 1 offday for all double union
Select 2 empid, to_date('23/01/2015','dd/mm/yyyy') dt, 1 offday for all double union
Select 3 empid, to_date('21/01/2015','dd/mm/yyyy') dt, 1 double offday
);
create or replace view payrolldates as
(
Select to_date('21/01/2015','dd/mm/yyyy') as dt of union double all the
Select to_date('22/01/2015','dd/mm/yyyy') as dt of union double all the
Select to_date('23/01/2015','dd/mm/yyyy') as dt of union double all the
Select to_date('24/01/2015','dd/mm/yyyy') as dt of union double all the
Select to_date (' 25/01/2015 ',' dd/mm/yyyy') in the double dt
);
Select * from payrolldates and b offdays left outer join on b.dt order = b.empid a.dt, a.dt;
With the above query, I get after release
DATE
EMPID
OFF
23/01/2015
1
1
23/01/2015
2
1
21/01/2015
3
1
22/01/2015
< NULL >
< NULL >
24/01/2015
< NULL >
< NULL >
25/01/2015
< NULL >
< NULL >
But I need the output as follows
DATE
EMPID
OFF
21/01/2015
1
0
22/01/2015
1
0
23/01/2015
1
1
24/01/2015
1
0
25/01/2015
1
0
21/01/2015
2
0
22/01/2015
2
0
23/01/2015
2
1
24/01/2015
2
0
25/01/2015
2
0
21/01/2015
3
1
22/01/2015
3
0
23/01/2015
3
0
24/01/2015
3
0
25/01/2015
3
0
I use oracle 10 g.
Help, please
According to the power requested that each record in the view offdays should be repeated as many times as the number of records in the view of payrolldates-, this could be done via the cross join and a simple CASE statement to get an output of the column offday.
Select a.dt
empid
case when a.dt = b.dt
then offday
0 otherwise
end offday
of payrolldates one
offdays b
order of b.empid, a.dt;
-
Hi all
Is there a possible way to cross the join with condition?
I want something like this:
Table A: custid, accu_id, sum
CustID, accu_id unique =
CustomerID amount accu_id
10 25 400
10 35 447
10 29 420
20 30 510
30 35 472
.
.
.
Table b: accu_id, description
accu_id description
shoes 25
Book 29
30 computer
pen 35
Note: in table A, it is a mismatch of the accu_id values that do not use. Please, take into account this.
I want to see all the columns in the TABLE B for each custid in A. TABLE (something like cross join but with ONE article.) The following query does not work.
Select * from a right join B on A.accu_id = B.accu_id;
10-25
10-29
10-30
10-35
20-25
20-29
20-30
20-35
30 25
30 29
30 30
30-35
What should I do for this?
Thanks in advance
Use partition outer join:
with a (too)
Select double union all 10 custid, accu_id 25, amount 400
Select 10,35,447 from all the double union
Select 10,29,420 from all the double union
Select 20,30,510 from all the double union
Select double 30,35,472
),
b like)
Select accu_id 25, "shoe" description of all the double union
Select 29, 'book' from dual union all
Select 30, 'computer' from dual union all
Select 35, 'pen' from dual
)
Select a.custid,
a.accu_id,
a.amount,
b.
a
by (a.custid) partition
right join
b
On a.accu_id = b.accu_id
order of a.custid,
a.accu_id
/
AMOUNT ACCU_ID CUSTID DESCRIPT
---------- ---------- ---------- --------
10 25 400 shoe
10 29 420 book
10 35 447 pen
10 computer
20 30 510 computer
shoe 20
book 20
20 pen
30 35 472 pen
30 shoe
30 bookAMOUNT ACCU_ID CUSTID DESCRIPT
---------- ---------- ---------- --------
30 computer12 selected lines.
SQL >
SY.
-
Join dba_segments to dba_indexes
Oracle 11.2.0.2.0
Oracle 5.6 Linux 64 bit
The following query is based on one I've seen on AskTom. The first is to generate the sql code to move the tables to one tablespace to another. I am trying to add logic to
(1) to generate code for a single table, as specified by the user
(2) also generate code to rebuild all the indexes on the selected table.
Here's what I have so far. Note that all "tables" are seen in the standard dictionary.
Select decode (segment_type, 'TABLE',
nom_segment, table_name) order_col1,.
Decode (segment_type, 'TABLE', 1, 2) order_col2.
'Edit ' | segment_type. ' ' || nom_segment |
Decode (segment_type, 'TABLE', 'move', 'rebuild') |
Decode (segment_type, 'TABLE', "tablespace stageb",
' (rebuild tablspace stage_idx') |
';'
from dba_segments,.
(select table_name, index_name dba_indexes)
where segment_type in ('TABLE', 'INDEX')
and nom_tablespace in ('STAGE', 'STAGE_IDX')
and (segment_type = 'TABLE' and nom_segment = upper ('& tblnme'))
and nom_segment = index_name
order by 1, 2
I tried various permutations, or return the code for all indexes regardless of their association of table, or no index at all. Having trouble getting my head wrapped around it.
Like this? I've highlighted in RED user input values.
SQL > with my_segment
2 as
(3)
4. Select the owner 'STAGE', 'TABLE', 'My_table' double nom_segment segment_type
5 Union all the
6. Select the owner 'STAGE', 'TABLE', 'FUBAR' double nom_segment segment_type
7 union of all the
8. Select owner 'STAGE', 'INDEX', 'UK_ATTR_GROUP' nom_segment double segment_type
9)
10, my_index
11 as
(12)
13. Select owner 'STAGE' index_name 'UK_ATTR_GROUP', 'STAGE' table_owner, table_name "My_table", nom_tablespace "STAGE".
14 double
all 15 union
16. Select index_name 'PK_MY_TABLE', 'STAGE' table_owner, table_name 'My_table', owner 'STAGE', 'STAGE_IDX' nom_tablespace
17 of the double
18)
19 select coalesce (table_script, index_script) script
20 of)
21 select case when rno = 1 then
22 'alter table ' | owner | '.' || nom_segment | "move the tablespace STAGE_B;
end 23 table_script
24, "alter index". owner | '.' || index_name | 'rebuild TABLESPACE STAGE_IDX;' index_script
25 years of)
26 select s.owner
27, s.segment_name
28, i.index_name
29, row_number() over NWR (partition by order of s.segment_name of i.index_name)
30 , ind
s my_segment 31
32 left
Join 33 my_index I
34 on s.owner = i.table_owner
35 and s.segment_name = i.table_name
36 cross join (select level double ind connect by level<=>=>
37 where s.segment_type = 'TABLE '.
38 and s.segment_name = 'my_table '.
39 )
where the 40 (rno = 1 and ind (1, 2))
41 or (rno > 1 and ind in (1))
42 );SCRIPT
-------------------------------------------------------------
ALTER table STEP. My_table move tablespace STAGE_B;
change the index STEP. PK_MY_TABLE rebuild TABLESPACE STAGE_IDX;
change the STAGE.UK_ATTR_GROUP TABLESPACE STAGE_IDX index rebuild;SQL >
-
left outer join, after summarizing
Hello
I need to create a summary of a lot of tables and join the result set with another table. The other table must do a left outer join. When I do a left outer join, the column which do not appear in the second table appear as a draft. However, I would like to than all results in the table on the right to replicate once for each row in the left table. Here is a sample data set:
CREATE THE TRIGHT TABLE
(
PKEY CHAR(1),
AMT NUMERIC (13.2).
GRP TANK (4),
FILTER TANK (3)
)
INSERT INTO TRIGHT VALUES ('A', 200 ' GRP1 "," XXX ");
INSERT INTO TRIGHT VALUES ('A', 500, "GRP2', 'XXX'");
INSERT INTO TRIGHT VALUES ('A', 150 'GRP1', 'YYY');
INSERT INTO TRIGHT VALUES ('A', 100, "GRP2', 'YYY'");
CREATE THE TLEFT TABLE
(
PKEY CHAR (1)
)
INSERT INTO VALUES TLEFT ('A')
INSERT INTO TLEFT VALUES ('B')
SELECT A.PKEY, GRP, SUM (AMT) OF TLEFT A LEFT JOIN
(SELECT PKEY, GRP, SUM (AMT) AS AMT IN TRIGHT)
WHERE FILTER = "XXX".
PKEY, GRP) B
ON A.PKEY = B.PKEY
A.PKEY GROUP, GRP
The result I get is:
A 200 GRP1
A 500 GRP2
B (white) (white)
Instead, I would like the following:
A 200 GRP1
A 500 GRP2
GRP1 B 0
B GRP2 0
Can anyone help? Thanks in advance.
Sorry, I couldn't find the button skip yet, although there is an urgent need for such a feature in this forum.
That is why another chance for you:
SELECT
a.PKEY
GRP
SUM (case
When a.pkey = b.pkey
can AMT 0 otherwise
AMT end)
Of
TLEFT HAS
Cross JOIN
TRIGHT B
where FILTER = "XXX".
GROUP BY a.PKEY, GRP
ORDER BY a.PKEY, GRP
PKEY GRP AMT A GRP1 200 A GRP2 500 B GRP1 0 B GRP2 0 -
outer join when there are several tables are involved
Could not put up the question correctly to the last channel, my problem is with the join when there are several tables are involved, this is just one example of the task that I have to carry.
Tab1 aura model id retailer_id information for all the weeks (from the first Monday) of the month of JUNE with cost and Helen
Tab1
model_id
retailer_id
sell_date
cost
Helene
1
12
June 3, 13
100
40
1
12
June 10, 13
200
20
1
12
17 June 13
300
20
1
12
24 June 13
400
20
2
12
June 3, 13
300
10
2
12
June 10, 13
200
20
2
12
17 June 13
300
20
2
12
24 June 13
400
20
Tab2:
each retailer belongs to a dealer, under the table has the same information
retailer_id
Dealer_id
12
100
13
100
14
101
15
101
16
101
Tab 3
There is a third layer where each dealership is having a garage band
Dealer_id
Dealer_group
100
1001
101
1001
102
2001
103
2001
104
3001
105
3001
Tab4:
Of this table for each model and dealer discount information for the month of June (every week)
model_id
Dealer_group
discount_date
discount
1
1001
June 3, 13
10
1
1001
June 10, 13
20
1
1001
17 June 13
10
1
1001
24 June 13
30
2
1001
June 3, 13
10
2
1001
June 10, 13
20
2
1001
17 June 13
10
2
1001
24 June 13
30
3
2001
June 3, 13
10
3
2001
June 10, 13
20
3
2001
17 June 13
10
3
2001
24 June 13
30
Master_info:
It's the main table which is the master table for model /retailer information
Model_id
retailer_id
1
12
2
12
3
12
4
12
1
13
2
13
Output
model_id
retailer_id
sell_date
cost
Helene
Final (cost-helene-discount)
1
12
June 3, 13
100
40
50
1
12
June 10, 13
200
20
160
1
12
17 June 13
300
20
270
1
12
24 June 13
400
20
350
2
12
June 3, 13
300
10
280
2
12
June 10, 13
200
20
160
2
12
17 June 13
300
20
270
2
12
24 June 13
400
20
350
3
12
June 3, 13
0
0
0
3
12
June 10, 13
0
0
0
3
12
17 June 13
0
0
0
3
12
24 June 13
0
0
0
4
12
June 3, 13
0
0
0
4
12
June 10, 13
0
0
0
4
12
17 June 13
0
0
0
4
12
24 June 13
0
0
0
1
13
June 3, 13
0
0
0
1
13
June 10, 13
0
0
0
1
13
17 June 13
0
0
0
1
13
24 June 13
0
0
0
2
13
June 3, 13
0
0
0
2
13
June 10, 13
0
0
0
2
13
17 June 13
0
0
0
1
13
24 June 13
0
0
0
For highted above records (model_id / retailer_id combination) there is no record in tab1 but they have entered in master_info then the recordings should come for all model_id/retailer_id with all the 0 values
Hello
Thanks for posting the sample data.
It is unclear what dates you want to include in the output. The following query shows how you can generate every Monday in a given range. If you only want to include the dates that are actually present in tabl1 and/or tab4, you can simplify this a bit.
WITH date_range AS
(
SELECT TRUNC (TO_DATE (' 3 June 2013', 'DD-Mon-YYYY'))
, 'IW '.
) AS first_monday
, TRUNC (TO_DATE (24 June 2013 ', 'DD-Mon-YYYY') + 6)
, 'IW '.
) AS last_monday
OF the double
)
all_mondays AS
(
First_monday SELECT + (7 * (LEVEL - 1)) AS sell_date
OF date_range
CONNECT BY LEVEL<= 1="" +="" (="" (last_monday="" -="">=>
/ 7
)
)
SELECT mi.model_id
mi.retailer_id
am.sell_date
, Cost of NVL (t1.cost, 0) AS
, NVL (t1.rebat, 0) IN the refund
, NVL (t1.cost, 0)
-(NVL (t1.rebat, 0))
+ NVL (t4.discount, 0)
) AS final
E master_info
CROSS JOIN all_mondays am
LEFT OUTER JOIN tab1 t1 ON t1.model_id = mi.model_id
AND t1.retailer_id = mi.retailer_id
AND t1.sell_date = am.sell_date
LEFT OUTER JOIN tab2 t2 ON t2.retailer_id = mi.retailer_id
LEFT OUTER JOIN tab 3 t3 ON t3.dealer_id = t2.dealer_id
LEFT OUTER JOIN tab4 t4 ON t4.model_id = t1.model_id
AND t4.dealer_group = t3.dealer_group
AND t4.discount_date = t1.sell_date
ORDER BY mi.retailer_id
mi.model_id
am.sell_date
;
The results are not exactly what said you you wanted. I suspect it's because of typos in that you posted.
Maybe you are looking for
-
IPhoto breaking when running a slide show
Hello My iPhoto 11 9.4.3 crashes whenever I try to run a slide show. I have a 2012 end Macbook Pro osx 10.8.5 with all updates of operation. I tried a lot of things. I rebuilt and repaired each option with the option/command start of iPhoto on librar
-
When I start Firefox, I always "Welcome to Firefos" tab. I tried to change the options for mu, but when I close Firefox, the options return to the origional options before them, I changed.
-
How to disable the touchpad in windows 8 - Pavilion g6
How to disable the touchpad in windows 8 - Pavilion g6
-
The problem that I have are cuando voy a print any document, the bar of PROPIEDADES esta inactiva y no puedo senalar o so what I need. Is he the Patrick desinstalado y the nuevo pero instaled nor is ha arreglado el problema. If alguien me you can ind
-
Camera memory card will not show a menu
Hello, my computer is now not letting me store or even to open images from my camera or my daughters. We have all two nikon cameras. I used to get a menu asking me what I would do and what I wanted to use programs. I assume that it it the menu box. N