引用选择子查询字段

时间:2017-05-02 14:30:09

标签: sql oracle

我有以下代码,它使用select语句中的子查询来查找特定年份的销售总额。我想做的是在select语句中再次引用该总和,以确定每年增加或减少的百分比。但是当我尝试这样做时,我当前收到“无效标识符”错误。

SELECT    mc.rsm as terr
    , mc.salesman_code
        , mc.customer_id
        , mc.name
        , mc.city
        , mc.state
        , mc.discount
        , DECODE(cp.customer_id,NULL,'N','Y') as price_list
        , DECODE(ca.customer_no,NULL,'N','Y') as agreement
        , i.FY_sales
        ,(SELECT SUM(inv.sales) as Last_FY_sales                
            FROM ifsinfo.hb_invoicing_all inv 
            WHERE inv.site IN ('06','01')
                AND TO_DATE(inv.invdate) between Trunc(sysdate,'Year')-458 and Trunc(sysdate,'Year')-92
                And inv.customer_id = mc.customer_id            
               HAVING SUM(inv.sales) <> 0
           )  FY_Last_Year 
        , i.FY_sales / FY_Last_Year as Percent_increaseORdecrease         
FROM iffo.info mc 
LEFT JOIN(SELECT inv.company as company
               , inv.customer_id as cust
               , inv.address_id
               , SUM(inv.sales) as FY_sales
               , SUM(inv.cost) as costs 
            FROM ifsinfo.hb_invoicing_all inv 
            WHERE inv.site IN ('06','01')           
                AND TO_DATE(inv.invdate) between Trunc(sysdate,'Year')-92 and add_months(trunc(sysdate,'YEAR'),12)-93                 
            GROUP BY inv.company
               , inv.customer_id
               , inv.address_id `enter code here`
            HAVING SUM(inv.sales) <> 0
           ) i                    
ON (mc.company = i.company)
    AND (mc.customer_id = i.cust)
    AND (mc.address_id = i.address_id)

3 个答案:

答案 0 :(得分:1)

您可以尝试将子查询提取到CTE中:

with sales(customer_id, Last_FY_sales) as (
  SELECT inv.customer_id, SUM(inv.sales) as Last_FY_sales                
  FROM ifsinfo.hb_invoicing_all inv 
  WHERE inv.site IN ('06','01')
  AND TO_DATE(inv.invdate) between Trunc(sysdate,'Year')-458 and Trunc(sysdate,'Year')-92
  GROUP BY inv.customer_id           
  HAVING SUM(inv.sales) <> 0
)

SELECT    mc.rsm as terr
        , mc.salesman_code
        , mc.customer_id
        , mc.name
        , mc.city
        , mc.state
        , mc.discount
        , DECODE(cp.customer_id,NULL,'N','Y') as price_list
        , DECODE(ca.customer_no,NULL,'N','Y') as agreement
        , i.FY_sales
        , sales.Last_FY_sales FY_Last_Year 
        , i.FY_sales / sales.Last_FY_sales as Percent_increaseORdecrease         
FROM iffo.info mc 
LEFT JOIN sales on sales.customer_id = mc.customer_id
LEFT JOIN(SELECT inv.company as company
               , inv.customer_id as cust
               , inv.address_id
               , SUM(inv.sales) as FY_sales
               , SUM(inv.cost) as costs 
          FROM ifsinfo.hb_invoicing_all inv 
          WHERE inv.site IN ('06','01')           
                AND TO_DATE(inv.invdate) between Trunc(sysdate,'Year')-92 and add_months(trunc(sysdate,'YEAR'),12)-93                 
          GROUP BY inv.company
               , inv.customer_id
               , inv.address_id `enter code here`
          HAVING SUM(inv.sales) <> 0
           ) i                    
ON (mc.company = i.company)
    AND (mc.customer_id = i.cust)
    AND (mc.address_id = i.address_id)

答案 1 :(得分:0)

将您的子查询添加为联接:

SELECT ...., 
  inv.Last_FY_sales, i.FY_sales / inv.FY_Last_Year as Percent_increaseORdecrease 
FROM ...
JOIN (SELECT customer_id, SUM(inv.sales) as Last_FY_sales                
      FROM ifsinfo.hb_invoicing_all inv 
      WHERE inv.site IN ('06','01')
      AND TO_DATE(inv.invdate) between Trunc(sysdate,'Year')-458 and 
                Trunc(sysdate,'Year')-92
      GROUP BY inv.customer_id             
      HAVING SUM(inv.sales) <> 0 ) inv 
         ON inv.customer_id = mc.customer_id

答案 2 :(得分:0)

这是一个解决方案,它将ifsinfo.hb_invoicing_all上的两个查询合并为一个,使用CASE()语句来维护不同年份的聚合。这意味着您可以避免重复代码,同时仍然具有主查询范围内的所有值。

SELECT  mc.rsm as terr
        , mc.salesman_code
        , mc.customer_id
        , mc.name
        , mc.city
        , mc.state
        , mc.discount
        , DECODE(cp.customer_id,NULL,'N','Y') as price_list
        , DECODE(ca.customer_no,NULL,'N','Y') as agreement
        , i.FY_sales
        , i.FY_Last_Year 
        , i.FY_sales / i.FY_Last_Year as Percent_increaseORdecrease         
FROM iffo.info mc 
LEFT JOIN  (SELECT inv.company as company
               , inv.customer_id as cust
               , inv.address_id
               , SUM(case when TO_DATE(inv.invdate) between Trunc(sysdate,'Year')-92 and add_months(trunc(sysdate,'YEAR'),12)-93 
                          then   inv.sales else 0 end) as FY_sales
               , SUM(case when TO_DATE(inv.invdate) between Trunc(sysdate,'Year')-92 and add_months(trunc(sysdate,'YEAR'),12)-93 
                          then   inv.cost else 0 end) as ) as costs 
              , SUM(case when TO_DATE(inv.invdate) between Trunc(sysdate,'Year')-458 and Trunc(sysdate,'Year')-92
                         then   inv.sales else 0 end) as FY_Last_Year 
            FROM ifsinfo.hb_invoicing_all inv 
            WHERE inv.site IN ('06','01')           
                AND TO_DATE(inv.invdate) between  Trunc(sysdate,'Year')-458 and add_months(trunc(sysdate,'YEAR'),12)-93                 
            GROUP BY inv.company
               , inv.customer_id
               , inv.address_id
            HAVING SUM(inv.sales) <> 0
           )       i  
ON (mc.company = i.company)
    AND (mc.customer_id = i.cust)
    AND (mc.address_id = i.address_id);

警告:该问题不包含架构定义或示例数据,因此无法保证语法正确性。