簡體   English   中英

使用這種SQL樣式有什么好處?

[英]What are the benefits of using this SQL style?

我有以下SQL查詢,它的結構對我來說很奇怪:

SELECT 
  SUM("x1") AS "X1 col",
  SUM("x2") AS "X2 col"
FROM (
  SELECT DISTINCT xyz
  FROM some_tables
  WHERE some_cond
 );

實際的代碼在這里:

select 
  sum("Req Lines") as "Requisitions Created",
  sum("Approved Req") as "Requisitions Approved",
  sum("Non Approved PO") as "PO Started",
  sum("Approved PO") as "PO Approved",
  sum("w/ Receipt") as "Receipts Completed",
  sum("w/ Invoice") as "Invoices Completed"
from(
  select distinct prha.Segment1 as "Req#", 
    pha.Segment1 as "PO#",
    CASE when prha.creation_date between to_date(:myDate,'DD-MON-YY') and to_date(:myDate,'DD-MON-YY') + 7  then 'Week 1' 
        when prha.creation_date between to_date(:myDate,'DD-MON-YY') + 7 and to_date(:myDate,'DD-MON-YY') + 14  then 'Week 2' 
        when prha.creation_date between to_date(:myDate,'DD-MON-YY') + 14 and to_date(:myDate,'DD-MON-YY') + 21 then 'Week 3'
        when prha.creation_date between to_date(:myDate,'DD-MON-YY') + 21 and to_date(:myDate,'DD-MON-YY') + 28  then 'Week 4' 
     else 'After Month 1' END as "Req_Created",
    --CASE when prha.creation_date < '28-Aug-2015' then 'Week 1' else 'After Week 1' END,
    1  as "Req Lines",
    prha.authorization_status as "Req Status",
    CASE when prha.authorization_status='APPROVED' then 1 else 0 END as "Approved Req",
    CASE when pha.authorization_status='APPROVED' then 1 else 0 END as "Approved PO",
    pha.authorization_status,
    CASE when (pha.authorization_status = 'APPROVED' or pha.authorization_status is null) then 0 else 1 END as "Non Approved PO",
    CASE when pda.quantity_delivered >=1 then 1 else 0 END as "w/ Receipt",
    CASE when pda.quantity_billed >=1 then 1 else 0 END as "w/ Invoice",
    prha.*, 
    prl.*
  from po_requisition_lines_all prl, 
    po_requisition_headers_all prha, 
    po_req_distributions_all prda, 
    po_distributions_all pda, 
    po_headers_all pha
  WHERE 1=1 
   AND prl.requisition_header_id = prha.requisition_header_id
  and prl.requisition_line_id = prda.requisition_line_id
  AND prda.distribution_id= pda.req_distribution_id(+)
  and pha.po_header_id(+) = pda.po_header_id
  --and item_description =  'Brads Test'
  and prda.creation_date > to_date(:myDate,'DD-MON-YY')
  and prha.org_id in (279,282,351,105,102) -- NA operating Units
)
;

我對這種SQL的工作方式很好奇,為什么還要使用這種樣式的SQL代碼?

您所顯示的只是從子查詢中選擇聚合。

您可以查詢子查詢的結果,而這正是您的示例所做的。 根據您的顯示,以這種方式編寫查詢不是100%必要的,但是以這種方式編寫查詢也沒有錯。 我可以想到可以得到相同結果的其他方法,但是在涉及SQL方面,總有不止一種方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM