Oracle 中的分组依据

Group By in Oracle

oracle中group by如何使用?我已经尝试了几个小时,但我仍然不知道为什么会出现此错误。原来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;

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

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