简体   繁体   中英

Oracle SQL equivalent of Index Match of Excel

I want to find out how to use ORACLE SQL codes to mimic what Index and Match can achieve in Excel.

Table 1:

cid  Cname  Age  Height 
001  Mary   E40  E22 
002  Cat    E22  E40

Table 2:

Fname   CODE     MEAING 
Age     E40      40 years old 
Age     E22      22 years old
Height  E22      5'2 
Height  E40     5'4

Goal:

Replace value of Age and Height with Meaning from table 2

Expected Result:

cid  Name  Age           Height
001  Mary  40 years old  5'2 
002  Cat   22 years old  5'4

Current way of doing it in Excel:

卓越的过程

How can I do the same thing in Oracle?

You could join from table_1 to table_2 twice, once for each data point type:

select t1.cid, t1.cname as name,
  t2_age.meaing as age, t2_height.meaing as height
from table_1 t1
join table_2 t2_age on t2_age.fname = 'Age' and t2_age.code = t1.age
join table_2 t2_height on t2_height.fname = 'Height' and t2_height.code = t1.height

With your sample data in CTEs:

with table_1 (cid, cname, age, height) as (
  select '001', 'Mary', 'E40', 'E22' from dual
  union all select '002', 'Cat', 'E22', 'E40' from dual
),
table_2 (fname, code, meaing) as (
  select 'Age', 'E40', '40 years old' from dual
  union all select 'Age', 'E22', '22 years old' from dual
  union all select 'Height', 'E22', '5''2' from dual
  union all select 'Height', 'E40', '5''4' from dual
)
select t1.cid, t1.cname as name,
  t2_age.meaing as age, t2_height.meaing as height
from table_1 t1
join table_2 t2_age on t2_age.fname = 'Age' and t2_age.code = t1.age
join table_2 t2_height on t2_height.fname = 'Height' and t2_height.code = t1.height;

CID NAME AGE          HEIGHT      
--- ---- ------------ ------------
001 Mary 40 years old 5'2         
002 Cat  22 years old 5'4         

or you could join once and use conditional aggregation to pick the relevant values:

select t1.cid, t1.cname as name,
  max(case when t2.fname = 'Age' then t2.meaing end) as age,
  max(case when t2.fname = 'Height' then t2.meaing end) as height
from table_1 t1
join table_2 t2 on (t2.fname = 'Age' and t2.code = t1.age)
                or (t2.fname = 'Height' and t2.code = t1.height)
group by t1.cid, t1.cname;

CID NAME AGE          HEIGHT      
--- ---- ------------ ------------
001 Mary 40 years old 5'2         
002 Cat  22 years old 5'4         

which is essentially a manual pivot:

select *
from (
  select t1.cid, t1.cname as name, t2.fname, t2.meaing
  from table_1 t1
  join table_2 t2 on (t2.fname = 'Age' and t2.code = t1.age)
                  or (t2.fname = 'Height' and t2.code = t1.height)
)
pivot (max(meaing) for (fname) in ('Age' as age, 'Height' as height));

CID NAME AGE          HEIGHT      
--- ---- ------------ ------------
001 Mary 40 years old 5'2         
002 Cat  22 years old 5'4         

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM