SQL select计数为1个值
我有一张这样的桌子:SQL select计数为1个值,sql,Sql,我有一张这样的桌子: +---------+------------+--------+--------------+ | Id | Name | Status | Content_type | +---------+------------+--------+--------------+ | 2960671 | PostJob | Error | general_url | | 2960670 | auto_index | Done | general_u
+---------+------------+--------+--------------+
| Id | Name | Status | Content_type |
+---------+------------+--------+--------------+
| 2960671 | PostJob | Error | general_url |
| 2960670 | auto_index | Done | general_url |
| 2960669 | auto_index | Done | document |
| 2960668 | auto_index | Error | document |
| 2960667 | auto_index | Error | document |
+---------+------------+--------+--------------+
SELECT COUNT(DISTINCT Content_type) from Indexing where Status = 'Error';
我想计算每种类型中有多少种状态为“Error”,因此结果将是1x general\u url
和2x document
我试过这样的方法:
+---------+------------+--------+--------------+
| Id | Name | Status | Content_type |
+---------+------------+--------+--------------+
| 2960671 | PostJob | Error | general_url |
| 2960670 | auto_index | Done | general_url |
| 2960669 | auto_index | Done | document |
| 2960668 | auto_index | Error | document |
| 2960667 | auto_index | Error | document |
+---------+------------+--------+--------------+
SELECT COUNT(DISTINCT Content_type) from Indexing where Status = 'Error';
但是我不知道如何从中获取内容类型你想要这个吗
select Content_type,
count(Status)
from Indexing
where Status='Error'
group by Content_type;
小组成员应完成以下工作:
SELECT Content_type, COUNT(Id) from Indexing where Status = 'Error' GROUP BY Content_type;
说明:
COUNT(x)
统计组中的行数,COUNT(*)
也会这样做
COUNT(DISTINCT x)
统计组中不同值的数量
如果没有GROUP BY
子句,GROUP就是整个记录集,因此在您的示例中,您会看到一个值(2)作为结果;i、 e.集合中有两种不同的内容类型。
模式
create table test
(id varchar2(10),
name varchar2(30),
status varchar2(20),
content_type varchar2(30)
);
insert into test values('2960671','PostJob','Error','general_url');
insert into test values('2960670','auto_index','Done','general_url');
insert into test values('2960669','auto_index','Done','document');
insert into test values('2960668','auto_index','Error','document');
insert into test values('2960667','auto_index','Error','document');
选择查询
SELECT LISTAGG(content_type, ',') WITHIN GROUP (ORDER BY rownum) AS content_type,
count(content_type) as content_type_count
from
(
select distinct(content_type) content_type
FROM test
where status='Error'
);
输出
| CONTENT_TYPE | CONTENT_TYPE_COUNT |
|----------------------|--------------------|
| document,general_url | 2 |
这里的想法是打印逗号分隔的内容类型值,这样您就可以知道内容类型的计数以及实际值
SELECT count(`content_type`) as 'count', content_type as 'x content type' FROM `tablename` where status= 'Error' group by(`content_type`)
哇,这看起来不错,但这并不意味着只有1个通用url失败:)似乎我不太理解这个问题。我想你已经得到了答案