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

新手请问:如何统计组合出现的次数?

楼主bh586()2006-02-26 20:05:10 在 MS-SQL Server / 应用实例 提问

有如下表格:  
  id     n1     n2       TIME  
  001   3       4           ...  
  001   3       5           ...  
  002   4       2.4       ...  
  003   5       5.5       ...  
  003   6       5.5       ...  
  003   5       5.5       ...  
  004   5       3           ...  
  005   2.1   4.4       ...  
  ...   ...   ...  
   
  ...   ...   ...  
  N1和N2两列无规则变化,要求统计N1和N2组合的出现次数。  
  其中,ID:004   和(第二条)ID   001算同一种情况。  
  问题点数:100、回复次数:8Top

1 楼libin_ftsafe(子陌红尘:TS for Banking Card)回复于 2006-02-26 20:13:44 得分 50

select    
          case   when   n1>n2   then   n2   else   n1   end   as   n1,  
          case   when   n1>n2   then   n1   else   n2   end   as   n2,  
          count(*)  
  from  
          表  
  group   by  
          case   when   n1>n2   then   n2   else   n1   end,  
          case   when   n1>n2   then   n1   else   n2   endTop

2 楼libin_ftsafe(子陌红尘:TS for Banking Card)回复于 2006-02-26 20:16:51 得分 0

declare   @t   table(id   varchar(10),n1   numeric(2,1),n2   numeric(2,1))  
  insert   into   @t   select   '001',3       ,4  
  insert   into   @t   select   '001',3       ,5  
  insert   into   @t   select   '002',4       ,2.4  
  insert   into   @t   select   '003',5       ,5.5  
  insert   into   @t   select   '003',6       ,5.5  
  insert   into   @t   select   '003',5       ,5.5  
  insert   into   @t   select   '004',5       ,3  
  insert   into   @t   select   '005',2.1   ,4.4  
   
  select    
          case   when   n1>n2   then   n2   else   n1   end   as   n1,  
          case   when   n1>n2   then   n1   else   n2   end   as   n2,  
          count(*)   as   num  
  from  
          @t  
  group   by  
          case   when   n1>n2   then   n2   else   n1   end,  
          case   when   n1>n2   then   n1   else   n2   end  
   
  /*  
  n1       n2       num                    
  ----   ----   -----------    
  2.4     4.0     1  
  3.0     4.0     1  
  2.1     4.4     1  
  3.0     5.0     2  
  5.0     5.5     2  
  5.5     6.0     1  
  */Top

3 楼hycheng163()回复于 2006-02-26 21:54:40 得分 0

子陌红尘老兄,强阿,佩服Top

4 楼xeqtr1982(Visual C# .NET)回复于 2006-02-27 09:31:51 得分 0

学习一下:)Top

5 楼bugchen888(臭虫)回复于 2006-02-27 09:37:15 得分 0

佩服佩服。。。。。Top

6 楼zjcxc(邹建)回复于 2006-02-27 10:57:08 得分 50

select   n1,n2,   cnt=count(*)  
  from   (  
          select   n1,   n2   from   tb   where   n1>=n2    
          union   all  
          select   n2,   n1   from   tb   where   n2>n1    
  )a  
  group   by   n1,   n2Top

7 楼bh586()回复于 2006-03-01 20:24:17 得分 0

请问:如果再增加一列N3。N1,N2,N3无规则变化,要求统计N1/N2/N3组合的出现次数。      
   
  如   0.11/0.55/7.5         7.5/0.55/0.11算同一种情况  
   
   
  Top

8 楼libin_ftsafe(子陌红尘:TS for Banking Card)回复于 2006-03-06 10:29:45 得分 0

难怪眼熟:  
   
  declare   @t   table(id   int,n1   numeric(5,2),n2   numeric(5,2),n3   numeric(5,2))  
  insert   into   @t   select   '001',3     ,4     ,0.12  
  insert   into   @t   select   '001',3     ,5     ,0.5  
  insert   into   @t   select   '002',4     ,2.4,4  
  insert   into   @t   select   '003',5     ,5.5,1.2  
  insert   into   @t   select   '003',6     ,5.5,18  
  insert   into   @t   select   '003',5     ,5.5,1.2  
  insert   into   @t   select   '004',0.5,5     ,3  
  insert   into   @t   select   '005',2.1,4.4,27  
   
  select  
          (case   when   n1>=n2   and   n1>=n3   then   n1   when   n2>=n1   and   n2>=n3   then   n2   else   n3   end)   as   n1,  
          (case   when   (n1>=n2   and   n1<=n3)   or   (n1<=n2   and   n1>=n3)   then   n1  
                      when   (n2>=n1   and   n2<=n3)   or   (n2<=n1   and   n2>=n3)   then   n2  
                      else   n3   end)   as   n2,  
          (case   when   n1<=n2   and   n1<=n3   then   n1   when   n2<=n1   and   n2<=n3   then   n2   else   n3   end)   as   n3,  
          count(*)   as   num  
  from  
          @t  
  group   by  
          (case   when   n1>=n2   and   n1>=n3   then   n1   when   n2>=n1   and   n2>=n3   then   n2   else   n3   end),  
          (case   when   (n1>=n2   and   n1<=n3)   or   (n1<=n2   and   n1>=n3)   then   n1  
                      when   (n2>=n1   and   n2<=n3)   or   (n2<=n1   and   n2>=n3)   then   n2  
                      else   n3   end),  
          (case   when   n1<=n2   and   n1<=n3   then   n1   when   n2<=n1   and   n2<=n3   then   n2   else   n3   end)Top

相关问题

  • 有没有统计字符串出现次数的函数?
  • 请问统计数字出现次数的问题!
  • 请问怎样统计数组里面各个数字的出现次数?
  • 如何统计一段字符串中某个字符出现的次数??
  • 怎样在一篇文章里查找出现频率超过1次的词,并且统计出现次数
  • PB 中有没这样一个函数。统计一个字符的出现次数
  • linux下有没有统计文本文件中字符串出现次数的命令?
  • 谁能告诉我统计一个字符在一个字符串中出现次数的函数。急啊!
  • 有没有什么函数可以统计某字符在指定字符串里出现的次数?
  • 100分请教一个问题(如何统计一篇英文文章中每个单词出现的次数!)

关键词

  • 次数
  • 统计
  • t select
  • insert
  • numeric
  • 佩服
  • then
  • case
  • 出现
  • else

得分解答快速导航

  • 帖主:bh586
  • libin_ftsafe
  • zjcxc

相关链接

  • SQL Server类图书

广告也精彩

反馈

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