加入两个案例陈述

时间:2014-07-27 04:47:11

标签: oracle case

我想检查一下可以加入两个案例陈述吗?我尝试过这样的事情,但我有错误消费费用是无效的标识符'。我知道我收到此错误,因为我没有ConsumerBill属性。

那么仍然可以加入两个案例陈述吗?

    SELECT CustName, preRead, newRead, newRead-preRead AS TotConsumption, 
CASE
   WHEN SYSDATE - sentDate > 30 THEN Consumption + FP
   WHEN SYSDATE - sentDate > 45 THEN Consumption + SP
   ELSE ConsumptionBill + 0
  END as BIll
FROM ( 
        SELECT C.firstName||''||C.lastName as CustName, R.newReading prevRead,
               lead(R.newReading) OVER (PARTITION BY R.electircmeterID ORDER BY R.dateVisit) AS newRead, B.firstconRate as FR,
               B.secconRate as SR, B.thirdconRate as TR, P.firstPenAmt as FP, P.secPenAmt as SP, bl.sentDate,
               CASE
                WHEN newRead-preRead <= 20 THEN (newRead-preRead)*FR
                WHEN newRead-preRead <= 30 THEN 20*FR + (newRead-preRead-100)*SR
                WHEN newRead-preRead > 40 THEN 20*FR + 30*SR + ((newRead-previousRead)-50)*TR 
              END as Consumption
        FROM reading R, svcaddress S, electricmeter W, customer C, rate A, nonresrate B, penRate P, bill bl
           WHERE W.electricmeterID = S.electricmeterID 
           AND R.electricmeterID = W.electricmeterID 
           AND A.rateID = S.rateID
           AND B.rateID = A.rateID
           AND C.customerID = S.customerID 
           AND C.customerID = bl.customerID 
           AND R.readingID = bl.readingID
           AND P.preRateID = C.preRateID
           AND C.customerType = 'NonResidential'
           AND  r.datevisited >=  TO_DATE('01-05-2013','DD-MM-YYYY') 
           AND r.datevisited < TO_DATE('31-08-2013','DD-MM-YYYY')
        )where newRead is not null;

1 个答案:

答案 0 :(得分:1)

如果没有在你正在做的事情的背景下看到它,很难说。

但是,您可能需要一个子选择

SELECT CASE
   WHEN SYSDATE - sentDate > 30 THEN ConsumptionBill + FP;
   WHEN SYSDATE - sentDate > 45 THEN ConsumptionBill + SP
   ELSE ConsumptionBill + 0
  END as TotalBIll
FROM (
  SELECT FP,
    sentDate,
    SP,
    CASE
      WHEN newRead-previousRead <= 100 THEN (newRead-previousRead)*FR
      WHEN newRead-previousRead <= 200 THEN 100*FR + (newRead-previousRead-100)*SR
      WHEN newRead-previousRead > 200 THEN 100*FR + 100*SR + ((newRead-previousRead)-200)*TR 
    END as ConsumptionBill
  FROM my_table
) t

在这里,我们创建了一个中间记录集t,其中包含我们稍后要使用的计算字段ConsumptionBill

(我说&#34;中间&#34;,但仅在概念上......查询计划者可能会也可能不会制作中间记录集 - 它可能会赢得#。)