简体   繁体   English

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

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

I'm currently using Oracle SQL Developer 19.2.我目前正在使用 Oracle SQL Developer 19.2。

A client has requested a report that shows the sales value and profit for specific customers with a column that shows every item that each customer has purchased as a unique value.客户请求了一份报告,该报告显示特定客户的销售价值和利润,其中一列将每个客户购买的每件商品显示为唯一价值。 The unique/distinct column should not alter the initial query.唯一/不同的列不应改变初始查询。

This is what I have so far:这是我到目前为止所拥有的:

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
;

So now I want to add a column at the end for the sales item:所以现在我想在销售项目的末尾添加一列:

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
;

I think this script will return the unique items that have been purchased by each customer and I'd like that list of items as a column in the original query.认为此脚本将返回每个客户已购买的唯一商品,并且我希望将该商品列表作为原始查询中的一列。 I can just run each query individually and use the data to create the report the client is asking for, but as I'd like to improve my SQL skills, I'm curious about how this can be done as a single script.我可以单独运行每个查询并使用数据来创建客户要求的报告,但由于我想提高我的 SQL 技能,我很好奇如何将其作为单个脚本完成。

EDIT:编辑:

Using LISTAGG as suggested returns error, script used:按照建议使用 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
;

Error Message Received:收到错误消息:

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

are you looking for listagg ?您在寻找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