Código SQL:
Ver originalSELECT DISTINCT SubQuery3.TIPO_INC, COUNT(SubQuery3.TIPO_INC) AS Cuenta INTO #temp FROM
(
SELECT AVISOS.NUMERO, AVISOS.DNI, AVISOS.DESCRIP, DATEADD (MINUTE,MINUTOS,CONVERT(datetime, FECHA, 120)) AS T_AVISO, INTERV.TEXTO AS TIPO_INC, FPRIOR.TEXTO AS PRIORIDAD
FROM AVISOS INNER JOIN INTERV
ON AVISOS.TIPOINTERV=INTERV.ID
INNER JOIN FPRIOR
ON AVISOS.PRIORIDAD=FPRIOR.ID
WHERE AVISOS.CONTRATO=11
AND AVISOS.ESTADO02=0
AND AVISOS.PRIORIDAD IN (67108869,67108870,67108873)
AND DATEADD (MINUTE,MINUTOS,CONVERT(datetime, FECHA, 120)) BETWEEN CONVERT(datetime, '2014-09-01 00:00:00',120) AND CONVERT(datetime, '2014-09-30 23:59:00',120)
)
SubQuery3
GROUP BY SubQuery3.TIPO_INC
SELECT t1.TIPO_INC,t1.cuenta,t2.total FROM #temp AS t1,(
SELECT SUM(cuenta) AS total FROM #temp) AS t2
En lugar de una temporal podrias usar una CTE que seria algo como esto:
Código SQL:
Ver original-- Define the CTE expression name and column list.
WITH query_cte (tipo_inc,cuenta)
AS
-- Define the CTE query.
(
SELECT DISTINCT SubQuery3.TIPO_INC, COUNT(SubQuery3.TIPO_INC) AS Cuenta INTO #temp FROM
(
SELECT AVISOS.NUMERO, AVISOS.DNI, AVISOS.DESCRIP, DATEADD (MINUTE,MINUTOS,CONVERT(datetime, FECHA, 120)) AS T_AVISO, INTERV.TEXTO AS TIPO_INC, FPRIOR.TEXTO AS PRIORIDAD
FROM AVISOS INNER JOIN INTERV
ON AVISOS.TIPOINTERV=INTERV.ID
INNER JOIN FPRIOR
ON AVISOS.PRIORIDAD=FPRIOR.ID
WHERE AVISOS.CONTRATO=11
AND AVISOS.ESTADO02=0
AND AVISOS.PRIORIDAD IN (67108869,67108870,67108873)
AND DATEADD (MINUTE,MINUTOS,CONVERT(datetime, FECHA, 120)) BETWEEN CONVERT(datetime, '2014-09-01 00:00:00',120) AND CONVERT(datetime, '2014-09-30 23:59:00',120)
)
SubQuery3
GROUP BY SubQuery3.TIPO_INC
)
-- Define the outer query referencing the CTE name.
SELECT t1.TIPO_INC,t1.cuenta,t2.total FROM query_cte AS t1,(
SELECT SUM(cuenta) AS total FROM query_cte ) AS t2
Aclarando las CTE's solo son validas en sql server 2008 y posteriores