1
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它的子句...是否有任何其他解决方案...并且不想要无效记录

4

3 回答 3

5

You can use SUM/CASE or (SUM/DECODE if you prefer)

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

See it working here

You can also use pivot but getting the count(*) is a little ugly

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 

Pivot version

于 2012-04-19T06:04:11.500 回答
4

You can easily make use of case when statement and get this result ...have look to it also...

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
于 2012-04-19T06:02:30.113 回答
0

在 Oracle 中,您可以

SELECT
  (SELECT COUNT(*) FROM TEMP25) total,
  (SELECT COUNT(*) FROM TEMP25 WHERE STATUS='PENDING') pending,
[...]
FROM dual ;

所有数据库都有一个空表,你可以从这种东西中使用。

于 2012-04-19T06:21:18.917 回答