SQL Server query - Selecting COUNT(*) with DISTINCT


En SQL Server 2005 tengo una tabla cm_production que enumera todo el código que se ha puesto en producción. La tabla tiene un ticket_number, program_type, y program_name y push_number junto con algunas otras columnas.

OBJETIVO: Contar todos los nombres de programas DISTINTOS por tipo de programa y número de inserción

Lo que tengo hasta ahora es:

SELECT DISTINCT COUNT(*) AS Count, program_type AS [Type] 
FROM cm_production 
WHERE push_number=@push_number 
GROUP BY program_type

Esto me lleva a la mitad, pero está contando todos los nombres de los programas, no los distintos (lo que no espero que haga en eso consulta). Supongo que no puedo entender cómo decirle que cuente solo los distintos nombres de programas sin seleccionarlos. O algo así.

Author: marc_s, 2009-10-05

7 answers

Cuente todos los nombres de programas DISTINTOS por tipo de programa y número de inserción

SELECT COUNT(DISTINCT program_name) AS Count,
  program_type AS [Type] 
FROM cm_production 
WHERE push_number=@push_number 
GROUP BY program_type

DISTINCT COUNT(*) devolverá una fila para cada recuento único. Lo que quieres es COUNT(DISTINCT expresión): evalúa la expresión para cada fila de un grupo y devuelve el número de valores únicos y no completos.

 554
Author: Remus Rusanu,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-10-05 18:26:03

Necesitaba obtener el número de ocurrencias de cada valor distinto. La columna contenía información de la región. La simple consulta SQL con la que terminé fue:

SELECT Region, count(*)
FROM item
WHERE Region is not null
GROUP BY Region

Que me daría una lista como, por ejemplo:

Region, count
Denmark, 4
Sweden, 1
USA, 10
 74
Author: Netsi1964,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-07-31 16:17:41

Debe crear una tabla temporal para las distintas columnas y luego consultar el recuento de esa tabla

SELECT COUNT(*) 
FROM (SELECT DISTINCT column1,column2
      FROM  tablename  
      WHERE condition ) as dt

Aquí dt es una tabla temporal

 23
Author: venkatesh,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-04-30 07:33:08

Prueba esto:

SELECT
    COUNT(program_name) AS [Count],program_type AS [Type]
    FROM (SELECT DISTINCT program_name,program_type
              FROM cm_production 
              WHERE push_number=@push_number
         ) dt
    GROUP BY program_type
 13
Author: KM.,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-10-05 18:25:26
SELECT COUNT(DISTINCT program_name) AS Count, program_type AS [Type] 
FROM cm_production 
WHERE push_number=@push_number 
GROUP BY program_type
 12
Author: van,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-10-05 18:27:17

Este es un buen ejemplo donde desea obtener el recuento de Pincode que se almacena en el último campo de dirección

SELECT DISTINCT
    RIGHT (address, 6),
    count(*) AS count
FROM
    datafile
WHERE
    address IS NOT NULL
GROUP BY
    RIGHT (address, 6)
 -1
Author: Uday Phadke,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-02-01 08:27:44
select  count (distinct NumTar),'PROPIAS'
from ATM_TRANe with (nolock)
where Fecha>='2014-01-01'
  AND Fecha<='2015-05-31'and NetDestino=0
  and SystemCodResp=0
group by NetDestino 
union 
select sum (contar),'FORANEAS'
from  
(
  select  count(distinct NumTar) as contar
  from ATM_TRANe with (nolock)
  where Fecha>='2014-01-01'
    AND Fecha<='2014-01-31'
    and NetDestino!=0
    and SystemCodResp=0
  group by NetDestino
)dt
 -5
Author: Taryn,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-06-02 17:24:29