在Oracle中分组

时间:2015-03-18 18:41:09

标签: sql oracle pervasive

如何在oracle中使用组?我已经尝试了几个小时,我仍然不知道为什么我得到这个错误。原始的sql是无处不在的。我想把它转换成oracle。

我得到的错误是:

ORA-00904: "SHA": invalid identifier
00904. 00000 -  "%s: invalid identifier"

这是我的疑问:

SELECT 
  CASE 
    WHEN (pch + pk) > 999999999 
      THEN 'j-9999 and OVER' 
    WHEN (pch + pk) >= 1000000 
      THEN 'i-1, < 999'  
  END AS SHA,  
  SUM(pch + pk) AS TOTALSHA
FROM hold, pos  
WHERE  hold.ho = pos.pho AND (pch + pk) > 0 
GROUP BY SHA;

1 个答案:

答案 0 :(得分:3)

您无法对输出值进行分组。重复表达式:

SELECT 
  CASE 
    WHEN (pch + pk) > 999999999 
      THEN 'j-9999 and OVER' 
    WHEN (pch + pk) >= 1000000 
      THEN 'i-1, < 999'  
  END AS SHA,  
  SUM(pch + pk) AS TOTALSHA
FROM hold, pos  
WHERE  hold.ho = pos.pho AND (pch + pk) > 0 
GROUP BY CASE 
    WHEN (pch + pk) > 999999999 
      THEN 'j-9999 and OVER' 
    WHEN (pch + pk) >= 1000000 
      THEN 'i-1, < 999'  
  END;

或者,计算子查询中的值并将结果分组:

SELECT
  SHA,
  SUM(pch + pk) AS TOTALSHA
FROM (
  SELECT 
    CASE 
      WHEN (pch + pk) > 999999999 
        THEN 'j-9999 and OVER' 
      WHEN (pch + pk) >= 1000000 
        THEN 'i-1, < 999'  
    END AS SHA,  
    pch,
    pk
  FROM hold, pos  
  WHERE  hold.ho = pos.pho AND (pch + pk) > 0 
) AS X
GROUP BY SHA