在SQL中,如何创建将显示此类图表的查询?

时间:2015-08-18 20:08:07

标签: sql oracle oracle-sqldeveloper

我正在寻找有关如何执行以下操作的一般指示:

我的图表看起来像这样:

enter image description here

以下是我到目前为止的SQL,我使用的是WITH子句:

WITH WithSubquery1 AS ( 
      SELECT  AUTHORIZATION_STATUS AS "TOTAL AUTHO",  COUNT(authorization_status) AS "Requisition Lines Count",  LOCATION_CODE AS "Location",
            imcb.SEGMENT1 AS "CATEGORY"  

      FROM 
           apps.po_requisition_headers_all   prha  
                    JOIN apps.po_requisition_lines_all prla ON prla.REQUISITION_HEADER_ID = prha.REQUISITION_HEADER_ID
                        AND (prha.CANCEL_FLAG = 'N' OR prha.CANCEL_FLAG IS NULL )

           JOIN INV.MTL_CATEGORIES_B imcb  ON prla.category_id  = imcb.category_id    
           JOIN HR.PER_ALL_PEOPLE_F P ON P.person_id = prha.preparer_id  
           JOIN apps.HR_LOCATIONS ahl ON prla.deliver_to_location_id =  ahl.location_id
           JOIN apps.FND_USER  afu ON    p.person_id = afu.employee_id

      WHERE  prla.CREATION_DATE >= '13-JUN-14'    
                        AND  P.effective_start_date >= 
                        ALL (SELECT p_temp.EFFECTIVE_START_DATE
                        FROM HR.PER_ALL_PEOPLE_F p_temp
                        WHERE P.PERSON_ID = p_temp.PERSON_ID)
                         AND P.current_employee_flag = 'Y' 


      AND ahl.country IN ( 'US', 'CA', 'MX' ) /* countries in NA */ 
      AND imcb.SEGMENT1 = 'NONBOM'
      GROUP BY 
               imcb.SEGMENT1 , authorization_status, LOCATION_CODE
      ORDER BY Location_code Asc          

    ) ,

    WithSubquery2 AS 
    (
     SELECT  AUTHORIZATION_STATUS AS "APPROVED AUTHO",  COUNT(authorization_status) AS  "Requisition Lines Apprvd Count",  LOCATION_CODE AS "Location",
            imcb.SEGMENT1 AS "CATEGORY"  

      FROM 
           apps.po_requisition_headers_all   prha  
                    JOIN apps.po_requisition_lines_all prla ON prla.REQUISITION_HEADER_ID = prha.REQUISITION_HEADER_ID
                        AND (prha.CANCEL_FLAG = 'N' OR prha.CANCEL_FLAG IS NULL )

           JOIN INV.MTL_CATEGORIES_B imcb  ON prla.category_id  = imcb.category_id    
           JOIN HR.PER_ALL_PEOPLE_F P ON P.person_id = prha.preparer_id  
           JOIN apps.HR_LOCATIONS ahl ON prla.deliver_to_location_id =  ahl.location_id
           JOIN apps.FND_USER  afu ON    p.person_id = afu.employee_id

      WHERE  prla.CREATION_DATE >= '13-JUN-14'    
                        AND  P.effective_start_date >= 
                        ALL (SELECT p_temp.EFFECTIVE_START_DATE
                        FROM HR.PER_ALL_PEOPLE_F p_temp
                        WHERE P.PERSON_ID = p_temp.PERSON_ID)
                         AND P.current_employee_flag = 'Y' 
                       and AUTHORIZATION_STATUS = 'APPROVED'

      AND ahl.country IN ( 'US', 'CA', 'MX' ) /* countries in NA */ 
      AND imcb.SEGMENT1 = 'NONBOM'
      GROUP BY 
               imcb.SEGMENT1 , authorization_status, LOCATION_CODE
      ORDER BY Location_code Asc          



    )
          SELECT WithSubquery1."TOTAL AUTHO", WithSubquery1."Requisition Lines Count", WithSubquery2."APPROVED AUTHO",  WithSubquery2."Requisition Lines Apprvd Count"
          FROM 
                WithSubquery1 JOIN WithSubquery2 
           ON  
                WithSubquery1."Location"  = WithSubquery2."Location"

我遇到的问题是,我不确定如何生成SQL以使其显示缩进,并使用"发送到" 5个子类别中每个子类别下面都有空格的位置(" #Requisition Lines"," #Requisition Lines Approved" etc)。到目前为止,我得到的结果如下:

enter image description here

这令人困惑阅读,并没有挂起的内容。

任何提示表示赞赏,谢谢!

1 个答案:

答案 0 :(得分:1)

通常,我会说你应该担心前端而不是SQL中的显示问题。但是,如果您愿意,可以使用此技术:

WITH d AS ( 
SELECT 'Canada' loc, 'APPROVED' status, 5 this_week FROM DUAL UNION ALL
SELECT 'Canada' loc, 'NOT APPROVED' status, 6 this_week FROM DUAL UNION ALL
SELECT 'New York' loc, 'APPROVED' status, 15 this_week FROM DUAL UNION ALL
SELECT 'Philadelphia' loc, 'APPROVED' status, 8 this_week FROM DUAL UNION ALL
SELECT 'Philadelphia' loc, 'NOT APPROVED' status, 2 this_week FROM DUAL )
SELECT case when row_number() over ( partition by loc order by status ) = 1 THEN loc else null end loc_display, status, this_week From d
-- make sure your order by matches your partition and order by 
order by loc, status;