如何连接两个表并获取数据
How to join two tables and get the data
我有两个 table,如下所示。
Table #1: 客户群 table:
No Reg Name
----------------------------------------
111 Account Owner Josh
111 Customer Group Josh Group
Table#2:客户table:
No Name Address
----------------------------------
111 Josh Ltd Lala Land
我如何编写查询 returns 如下所示的数据:
No Customer Account_Owner Customer_Group
--------------------------------------------------
111 Josh ltd Josh Josh Group
请协助
使用条件聚合:
select c.no, c.name as customer,
max(case when cg.reg = 'Account Owner' then name end) as account_owner,
max(case when cg.reg = 'Customer Group' then name end) as customer_group
from customer c
inner join customer_group cg on cg.no = c.no
group by c.no, c.name
我有两个 table,如下所示。
Table #1: 客户群 table:
No Reg Name
----------------------------------------
111 Account Owner Josh
111 Customer Group Josh Group
Table#2:客户table:
No Name Address
----------------------------------
111 Josh Ltd Lala Land
我如何编写查询 returns 如下所示的数据:
No Customer Account_Owner Customer_Group
--------------------------------------------------
111 Josh ltd Josh Josh Group
请协助
使用条件聚合:
select c.no, c.name as customer,
max(case when cg.reg = 'Account Owner' then name end) as account_owner,
max(case when cg.reg = 'Customer Group' then name end) as customer_group
from customer c
inner join customer_group cg on cg.no = c.no
group by c.no, c.name