"不是按表达式分组" Oracle SQL语句

时间:2018-05-15 20:33:58

标签: sql oracle count case

我正在尝试运行以下代码,但我一直收到Not a Group by Expression错误。我做了一些研究,发现我需要添加" Group By"我做了但我不确定我做错了什么?

最终结果如果medicaid_no1计数在test_flag,我正在查找more > 0In_Op的列表等于inpatient

如果您看到下面的第1,2和4行符合条件count > 0并且Inpatient而其他人不在,那么他们会得到0

Row MEDICAID_NO TEST_FLAG
1   999999         1
2   1111111        1
3   3333333        0
4   444444         1
5   88888888       0

继承我的代码

SELECT medicaid_no, count(*)
,(CASE WHEN count(*)>0 and In_Op IN('Inpatient')
THEN 1 ELSE 0 END) AS test_FLAG
FROM top_member_claims 
GROUP BY medicaid_no

1 个答案:

答案 0 :(得分:2)

似乎In_Op表中有一个名为top_member_claims的列,应该在

Group By表达式为Group By medicaid_no, In_Op

所以,请使用:

CREATE TABLE top_member_claims
(
  row_   int,
  MEDICAID_NO    int,
  TEST_FLAG int,
  In_Op varchar2(50)
);

INSERT INTO top_member_claims VALUES (1, 999999, 1, 'Inpatient');

SELECT medicaid_no, count(*),
      (CASE WHEN count(*)>0 and In_Op IN('Inpatient')
            THEN 1 
            ELSE 0 
       END) AS test_FLAG
 FROM top_member_claims 
GROUP BY medicaid_no, In_Op;

MEDICAID_NO COUNT(*)    TEST_FLAG
  999999       1            1
  3333333      1            1
  1111111      1            0
  444444       1            0
  88888888     1            1

但请勿使用

SELECT medicaid_no, count(*),
      (CASE WHEN count(*)>0 and In_Op IN('Inpatient')
            THEN 1 
            ELSE 0 
       END) AS test_FLAG
 FROM top_member_claims 
GROUP BY medicaid_no; -- yields below

ORA-00979: not a GROUP BY expression

SQL Fiddle Demo