簡體   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