SQL SERVER查询请教.
小弟初学,想请教各位大大一些基础问题.还请各位帮忙解答.
1.如何统计一张表中,[字段类型为bit,值为1]出现的次数?
SUM好像是不可以的.
PS:第一次用这里,积分给20分会不会太少??
问题点数:20、回复次数:6Top
1 楼klan(因帅被判7年)回复于 2005-09-19 09:54:36 得分 5
用count就可以了
Returns the number of items in a group.
Syntax
COUNT({[ALL | DISTINCT] expression] | *})
Arguments
ALL
Applies the aggregate function to all values. ALL is the default.
DISTINCT
Specifies that COUNT returns the number of unique nonnull values.
expression
Is an expression of any type except uniqueidentifier, text, image, or ntext. Aggregate functions and subqueries are not permitted.
*
Specifies that all rows should be counted to return the total number of rows in a table. COUNT(*) takes no parameters and cannot be used with DISTINCT. COUNT(*
) does not require an expression parameter because, by definition, it does not use information about any particular column. COUNT(*) returns the number of rows in a specified table without eliminating duplicates. It counts each row separately, including rows that contain null values.
Important Distinct aggregates, for example AVG(DISTINCT column_name), COUNT(DISTINCT column_name), MAX(DISTINCT column_name), MIN(DISTINCT column_name), and SUM(DISTINCT column_name), are not supported when using CUBE or ROLLUP. If used, Microsoft® SQL Server™ returns an error message and cancels the query.
Return Types
int
Remarks
COUNT(*) returns the number of items in a group, including NULL values and duplicates.
COUNT(ALL expression) evaluates expression for each row in a group and returns the number of nonnull values.
COUNT(DISTINCT expression) evaluates expression for each row in a group and returns the number of unique, nonnull values.Top
2 楼tina2elva(倒霉鬼)回复于 2005-09-19 09:54:37 得分 5
select count(1) from tb where [field] = 1Top
3 楼samfeng_2003(凤翼天翔)回复于 2005-09-19 09:55:06 得分 5
select count(*) from 表 where bit字段=1 group by 需分组的列Top
4 楼vivianfdlpw()回复于 2005-09-19 10:01:38 得分 5
select count(1) from 表 where [bit字段] = 1
or
select sum(case when [bit字段]=1 then 1 else 0 end)
from 表
Top
5 楼ynmc(一天多一点)回复于 2005-09-19 10:23:11 得分 0
来晚了.上面说的都对.Top
6 楼shidb(bonne)回复于 2005-09-19 10:26:09 得分 0
谢谢楼上各位的解答,现在已经可以利用语句统计出我想要的值.
接下来准备把这些和别的查询拉到一起去...还没试成功.
我从来没玩过软件,超级小白一个,莫见笑..............Top




