计算出现次数

时间:2017-05-22 13:42:10

标签: oracle count grouping

我如何满足条件?我需要计算满足条件的记录数

enter image description here

(A || B) && С
(A || B) && !С
(A || B) || С

例如第一个条件

(A || B)&& С

  • num = 1 - 符合条件。存在于CAT = A,CAT = B和CAT = C

  • num = 2 - 符合条件。出现在CAT = A和CAT = C

  • num = 3 - 不适合条件。仅出现在CAT = C中 (表达式(A || B)&&С是假的)

所以表达式的结果表看起来

COLUMN_EXP              COUNT
 (A || B) && С          2
 (A || B) && !С         0
 (A || B) || С          3

1 个答案:

答案 0 :(得分:3)

<强>查询

SET DEFINE OFF;

SELECT *
FROM   (
  SELECT COUNT( CASE WHEN ( A > 0 OR B > 0 ) AND C > 0 THEN 1 END ) AS "(A||B)&&C",
         COUNT( CASE WHEN ( A > 0 OR B > 0 ) AND C = 0 THEN 1 END ) AS "(A||B)&&!C",
         COUNT( CASE WHEN ( A > 0 OR B > 0 ) OR  C > 0 THEN 1 END ) AS "(A||B)||C"
  FROM   your_table
  PIVOT ( COUNT(*) FOR cat IN ( 'A' AS A, 'B' AS B, 'C' AS C ) )
)
UNPIVOT(
  "COUNT" FOR COLUMN_EXP IN ( "(A||B)&&C", "(A||B)&&!C", "(A||B)||C" )
);

<强>输出

COLUMN_EXP COUNT
---------- -----
(A||B)&&C      2
(A||B)&&!C     0
(A||B)||C      3