CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
不看会后悔的Windows XP之经验谈 简单快捷DIY实用家庭影院
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  MS-SQL Server >  基础类

SQL SERVER查询请教.

楼主shidb(bonne)2005-09-19 09:48:04 在 MS-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

相关问题

  • SQL SERVER 查询
  • 关于SQL SERVER 的查询
  • SQL server 日期查询
  • SQL SERVER查询故障~~
  • SQL Server的连接查询???
  • 请问sql server “sql查询分析器”
  • SQL Server分布式数据查询
  • SQL Server子查询,请帮助!!!!!!!!!!
  • sql server怎么跨库查询?
  • 两个日期间的查询(sql server)!

关键词

  • count
  • expression
  • rows
  • number

得分解答快速导航

  • 帖主:shidb
  • klan
  • tina2elva
  • samfeng_2003
  • vivianfdlpw

相关链接

  • SQL Server类图书

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo