如何在SQL中找到以下行的SUM?

时间:2015-08-21 15:32:01

标签: sql oracle

我在SQL中有以下查询:

SELECT LOCATION_CODE AS "Location",
       AUTHORIZATION_STATUS AS "Authorization Status",
       COUNT(authorization_status) AS "Requisition Lines Count", 
       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   

它产生以下输出: enter image description here

但是,我希望它具有单个位置的总计数,而不管授权状态如何。所以,它应该是这样的:

Liberty Lake             826

而不是

Liberty Lake             767
Liberty Lake             29
Liberty Lake             29
etc

如何生成此结果?我尝试像这样添加SUM

sum(authorization_status), 

但是这给了我以下错误:

  

ORA-01722:无效号码

2 个答案:

答案 0 :(得分:0)

使用group by rollup代替group by

您将获得当前的摘要以及汇总摘要:

GROUP BY imcb.SEGMENT1,  LOCATION_CODE, ROLLUP (authorization_status)

如果您不想要任何更详细的摘要,请从您的select和group by列表中删除authorization_status。

答案 1 :(得分:0)

您尝试在文本字段上调用聚合函数SUM,这就是您收到该错误的原因。 SUM用于将数值相加。 COUNT用于计算该分组范围中行的出现次数。我认为你真正想要做的是从你的查询中删除授权状态,所以它实际上是:

SELECT LOCATION_CODE AS "Location",
       COUNT(authorization_status) AS "Requisition Lines Count", 
       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 ,  LOCATION_CODE
ORDER BY Location_code ASC   

请注意,我已将其从SELECT列表和GROUP BY子句中删除。