Skip to main content
3 de 5
código #2
José Dız
  • 7mil
  • 2
  • 18
  • 30

Há algumas formas.

CTE

-- código #1 v2
with totVWDADOSFPY as (
SELECT 
  Data,
  SUM(CASE WHEN Status = 'OK' THEN 1 ELSE 0 END) as Passed,
  SUM(CASE WHEN Status = 'NOK' THEN 1 ELSE 0 END) as Failed,
  COUNT(*) AS Total
  from VWDADOSFPY
  group by Data
)
SELECT Data, Passed, Failed, Total, 
       Porcentagem= (Passed * 100.0) / Total
  from totVWDADOSFPY
  order by Data desc;

CÓDIGO LINEAR
Repete as fórmulas de cálculo de Passed e Total para calcular a porcentagem em um mesmo SELECT.

-- código #2
SELECT Data,
       SUM(CASE WHEN Status = 'OK' THEN 1 ELSE 0 END) as Passed,
       SUM(CASE WHEN Status = 'NOK' THEN 1 ELSE 0 END) as Failed,
       COUNT(*) AS Total,
       Porcentagem= SUM(CASE WHEN Status = 'OK' THEN 1 ELSE 0 END) * 100.0 / COUNT(*)
  from VWDADOSFPY
  group by Data
  order by Data desc;
José Dız
  • 7mil
  • 2
  • 18
  • 30