我想让我的报告更快,因为需要花费很长时间来加载,我想知道是否有办法从一个选择中进行多次计数。
例如,使这个选择愿望不会改变计数
SELECT IDS, Fecha_Recarga, Banco_Recarga, Tipo, Status, RefVerif, MontoVerif, RecargaDuplicada FROM transaccionesrr WHERE Fecha_Recarga = '2017-02-07' AND Banco_Recarga = 'BANESCO' AND Tipo = 'RECARGA' AND Status = 'PROCESADA'
根据这个选择的结果我想做多个计数,如
Count (*) WHERE RefVerif = '1' AS RefVerif
Count (*) WHERE RefVerif = '1' AND MontoVerif = '0' AS MontoVerif
Count (*) WHERE RecargaDuplicada = '1' AS Duplicada
这是我知道的存储过程
SELECT COUNT(*) AS RECARGAS,
(SELECT COUNT(*) FROM transaccionesrr WHERE Tipo = 'RECARGA' AND Status = 'PROCESADA' AND Fecha_Recarga BETWEEN PRM_Fecha_Desde AND PRM_Fecha_Hasta AND Banco_Recarga = PRM_Banco AND RefVerif = '1') AS VERIFICADAS,
(SELECT COUNT(*) FROM transaccionesrr WHERE Tipo = 'RECARGA' AND Status = 'PROCESADA' AND Fecha_Recarga BETWEEN PRM_Fecha_Desde AND PRM_Fecha_Hasta AND Banco_Recarga = PRM_Banco AND RefVerif = '0') AS NOVERIFICADAS,
(SELECT COUNT(*) FROM transaccionesrr WHERE Tipo = 'RECARGA' AND Status = 'PROCESADA' AND Fecha_Recarga BETWEEN PRM_Fecha_Desde AND PRM_Fecha_Hasta AND Banco_Recarga = PRM_Banco AND RefVerif = '1' AND MontoVerif = '0') AS MONTOVERIF,
(SELECT COUNT(*) FROM transaccionesrr WHERE Tipo = 'RECARGA' AND Status = 'PROCESADA' AND Fecha_Recarga BETWEEN PRM_Fecha_Desde AND PRM_Fecha_Hasta AND Banco_Recarga = PRM_Banco AND RecargaDuplicada = '1') AS DUPLICADAS
FROM transaccionesrr WHERE Tipo = 'RECARGA' AND Status = 'PROCESADA' AND Fecha_Recarga BETWEEN PRM_Fecha_Desde AND PRM_Fecha_Hasta AND Banco_Recarga = PRM_Banco ;
我试图这样做,因为我认为如果你认为有更好的方式我会受到赞赏会更快。
我一直在努力寻找解决方案,但我找不到任何
答案 0 :(得分:2)
使用条件聚合:
SELECT COUNT(*) AS RECARGAS,
SUM(RefVerif = '1') AS VERIFICADAS,
SUM(RefVerif = '0') AS NOVERIFICADAS,
SUM(RefVerif = '1' AND MontoVerif = '0') AS MONTOVERIF,
SUM(RecargaDuplicada = '1') AS DUPLICADAS
FROM transaccionesrr
WHERE Tipo = 'RECARGA' AND Status = 'PROCESADA' AND
Fecha_Recarga BETWEEN PRM_Fecha_Desde AND PRM_Fecha_Hasta AND
Banco_Recarga = PRM_Banco ;