create table organizations (
org_name varchar2(20),
org_status varchar2(10),
product_id integer
)
Table created.
create table products (
product_id integer,
product_name varchar2(10)
)
Table created.
insert into organizations values( 'Org 1', 'Active', 1 )
1 row(s) inserted.
insert into organizations values( 'Org 2', 'Legacy', 1 )
1 row(s) inserted.
insert into organizations values( 'Org 3', 'Legacy', 2 )
1 row(s) inserted.
insert into organizations values( 'Org 4', 'Active', 3 )
1 row(s) inserted.
insert into products values( 1, 'Product 1' )
1 row(s) inserted.
insert into products values( 2, 'Product 2' )
1 row(s) inserted.
insert into products values( 3, 'Product 3' )
1 row(s) inserted.
select o.org_id, o.org_status, p.product_id, p.product_name,
row_number() over (partition by p.product_id
order by case when o.org_status = 'Active'
then 1
else 2
end desc) rn
from products p
join organizations o
on p.product_id = o.product_id
ORA-00904: "O"."ORG_ID": invalid identifierMore Details: https://docs.oracle.com/error-help/db/ora-00904
drop table organizations
Table dropped.
drop table products
Table dropped.
create table organizations (
org_name varchar2(20),
org_status varchar2(10),
product_id integer
)
Table created.
create table products (
product_id integer,
product_name varchar2(10)
)
Table created.
insert into organizations values( 'Org 1', 'Active', 1 )
1 row(s) inserted.
insert into organizations values( 'Org 2', 'Legacy', 1 )
1 row(s) inserted.
insert into organizations values( 'Org 3', 'Legacy', 2 )
1 row(s) inserted.
insert into organizations values( 'Org 4', 'Active', 3 )
1 row(s) inserted.
insert into products values( 1, 'Product 1' )
1 row(s) inserted.
insert into products values( 2, 'Product 2' )
1 row(s) inserted.
insert into products values( 3, 'Product 3' )
1 row(s) inserted.
select o.org_name, o.org_status, p.product_id, p.product_name,
row_number() over (partition by p.product_id
order by case when o.org_status = 'Active'
then 1
else 2
end desc) rn
from products p
join organizations o
on p.product_id = o.product_id
ORG_NAME | ORG_STATUS | PRODUCT_ID | PRODUCT_NAME | RN | Org 2 | Legacy | 1 | Product 1 | 1 | Org 1 | Active | 1 | Product 1 | 2 | Org 3 | Legacy | 2 | Product 2 | 1 | Org 4 | Active | 3 | Product 3 | 1 |
---|
drop table organizations
Table dropped.
drop table products
Table dropped.
create table organizations (
org_name varchar2(20),
org_status varchar2(10),
product_id integer
)
Table created.
create table products (
product_id integer,
product_name varchar2(10)
)
Table created.
insert into organizations values( 'Org 1', 'Active', 1 )
1 row(s) inserted.
insert into organizations values( 'Org 2', 'Legacy', 1 )
1 row(s) inserted.
insert into organizations values( 'Org 3', 'Legacy', 2 )
1 row(s) inserted.
insert into organizations values( 'Org 4', 'Active', 3 )
1 row(s) inserted.
insert into products values( 1, 'Product 1' )
1 row(s) inserted.
insert into products values( 2, 'Product 2' )
1 row(s) inserted.
insert into products values( 3, 'Product 3' )
1 row(s) inserted.
select o.org_name, o.org_status, p.product_id, p.product_name,
row_number() over (partition by p.product_id
order by case when o.org_status = 'Active'
then 1
else 2
end asc) rn
from products p
join organizations o
on p.product_id = o.product_id
ORG_NAME | ORG_STATUS | PRODUCT_ID | PRODUCT_NAME | RN | Org 1 | Active | 1 | Product 1 | 1 | Org 2 | Legacy | 1 | Product 1 | 2 | Org 3 | Legacy | 2 | Product 2 | 1 | Org 4 | Active | 3 | Product 3 | 1 |
---|