I have a table like as this, it means the number of products owned by each company
productType company1 company2 company3
---------- -------- -------- --------
a 4 1 3
b 5 2 23
c 1 3 4
d 2 4 5
How can I get a table like following:
producType companyName amount
---------- ----------- ------
a company1 4
b company1 5
c company1 1
d company1 2
a company2 1
b company2 2
c company2 3
d company2 4
a company3 3
b company3 23
c company3 4
d company3 5
Please use function unpivot to solve your problem.
t = table(`a`b`c`d as productType, 4 5 1 2 as company1, 1 2 3 4 as company2, 3 23 4 5 as company3)
unpivot(t, `productType, `company1`company2`company3)
Related
Oracle Database 12c Enterprise Edition Release 12.1.0.2.0
I expect I'm just missing something, but if I run this query without the "connect by", I get 2 rows. When I add "connect by level <= 4", I would expect to get each of those 2 rows 4 times. The actual result is different.
Can anyone help me understand what's happening here? I'm not looking for a solution that only repeats each row 4 times - I've already got that. I'm just looking to understand what's happening and why.
with alpha as (
select 1 as id
from dual
),
beta as (
select 1 as alpha_id,
1 as beta_no
from dual
union all
select 1 as alpha_id,
2 as beta_no
from dual
)
select a.id,
b.beta_no,
level as the_level
from alpha a
inner join beta b
on b.alpha_id = a.id
connect by level <= 4
order by a.id,
b.beta_no,
level
;
ID BETA_NO THE_LEVEL
1 1 1
1 1 2
1 1 2
1 1 3
1 1 3
1 1 3
1 1 3
1 1 4
1 1 4
1 1 4
1 1 4
1 1 4
1 1 4
1 1 4
1 1 4
1 2 1
1 2 2
1 2 2
1 2 3
1 2 3
1 2 3
1 2 3
1 2 4
1 2 4
1 2 4
1 2 4
1 2 4
1 2 4
1 2 4
1 2 4
30 rows selected
Many thanks to mathguy. The second link he provided in the answer below had exactly what I was looking for. Specifically:
1 with t as (select 1 as id from dual union all
2 select 2 from dual)
3 --
4 select id, level
5 ,prior id
6 ,sys_connect_by_path(id,'=>') as cpath
7 from t
8* connect by level <= 3
SQL> /
ID LEVEL PRIORID CPATH
---------- ---------- ---------- --------------------------------------------------
1 1 =>1
1 2 1 =>1=>1
1 3 1 =>1=>1=>1
2 3 1 =>1=>1=>2
2 2 1 =>1=>2
1 3 2 =>1=>2=>1
2 3 2 =>1=>2=>2
2 1 =>2
1 2 2 =>2=>1
1 3 1 =>2=>1=>1
2 3 1 =>2=>1=>2
2 2 2 =>2=>2
1 3 2 =>2=>2=>1
2 3 2 =>2=>2=>2
14 rows selected.
It's clear to me from that example, but I'd be hard-pressed to succinctly put it into words.
With no condition other than "level <= 4", every row from the original table, view etc. (from the join, in this case) will produce two rows at level 2, then four more rows at level 3, and 8 more at level 4. "Connect by" is essentially a succession of joins, and you are doing cross joins if you have no condition with the PRIOR operator.
You probably want to add "and prior a.id = a.id". This will lead to Oracle complaining about cycles (because Oracle decides a cycle is reached when it sees the same values in the columns subject to PRIOR). That, in turn, is solved by adding a third condition, usually "and prior sys_guid() is not null".
(Edited; the original answer made reference to NOCYCLE, which is not needed when using the "prior sys_guid() is not null" approach.)
This has been discussed recently on OTN: https://community.oracle.com/thread/3999985
Same question discussed here: https://community.oracle.com/thread/2526535
To illustrate Mathguy's answer, you are missing some predicates out of your CONNECT BY clause:
with alpha as (
select 1 as id
from dual
),
beta as (
select 1 as alpha_id,
1 as beta_no
from dual
union all
select 1 as alpha_id,
2 as beta_no
from dual
)
select a.id,
b.beta_no,
level as the_level
from alpha a
inner join beta b
on b.alpha_id = a.id
connect by level <= 4
AND PRIOR a.id = a.id
AND PRIOR b.beta_no = b.beta_no
AND PRIOR sys_guid() IS NOT NULL
order by a.id,
b.beta_no,
LEVEL;
ID BETA_NO THE_LEVEL
---------- ---------- ----------
1 1 1
1 1 2
1 1 3
1 1 4
1 2 1
1 2 2
1 2 3
1 2 4
An alternative would be to use the recursive with clause:
with alpha as (
select 1 as id
from dual
),
beta as (
select 1 as alpha_id,
1 as beta_no
from dual
union all
select 1 as alpha_id,
2 as beta_no
from dual
),
multiply (id, beta_no, rn) AS (SELECT a.id,
b.beta_no,
1 rn
FROM alpha a
INNER JOIN beta b
ON a.id = b.alpha_id
UNION ALL
SELECT ID,
beta_no,
rn + 1
FROM multiply
WHERE rn + 1 <= 4)
SELECT ID,
beta_no,
rn AS the_level
FROM multiply
order by id,
beta_no,
rn;
ID BETA_NO THE_LEVEL
---------- ---------- ----------
1 1 1
1 1 2
1 1 3
1 1 4
1 2 1
1 2 2
1 2 3
1 2 4
I have Oracle g11 XE in Ubuntu 14.04, i will to show a table Without cutting , for example :
C_ID A_ID C_PRODUIT
---------- ---------- --------------------
1 1 real madrid
2 1 manunited
3 2 barca
4 3 barca
5 2 real madrid
6 5 barca
7 5 real madrid
8 10 juvantus
10 8 barca
11 1 chelsea
9 6
12 4
I do not want to appear so :
i will to delete the " ------------------------------ "
C_ID A_ID C_PRODUIT
---------- ---------- --------------------
1 1 real madrid
2 1 manunited
3 2 barca
4 3 barca
5 2 real madrid
6 5 barca
7 5 real madrid
8 10 juvantus
10 8 barca
11 1 chelsea
9 6
C_ID A_ID C_PRODUIT
---------- ---------- --------------------
12 4
IN SQL Plus:
set pagesize 1000;
Your cut will be after 1000 rows.
You can add this line in your glogin file, which is located under $ORACLE_HOME/sqlplus/admin. That way this line will be executed on every new SQL Plus session.
ORACLE select
calculate Sales، returns and the rest for a customer in the same table for the same product according to trans type
i need to calculate total sales and total returns and the rest for the customer and items.
and group by customer
Trans_Type:
1= Sales
2= Return
ID Trans_Type DATE Items_ID Quantity Clint_ID
--- ---------- -------- ---------- ---------- ----------
1 1 16-OCT-09 701555 3 1
2 2 12-DEC-09 701555 1 1
3 1 30-JUL-10 701511 63 2
4 2 30-JUL-10 701555 1 1
5 1 30-JUL-10 701234 2 3
6 1 30-JUL-10 701234 5 3
7 2 30-JUL-10 701511 1 2
8 1 30-JUL-10 701522 3 2
9 1 30-JUL-10 701555 2 3
10 1 30-JUL-10 701555 4 2
11 2 30-JUL-10 701555 2 2
If I understood everything correct you need to use case when ... and group by ... clauses, like here:
select clint_id, items_id, qty, ret, nvl(qty,0) - nvl(ret,0) rest
from (
select clint_id, items_id,
sum(case when trans_type = 1 then quantity end) qty,
sum(case when trans_type = 2 then quantity end) ret
from data group by clint_id, items_id )
order by clint_id, items_id
SQLFiddle demo
I have below source table.
Col1 Col2 Col 3 Col4 col5 col6 col7 col8
A 2 1 2 3 4 5 AAA
B 3 1 1 8 5 6 AAA
C 4 1 2 9 6 7 CC
D 5 2 3 10 7 8 CC
E 2 2 4 11 8 9 CC
F 3 3 5 12 9 10 BB
G 4 3 6 13 10 11 BB
H 5 3 7 14 11 12 BB
I 6 3 8 15 12 13 BB
I want to create a single mapping ( 1 source and 1 target stucture) which should create three extract from above source as below. All differnt extract will have different number of columns based on saome speific id.
Extract 1
Col1 Col2 Col 3 Col4 col8
A 2 1 2 AAA
B 3 1 1 AAA
Extract 2
Col1 Col2 Col 3 Col4 col5 col6 col7 col8
C 4 1 2 9 6 7 CC
D 5 2 3 10 7 8 CC
E 2 2 4 11 8 9 CC
Extract 3
Col1 Col2 col7 col8
F 3 10 BB
G 4 11 BB
H 5 12 BB
I 6 13 BB
I dont want to create three differnt target structure.
Please let us know if any one have any idea on that .
You can split the flow to three pipelines with three Target Definitions that all will point to the same Target Table on session level. This will be clear and effectively all will end up in same target structure.
Otherwise, if for some reason you's like to avoid having three target instances, you can use the Union Transformation that will span the three pipelines and send all data to one target instance.
I need an o/p as below.
1,1
2,1
2,2
3,1
3,2
3,3
4,1
4,2
4,3
4,4
... and so on.
I tried to write the query as below. But throwing error. SIngle row subquery returns more than one row.
with test1 as(
SELECT LEVEL n
FROM DUAL
CONNECT BY LEVEL <59)
select n,(
SELECT LEVEL n
FROM DUAL
CONNECT BY LEVEL <n) from test1
Appreciate your help in solving the same.
Here is one of the methods how you could get the desired result:
SQL> with t1(col) as(
2 select level
3 from dual
4 connect by level <= 5
5 )
6 select a.col
7 , b.col
8 from t1 a
9 join t1 b
10 on a.col >= b.col
11 ;
COL COL
---------- ----------
1 1
2 1
2 2
3 1
3 2
3 3
4 1
4 2
4 3
4 4
5 1
5 2
5 3
5 4
5 5
15 rows selected