2
SELECT HQRH_BADGENO,
       HQRH_SHIFT,
       HQBD_BADGENAME,
       HQRH_DISCRRECONCILE,
       HQRH_ADJUSTAMOUNT,
       HQRH_DISCRADJUSTMENT,
       CASE HQRH_DISCRRECONCILE
         WHEN 'HQRH_DISCRRECONCILE <0' THEN
          'HQRH_ADJUSTAMOUNT'
         WHEN 'HQRH_ADJUSTAMOUNT>0' THEN
          'HQRH_ADJUSTAMOUNT'
         WHEN 'HQRH_DISCRADJUSTMENT<0' THEN
          'HQRH_DISCRADJUSTMENT * (-1)'
         ELSE
          'HQRH_DISCRRECONCILE>0'
       END
  FROM HQ_CASHRECONCILEHEADER, HQ_BADGE
 WHERE HQ_CASHRECONCILEHEADER.HQRH_PLAZANO = HQ_BADGE.HQBD_PLAZANO
   and HQ_CASHRECONCILEHEADER.HQRH_BADGENO = HQ_BADGE.HQBD_BADGENO
   AND (HQ_CASHRECONCILEHEADER.HQRH_OPERATIONALDATE =
       TO_DATE(:dtstart, 'DD/MM/YYYY'))
   AND (HQ_CASHRECONCILEHEADER.HQRH_PLAZANO = :plazano)

我需要将 case/if else 条件应用于上面的 select 语句。我试过了,但我得到了一个错误。那个错误是

ORA : 00923: from keyword not found where expected

实际上我想应用这样的 if-else 条件:

if colA -ve then                             ' here colA = excess
    if colB +ve then paid = colB             ' here colB = paid
    elseif colC -ve then unpaid = colc * -1  ' here colC = unpaid
    else do nothing
else if colA +ve then excess = cola
else do nothing

任何人都可以检查我的陈述并提出任何建议吗?

4

3 回答 3

6

这就是我重新编写您的查询的方式:

SELECT crh.hqrh_badgeno,
       crh.hqrh_shift,
       b.hqbd_badgename,
       crh.hqrh_discrreconcile,
       crh.hqrh_adjustamount,
       crh.hqrh_discradjustment,
       CASE 
         WHEN crh.hqrh_discrreconcile < 0 THEN crh.hqrh_adjustamount
         WHEN crh.hqrh_adjustamount > 0 THEN crh.hqrh_adjustamount
         WHEN crh.hqrh_discradjustment < 0 THEN crh.hqrh_discradjustment * (-1)
         ELSE 0 --'HQRH_DISCRRECONCILE > 0' 
      END
 FROM HQ_CASHRECONCILEHEADER crh
 JOIN HQ_BADGE b ON b.hqbd_plazano = crh.hqrh_plazano
                AND b.hqbd_badgeno = crh.hqrh_badgeno
WHERE crh.hqrh_operationaldate = TO_DATE(:dtstart, 'DD/MM/YYYY')
  AND crh.hqrh_plazano = :plazano

如果没有 WHEN 子句匹配,我不清楚应该发生什么。CASE 表达式需要始终返回相同的数据类型,单引号内的任何内容都被视为字符串,但其他值似乎是数字。所以我将它硬编码为返回零,并让 ELSE 子句在注释中可见(不会出现在查询输出中)。

于 2010-09-30T04:47:56.353 回答
2

尝试这个

SELECT HQRH_BADGENO,
   HQRH_SHIFT,
   HQBD_BADGENAME,
   HQRH_DISCRRECONCILE,
   HQRH_ADJUSTAMOUNT,
   HQRH_DISCRADJUSTMENT,
   CASE 
     WHEN HQRH_DISCRRECONCILE <0 THEN
      HQRH_ADJUSTAMOUNT
     WHEN HQRH_ADJUSTAMOUNT>0 THEN
      HQRH_ADJUSTAMOUNT
     WHEN HQRH_DISCRADJUSTMENT<0 THEN
      HQRH_DISCRADJUSTMENT * (-1)
   END 
  FROM HQ_CASHRECONCILEHEADER, HQ_BADGE
  WHERE HQ_CASHRECONCILEHEADER.HQRH_PLAZANO = HQ_BADGE.HQBD_PLAZANO
and HQ_CASHRECONCILEHEADER.HQRH_BADGENO = HQ_BADGE.HQBD_BADGENO
AND (HQ_CASHRECONCILEHEADER.HQRH_OPERATIONALDATE =
   TO_DATE(:dtstart, 'DD/MM/YYYY'))
AND (HQ_CASHRECONCILEHEADER.HQRH_PLAZANO = :plazano);

如果所有其他方法都失败,则案例将返回 null,您可以对此进行调整。最大的问题是,一旦您为 case 提供了一个参数,它将使用它在 when 语句中进行比较。在上面你可以比较不同的条件

于 2010-09-30T05:19:02.680 回答
0

用例语句时从查询中删除单引号:

       CASE HQRH_DISCRRECONCILE
         WHEN HQRH_DISCRRECONCILE <0 THEN
          HQRH_ADJUSTAMOUNT
         WHEN HQRH_ADJUSTAMOUNT>0 THEN
          HQRH_ADJUSTAMOUNT
         WHEN HQRH_DISCRADJUSTMENT<0 THEN
          HQRH_DISCRADJUSTMENT * (-1)
         ELSE
          HQRH_DISCRRECONCILE>0
       END
于 2010-09-30T04:26:42.583 回答