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