首页 > 解决方案 > 客户有多个账户

问题描述

在此处输入图像描述我们有一个包含客户帐户的表。其中一位客户恰好有多个帐户。

客户表:( 格式:customer_ID Name)

账户表 (格式:Account_ID、customer_ID、Account_Type)

如果一个客户有多个账户,我们根据这个顺序选择account_type:A1,C1,B1

结果应该是:

我写了以下查询: 选择 c.customer_ID, case when Account_Type in ('A1','B1' 'C1' ) then A1 when Account_Type in (''B1' 'C1' ) then C1 else Account_Type End From customer c join a.customer_ID=c.customer_ID 上的帐户 a

我该如何提出条件:一个客户在此查询中有多个帐户?

谢谢

标签: if-statementhiveconditionalcase

解决方案


简单的解决方案是

SELECT * FROM (SELECT *, CASE AccountType WHEN 'A1' THEN 1 WHEN 'B1' THEN 3 WHEN 'C1' THEN 2 END As Rank1 FROM testAccount ) 按 CustomerID 排列的订单,Rank1


推荐阅读