繁体   English   中英

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

[英]Is it possible to add a column of distinct/unique values to a query?

我目前正在使用 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

您在寻找listagg吗?

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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