0

表:STATUS_TABLE

id | Status  | 
=================
1      true
2      false
3      false
4      true

如何使用 Oracle 在单独的列中计算 True 和 False?我必须只显示那些状态为真的记录。所以我把 where 条件。但我必须显示两者的计数。喜欢

MARKED_RECORD  UNMARKED_RECORD
      2                2
4

3 回答 3

3

How about something like

SELECT  SUM(CASE WHEN Status = 'true' THEN 1 ELSE 0 END) MARKED_RECORD,
        SUM(CASE WHEN Status = 'false' THEN 1 ELSE 0 END) UNMARKED_RECORD
FROM    STATUS_TABLE
于 2013-08-20T05:01:59.240 回答
0
WITH src1 AS
(
        SELECT 1 id, 'TRUE'  status FROM dual UNION ALL
        SELECT 2 id, 'TRUE'  status FROM dual UNION ALL
        SELECT 3 id, 'FLASE' status FROM dual UNION ALL
        SELECT 4 id, 'TRUE'  status FROM dual UNION ALL
        SELECT 5 id, 'FLASE' status FROM dual UNION ALL
        SELECT 6 id, 'TRUE'  status FROM dual UNION ALL
        SELECT 7 id, 'FLASE' status FROM dual
)
, src AS
(
        SELECT  id
        ,       status
        ,       SUM(DECODE(status, 'TRUE',   1, NULL)) OVER (ORDER BY id) AS is_true
        ,       SUM(DECODE(status, 'FLASE',  1, NULL)) OVER (ORDER BY id) AS is_false
        FROM    src1
)
SELECT  s1.id
,       s1.status
,       s2.m_is_true
,       s2.m_is_false
FROM    src s1
CROSS   JOIN
(
        SELECT  MAX(is_true)  AS m_is_true
        ,       MAX(is_false) AS m_is_false
        FROM    src
) s2
WHERE   s1.status = 'TRUE'
;

结果:

ID       STATUS   M_IS_TRUE     M_IS_FALSE
1        TRUE     4             3        
2        TRUE     4             3        
4        TRUE     4             3        
6        TRUE     4             3        
于 2013-08-20T09:23:49.407 回答
0

像这样

select decode(status,'true','Marked','false','Unmarked')status, 
       count(status) Count 
from sample_table 
code layoutgroup by status
于 2013-08-21T11:55:51.153 回答