1

我需要从许多表中生成一些关于过滤器的大数据,在那里我还需要获取一些列的总和,以及像示例这样的行数

我有 5 条记录

身份证 | 姓名 | 已删除

1 | 一个 | 1
2 | 一个 | 0
3 | 一个 | 1
4 | 乙| 1
5 | C | 1

我有疑问,

SELECT  p.name, sum(p.deleted) as del, count(p.id) as numbers from products as p
join other AS b ON p.id=b.id 

我需要的输出是,

The sum of deleted records

姓名 | 删除 | 数数

一个 | 2 | 3
乙 | 1 | 1
℃ | 1 | 1

4

3 回答 3

2

试试这个 ::

SELECT  
p.name, 
sum(p.deleted) as del, 
count(id)  as numbers
from products as p
join other AS b ON p.id=b.id 
group by p.name
于 2012-12-12T07:44:32.007 回答
1

您无需加入即可获得结果。这应该有效:

SELECT name, sum(deleted), count(1)
FROM products
GROUP BY name
于 2012-12-12T07:44:39.173 回答
1
SELECT  name,
        SUM(CASE WHEN deleted = 1 THEN 1 ELSE 0 END) Deletion,
        COUNT(*) `COunt`
FROM    products 
GROUP BY name

或者

SELECT  name,
        SUM(deleted) Deletion,
        COUNT(*) `COunt`
FROM    products 
GROUP BY name;
于 2012-12-12T07:44:56.257 回答