CREATE TABLE TEMP25
(
STATUS VARCHAR2(200 BYTE)
);
Insert into TEMP25
(STATUS)
Values
('PENDING');
Insert into TEMP25
(STATUS)
Values
('ERROR');
Insert into TEMP25
(STATUS)
Values
('ERROR');
Insert into TEMP25
(STATUS)
Values
('ERROR');
Insert into TEMP25
(STATUS)
Values
('NOT_REQUIRED');
Insert into TEMP25
(STATUS)
Values
('NOT_REQUIRED');
Insert into TEMP25
(STATUS)
Values
('PENDING');
Insert into TEMP25
(STATUS)
Values
('PENDING');
Insert into TEMP25
(STATUS)
Values
('ERROR');
Insert into TEMP25
(STATUS)
Values
('NOT_REQUIRED');
Insert into TEMP25
(STATUS)
Values
('ERROR');
Insert into TEMP25
(STATUS)
Values
('INVALID');
Insert into TEMP25
(STATUS)
Values
('INVALID');
Insert into TEMP25
(STATUS)
Values
('PENDING');
我想写一个查询,以便我算得像
TOTAL_RECORDS TOTAL_PENDING TOTAL_NOT_REQUIRED TOTAL_PENDING
-----------------------------------------------------------------
14 4 3 5
-----------------------------------------------------------------
我希望记录按顺序在一个查询中,我尝试使用with table
子句它有效...是否还有其他解决方案......并且dnt想要无效记录
答案 0 :(得分:5)
如果您愿意,可以使用SUM/CASE
或(SUM/DECODE
)
Select
COUNT(*) TOTAL_RECORDS ,
SUM(case when status = 'PENDING' then 1 else 0 END) TOTAL_PENDING,
SUM(case when status = 'NOT_REQUIRED' then 1 else 0 END) TOTAL_NOT_REQUIRED,
SUM(case when status = 'ERROR' then 1 else 0 END) TOTAL_ERROR
FROM temp25
您也可以使用枢轴,但获得计数(*)有点难看
WITH
COUNTS AS(
select *
from (
select status
from TEMP25 t
)
pivot
(
count(status)
for status in ('PENDING' AS TOTAL_PENDING,
'NOT_REQUIRED' AS TOTAL_NOT_REQUIRED,
'ERROR' AS TOTAL_ERROR)
))
SELECT COUNT(*) total_records,
total_pending,
total_not_required,
total_error
FROM temp25,
counts
GROUP BY total_pending,
total_not_required,
total_error
答案 1 :(得分:4)
您可以轻松地使用case语句并获得此结果...也可以查看...
Select COUNT(*) TOTAL_RECORDS ,
SUM(case when status = 'ERROR' then 1 else 0 END) TOTAL_ERROR
SUM(case when status = 'NOT_REQUIRED' then 1 else 0 END) TOTAL_NOT_REQUIRED,
SUM(case when status = 'PENDING' then 1 else 0 END) TOTAL_PENDING,
FROM temp25
答案 2 :(得分:0)
在Oracle中你可以
SELECT
(SELECT COUNT(*) FROM TEMP25) total,
(SELECT COUNT(*) FROM TEMP25 WHERE STATUS='PENDING') pending,
[...]
FROM dual ;
所有数据库都有一个空表,你可以从这种事情中使用它。