我需要从许多表中生成一些关于过滤器的大数据,在那里我还需要获得一些列的总和,以及像行一样的行数
我有5条记录
ID | NAME | DELETED
1 | A | 1
2 | A | 0
3 | A | 1
4 | B | 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
NAME |删除|计数
A | 2 | 3
B | 1 | 1
C | 1 | 1个
答案 0 :(得分: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
答案 1 :(得分:1)
您不需要加入即可获得结果。这应该有效:
SELECT name, sum(deleted), count(1)
FROM products
GROUP BY name
答案 2 :(得分:1)
SELECT name,
SUM(CASE WHEN deleted = 1 THEN 1 ELSE 0 END) Deletion,
COUNT(*) `COunt`
FROM products
GROUP BY name
OR
SELECT name,
SUM(deleted) Deletion,
COUNT(*) `COunt`
FROM products
GROUP BY name;