【问题标题】:sqlplus adding column headingsqlplus 添加列标题
【发布时间】:2019-02-22 00:37:21
【问题描述】:

我正在使用 oracle 的 sqlplus,并试图找出创建客户列表并根据以下标准对其进行分类的方法:

  • 如果帐户数为 1,则将其归类为“入门级”
  • 如果帐户数量为 2-3,则将它们归类为“增长中”
  • 如果帐户数量超过 4 个,则将其归类为“成熟”

然后创建一个预期的输出(我已将其作为图像附加)。

我还为此附上了 ERD。

这是我目前所拥有的。

SELECT DISTINCT first_name "First",
                surname "Last",
                customer_number "Cust #",
                account_type "# of Accts"
  FROM (  SELECT first_name,
                 surname,
                 customer_number,
                 account_type
            FROM wgb_customer
                 JOIN wgb_account USING (customer_number)
                 JOIN wgb_account_type USING (account_type)
        ORDER BY account_type);

请帮忙! 这是预期的输出!

First       Last        Cust#         # of Accts     Level
----------------------------------------------------------------        
Peter      Chen         2566217             3         Growing           
Byron      Griffith     1113004             1         Entry Level
Patricia   Lee          9871332             1         Entry Level
Henri      Poincare     1113501             3         Growing   
John       Synge        1112401             2         Growing   

【问题讨论】:

  • 请以表格文本的形式将示例数据和预期输出添加到您的问题中。事实上,您的查询没有多大意义。
  • 对,但表格文本对我们大多数人来说更具可读性。另外,请您也添加示例数据吗?
  • 对不起,我不知道如何做表格文本,所以我只是尽我所能将预期的输出作为文本大声笑
  • 请在您的问题中添加示例数据和预期输出。有关发布表格数据的帮助,请参阅 this Meta StackExchange post
  • 感谢您的预期输出。但是我们仍然缺少示例数据,您能提供吗?

标签: sql oracle group-by


【解决方案1】:

这听起来像是带有case 表达式的聚合:

select c.first_name, c.surname, c.customer_number,
       count(*) as num_accounts,
       (case when count(*) = 1 then 'Entry Level'
             when count(*) <= 3 then 'Growing'
             else 'Mature'
        end) as level
from wgb_customer c join
     wgb_account a
     using (customer_number) 
group by c.first_name, c.surname, c.customer_number;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-05
    • 2017-12-19
    • 2014-05-26
    相关资源
    最近更新 更多