CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
山寨机中的战斗机! 程序优化工程师到底对IT界有没有贡献
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  MS-SQL Server >  基础类

sql分类汇总的问题!!!!!急!!!!!!!!!!!!!!!!!在线等

楼主qingyun67(benben兔)2005-03-12 12:31:06 在 MS-SQL Server / 基础类 提问

编号           性质         数量           指标1           指标2  
    1                   00               10             1.1               10  
    2                   01               20             1.2               20  
    3                   00               30             1.5               10  
    4                   01               40             1.9               35  
    5                   00               40             1.2               20  
  得到这样的查询结果:  
   
  指标1         范围                               性质(00)                         性质(01)  
                    <1.0                                     0                                           0                          
                1.0-1.29                   (10+40)/(10+30+40)                 20/(20+40)  
                1.3-1.59                       30/(10+30+40)                               0  
                1.6-1.99                                   0                                     40/(20+40)  
                    >=2                                         0                                           0  
  指标1平均值:                       (1.1+1.5+1.2)/3                     (1.2+1.9)/2  
  指标2         <10                                         0                                           0  
                  10-30                           (30+40)/(30+40)                           20/(20+40)    
                  31-50                                       0                                           40/(20+40)  
                    >=50                                       0                                           0  
  指标2平均值:                             (10+10+20)/3                             (20+35)/2  
   
  数量合计:                                     10+30+40                                       20+40  
   
  请问大家这样的汇总怎么用sql语句怎么实现呢??  
  实现这样的格式,怎么做呢???????请大家帮帮忙?????????  
   
  问题点数:50、回复次数:2Top

1 楼libin_ftsafe(子陌红尘:TS for Banking Card)回复于 2005-03-12 13:20:33 得分 0

同样的问题已经有人问过了。Top

2 楼zjcxc(邹建)回复于 2005-03-12 14:42:34 得分 50

--示例  
   
  --测试数据  
  create   table   tb(编号   int,性质   varchar(10),数量   int,指标1   decimal(10,1),指标2   decimal)  
  insert   tb   select   1   ,'00'   ,10,1.1   ,10  
  union   all   select   2   ,'01'   ,20,1.2   ,20  
  union   all   select   3   ,'00'   ,30,1.5   ,10  
  union   all   select   4   ,'01'   ,40,1.9   ,35  
  union   all   select   5   ,'00'   ,40,1.2   ,20  
  go  
   
  --查询  
  select   a,范围,[性质(00)],[性质(01)]  
  from(  
  select    
  a=case   a.id   when   1   then   '指标1'   when   21   then   '指标2'   else   ''   end,  
  范围=a.lb,  
  [性质(00)]=cast(case   when   b.a>0   then   isnull(a.a*1./b.a,0)   else   0   end   as   decimal(10,2)),  
  [性质(01)]=cast(case   when   b.a>0   then   isnull(a.a*1./b.a,0)   else   0   end   as   decimal(10,2)),  
  a.id  
  from(  
  select   b.id,b.lb,  
  a=sum(case   a.性质   when   '00'   then   a.数量   end),  
  b=sum(case   a.性质   when   '01'   then   a.数量   end)  
  from   tb   a    
  right   join(  
  select   id=1,lb='<1.0'         ,a=null,b=1.0     union   all  
  select   id=2,lb='1.0-1.29',a=1.0   ,b=1.3     union   all  
  select   id=3,lb='1.3-1.59',a=1.3   ,b=1.9     union   all  
  select   id=4,lb='1.9-1.99',a=1.9   ,b=2.0     union   all  
  select   id=5,lb='>=2'           ,a=2.0   ,b=null  
  )b   on   a.指标1>=isnull(b.a,a.指标1)  
  and   a.指标1<isnull(b.b,a.指标1-1)  
  group   by   b.id,b.lb  
  union   all  
  select   b.id,b.lb,  
  a=sum(case   a.性质   when   '00'   then   a.数量   end),  
  b=sum(case   a.性质   when   '01'   then   a.数量   end)  
  from   tb   a   right   join(  
  select   id=21,lb='<10'     ,a=null,b=10     union   all  
  select   id=22,lb='10-31',a=10     ,b=31     union   all  
  select   id=23,lb='31-50',a=31     ,b=51     union   all  
  select   id=25,lb='>=50'   ,a=50     ,b=null  
  )b   on   a.指标2>=isnull(b.a,a.指标2)  
  and   a.指标2<isnull(b.b,a.指标2-1)  
  group   by   b.id,b.lb  
  )a,(  
  select    
  a=isnull(sum(case   性质   when   '00'   then   数量   end),0),  
  b=isnull(sum(case   性质   when   '01'   then   数量   end),0)  
  from   tb  
  )b  
  union   all  
  select   '指标1平均值','',  
  cast(isnull(  
  case    
  when   count(case   性质   when   '00'   then   性质   end)>0  
  then   sum(case   性质   when   '00'   then   指标1   end)  
  *1./count(case   性质   when   '00'   then   性质   end)  
  else   0    
  end,0)   as   decimal(10,2)),  
  cast(isnull(  
  case    
  when   count(case   性质   when   '01'   then   性质   end)>0  
  then   sum(case   性质   when   '01'   then   指标1   end)  
  *1./count(case   性质   when   '01'   then   性质   end)  
  else   0    
  end,0)   as   decimal(10,2)),  
  id=6  
  from   tb  
  union   all  
  select   '指标2平均值','',  
  cast(isnull(  
  case    
  when   count(case   性质   when   '00'   then   性质   end)>0  
  then   sum(case   性质   when   '00'   then   指标2   end)  
  *1./count(case   性质   when   '00'   then   性质   end)  
  else   0    
  end,0)   as   decimal(10,2)),  
  cast(isnull(  
  case    
  when   count(case   性质   when   '01'   then   性质   end)>0  
  then   sum(case   性质   when   '01'   then   指标2   end)  
  *1./count(case   性质   when   '01'   then   性质   end)  
  else   0    
  end,0)   as   decimal(10,2)),  
  id=26  
  from   tb  
  union   all  
  select   '数量合计:','',  
  isnull(sum(case   性质   when   '00'   then   数量   end),0),  
  isnull(sum(case   性质   when   '01'   then   数量   end),0),  
  id=30  
  from   tb  
  )a   order   by   id  
  go  
   
  --删除测试  
  drop   table   tb  
   
  /*--测试结果  
   
   
  a                       范围               性质(00)                   性质(01)                    
  -----------   --------   --------------   --------------    
  指标1               <1.0           .00                         .00  
                          1.0-1.29   .63                         .63  
                          1.3-1.59   .38                         .38  
                          1.9-1.99   .00                         .00  
                          >=2             .00                         .00  
  指标1平均值                       1.27                       1.55  
  指标2               <10             .00                         .00  
                          10-31         1.00                       1.00  
                          31-50         .00                         .00  
                          >=50           .00                         .00  
  指标2平均值                       13.33                     27.50  
  数量合计:                         80.00                     60.00  
   
  (所影响的行数为   12   行)  
  --*/Top

相关问题

  • SQL分类汇总问题
  • vb中Sql分类汇总问题
  • sql 的分类汇总问题
  • SQL 分类汇总的问题...
  • 分类汇总的sql写法
  • VFP分类汇总问题急!!
  • SQL分类汇总问题,各位请进
  • 我用SQL的"select",怎样分类汇总。比如。。。
  • 请教一个分类汇总的sql语句的写法。
  • SQL分类汇总语句出错,各位帮帮忙

关键词

  • 指标
  • 性质
  • 平均值
  • 数量
  • decimal
  • isnull
  • union
  • 范围
  • lb
  • tb

得分解答快速导航

  • 帖主:qingyun67
  • zjcxc

相关链接

  • SQL Server类图书

广告也精彩

反馈

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