是否可以向查询添加一列不同/唯一值?

Posted

技术标签:

【中文标题】是否可以向查询添加一列不同/唯一值?【英文标题】:Is it possible to add a column of distinct/unique values to a query? 【发布时间】:2021-01-08 11:16:01 【问题描述】:

我目前使用的是 Oracle SQL Developer 19.2。

一位客户请求了一份报告,该报告显示特定客户的销售价值和利润,其中有一列将每位客户购买的每件商品显示为唯一价值。唯一/不同的列不应改变初始查询。

这是我目前所拥有的:

select  
    cus_code as "Customer Account No.",
    cus_name as "Customer Name",
    sum(cmoh_value) as "Sales Value",
    sum(cmoh_cost_value) as "Cost Value",
    sum(cmoh_value-cmoh_cost_value) as "G.P. Value",
    round(sum(cmoh_value-cmoh_cost_value) / sum(cmoh_value)*100,2) as "G.P. %"
from completed_order_header
inner join completed_order_line on cmoh_company_number=cmol_company_number and cmoh_branch_number=cmol_branch_number and cmoh_order_number=cmol_order_number
inner join customer_master on cmoh_company_number=cus_company and cmoh_customer=cus_code
where cmoh_company_number = 1
  and cmoh_branch_number = 1
  and cmoh_status <> 3
  and cmoh_desp_date between '01-JAN-20' and '31-DEC-20'
  and cmoh_value <> 0
group by cus_code, cus_name
order by cus_code, cus_name
;

所以现在我想在销售项目的末尾添加一列:

select distinct
    cus_code,
    cus_name,
    cmol_item_code
from completed_order_header
inner join completed_order_line on cmoh_company_number=cmol_company_number and cmoh_branch_number=cmol_branch_number and cmoh_order_number=cmol_order_number
inner join customer_master on cmoh_company_number=cus_company and cmoh_customer=cus_code
where cmoh_company_number = 1
  and cmoh_branch_number = 1
  and cmoh_status <> 3
  and cmoh_desp_date between '01-JAN-20' and '31-DEC-20'
  and cmoh_value <> 0
  and cmol_item_code not in ('TEXT')
order by cus_code, cus_name
;

认为此脚本将返回每个客户已购买的唯一商品,我希望将该商品列表作为原始查询中的一列。我可以单独运行每个查询并使用数据来创建客户要求的报告,但由于我想提高我的 SQL 技能,我很好奇如何将其作为单个脚本完成。

编辑:

按照建议使用 LISTAGG 返回错误,使用脚本:

select  
    cus_code as "Customer Account No.",
    cus_name as "Customer Name",
    sum(cmoh_value) as "Sales Value",
    sum(cmoh_cost_value) as "Cost Value",
    sum(cmoh_value-cmoh_cost_value) as "G.P. Value",
    round(sum(cmoh_value-cmoh_cost_value) / sum(cmoh_value)*100,2) as "G.P. %",
    listagg(cmol_item_code, ',') within group (order by cus_code, cus_name) as Items
from completed_order_header
inner join completed_order_line on cmoh_company_number=cmol_company_number and cmoh_branch_number=cmol_branch_number and cmoh_order_number=cmol_order_number
inner join customer_master on cmoh_company_number=cus_company and cmoh_customer=cus_code
where cmoh_company_number = 1
  and cmoh_branch_number = 1
  and cmoh_status <> 3
  and cmoh_desp_date between '01-JAN-20' and '31-DEC-20'
  and cmoh_value <> 0
group by cus_code, cus_name
order by cus_code, cus_name
;

收到错误消息:

ORA-00923: FROM keyword not found where expected
00923. 00000 -  "FROM keyword not found where expected"
*Cause:    
*Action:
Error at Line: 10 Column: 45

【问题讨论】:

你的第一个查询不是有效的Oracle,所以这个问题让我很困惑。您需要修复第一个查询吗?你有错误的数据库标签吗?您还有其他想要包含的信息吗? 有一个错字现已更正。 【参考方案1】:

你在找listagg吗?

listagg(so_sales_item, ',') within group (order by so_sales_item) as so_sales_item

【讨论】:

我在使用 listagg 时收到错误“ORA-00923: FROM keyword not found where expected”。 请分享您使用过的查询 抱歉耽搁了,我已经更新了帖子,如果您有任何想法,我已经尝试使用 LISTAGG

以上是关于是否可以向查询添加一列不同/唯一值?的主要内容,如果未能解决你的问题,请参考以下文章

DBVisualizer 查询语法以添加一系列值

向 DataFrame 添加一列,其值为 1,其中预测大于自定义阈值

如何从不同的数据框中添加一列:Scala Frame

向熊猫数据框添加一列

在 Python 中使用 ThreadPoolExecutor 获取数据期间添加一列

Flyway-在名称中包含指定后缀的所有表中添加一列