选择 COUNT(*) 和 DISTINCT [英] Selecting COUNT(*) with DISTINCT
问题描述
在 SQL Server 2005 中,我有一个表 cm_production
,其中列出了所有已投入生产的代码.该表具有 ticket_number
、program_type
、program_name
和 push_number
以及一些其他列.
In SQL Server 2005 I have a table cm_production
that lists all the code that's been put into production. The table has a ticket_number
, program_type
, program_name
and push_number
along with some other columns.
目标:按程序类型和推送数量计算所有 DISTINCT 程序名称.
GOAL: Count all the DISTINCT program names by program type and push number.
到目前为止我所拥有的是:
What I have so far is:
DECLARE @push_number INT;
SET @push_number = [HERE_ADD_NUMBER];
SELECT DISTINCT COUNT(*) AS Count, program_type AS [Type]
FROM cm_production
WHERE push_number=@push_number
GROUP BY program_type
这让我走到了一半,但它计算了所有程序名称,而不是不同的名称(我不希望它在该查询中这样做).我想我只是无法理解如何告诉它只计算不同的程序名称而不选择它们.或者什么.
This gets me partway there, but it's counting all the program names, not the distinct ones (which I don't expect it to do in that query). I guess I just can't wrap my head around how to tell it to count only the distinct program names without selecting them. Or something.
推荐答案
按节目类型和推送次数统计所有DISTINCT节目名称
Count all the DISTINCT program names by program type and push number
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(*)
将为每个唯一计数返回一行.你想要的是 COUNT(DISTINCT
:为组中的每一行计算表达式并返回唯一的非空值的数量.
DISTINCT COUNT(*)
will return a row for each unique count. What you want is COUNT(DISTINCT <expression>)
: evaluates expression for each row in a group and returns the number of unique, non-null values.
这篇关于选择 COUNT(*) 和 DISTINCT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!