表:STATUS_TABLE
id | Status |
=================
1 true
2 false
3 false
4 true
如何使用 Oracle 在单独的列中计算 True 和 False?我必须只显示那些状态为真的记录。所以我把 where 条件。但我必须显示两者的计数。喜欢
MARKED_RECORD UNMARKED_RECORD
2 2
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
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
像这样
select decode(status,'true','Marked','false','Unmarked')status,
count(status) Count
from sample_table
code layoutgroup by status