sql type subtype 統計
select * from testtable;
type subtype value
a sa 1
b sb 1
a sb error
b sa error
a sb 1
b sb 1
c sa error
c sb 1
c sc 1
c sc 1
c sa error
select sum(`value`) from testtable where value != ‘error‘ AND type =‘b‘;
sum(`value`)
2
SELECT type,subtype,SUM(value) AS TOTNUM FROM testtable WHERE value !=‘error‘ GROUP BY type,subtype;
type subtype TOTNUM
a sa 1
a sb 1
b sb 2
c sb 1
c sc 2
c sc 2
SELECT type,subtype,COUNT(value) AS TOTCOUNT FROM testtable WHERE value =‘error‘ GROUP BY type,subtype;
type subtype TOTCOUNT
a sb 1
b sa 1
c sa 2
c sa 2
select * from (select type,subtype,sum(case when value <> ‘error‘ then value else ‘0‘ end) as 總數 ,sum(case when value = ‘error‘ then 1 else ‘0‘ end) as error行數 from testtable group by type,subtype) t ORDER BY 總數 desc
type subtype 總數 error行數
c sc 2 0
b sb 2 0
a sa 1 0
c sb 1 0
a sb 1 1
c sa 0 2
b sa 0 1
sql type subtype 統計