CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
可用分押宝游戏火热进行中... 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  MS-SQL Server >  基础类

菜鸟求 SQL:关于分类求和

楼主FoxLinn(业余编程爱好者)2006-03-02 14:50:45 在 MS-SQL Server / 基础类 提问

表a:  
  id     name           account  
  1     车费                   20  
  1     餐费                   50  
  2     津贴                 100  
  3     工资                 900  
   
  求除工资外的其他所有费用总和,以及工资总和 问题点数:50、回复次数:21Top

1 楼genphone_ru(哎,什么都要学,真累)回复于 2006-03-02 14:58:30 得分 0

select   '其他费用总和'   as   name,   sum(account)   as   account   where   id<>3  
  union  
  select   '工资总和'   as   name,   sum(account)   as   account   where   id=3  
  Top

2 楼FoxLinn(业余编程爱好者)回复于 2006-03-02 15:03:42 得分 0

谢谢楼上的。  
   
  我的表有10w多的数据,id各不相同(可以重复),name   也有10多个Top

3 楼ping3000(苦练葵花点穴手)回复于 2006-03-02 15:06:13 得分 0

除工资外的其他所有费用总和:  
  select   sum(account)   where   name   <>   '工资'  
  工资总和:  
  select   sum(account)   where   name   =   '工资'Top

4 楼lsqkeke(可可)回复于 2006-03-02 15:07:32 得分 0

select   [name]='其他费用',  
                amount=(select   sum(amount)   from   tb   where   [name]<>'工资')  
  union   all  
  select   '工资',  
                (select   sum(amount)   from   tb   where   [name]='工资')  
  Top

5 楼FoxLinn(业余编程爱好者)回复于 2006-03-02 15:15:23 得分 0

谢谢。  
   
  不过,如果需要计算特定id的,比如,id=100,又该如何Top

6 楼accpyy(小小鸟儿)回复于 2006-03-02 15:40:15 得分 0

用name等于或不等于来区分是工资总和还是其他费用总和,当需要计算特定id时,可以把id=特定id   当做条件参数传到sql中,默认没有也就是所有的,如指定再找到具体的Top

7 楼mislrb(上班看看早报,上上CSDN,下班看看电影)回复于 2006-03-02 16:55:21 得分 0

select   [name]=case   when   [name]='工资'   then   '工资'   else   '其他费用'   end,  
                amount=sum(account)  
  from   表a  
  --where   id=100  
  group   by   case   when   [name]='工资'   then   '工资'   else   '其他费用'   end  
   
  --若要ID=100,去掉上面的注解符  
  Top

8 楼yournose(虚心)回复于 2006-03-02 17:27:20 得分 0

select   sun(account)   AS   费用   ,费用类型   =   case   name  
                                                        when   name   =   '工资'   then   '工资'  
                                                        else   '其它费用'  
                                                        end  
  from   aTop

9 楼FoxLinn(业余编程爱好者)回复于 2006-03-06 23:40:54 得分 0

upTop

10 楼msjqd(黑色幽默)回复于 2006-03-07 08:42:53 得分 0

use   aa  
  create   table   test  
  (  
      id   int,  
      name   varchar(30),  
      account   int  
  )  
   
  insert   into   test   select   1,     '车费',                   20  
   
  insert   into   test   select   2,     '津贴',                 100  
  insert   into   test   select   3,     '工资',                 900  
   
  select   '工资'=   case   [name]   when   '工资'   then   '工资'   else   '其它'   end,  
                'cont'=   sum(account)  
      from   test  
  --   where   id   =   '100'  
    group   by   case   [name]   when   '工资'   then   '工资'   else   '其它'   end  
  drop   table   testTop

11 楼FoxLinn(业余编程爱好者)回复于 2006-04-11 16:14:18 得分 0

to:mislrb(上班看看早报,上上CSDN,下班看看电影)  
   
  你的我试过了,很满意。不过,出来的结果是2行,能否实现这样的效果:  
  工资     其他费用  
  xx           xxx  
   
  现在的结果是:  
  工资   xxx  
  其他费用     xxxTop

12 楼itblog(Just for wife!)回复于 2006-04-11 16:28:10 得分 0

select   工资=sum(case   when   [name]='工资'   then   [account]   end),其它费用=sum(case   when   [name]<>'工资'   then   [account]   end)  
  from   表a  
  Top

13 楼sxdoujg(无情过客)回复于 2006-04-11 16:31:14 得分 0

create   table   aaa(id   int,name   char(20),accont   int)  
  insert   into   aaa  
  select   1,'车费',20   union   all  
  select   1,'餐费',50   union   all  
  select   2,'津贴',100   union   all  
  select   3,'工资',900    
   
  select   distinct   (select   sum(accont)     from   aaa     where   name<>'工资')as   '工资外的其他所有费用总和',  
  (select   sum(accont)     from   aaa     where   name='工资')as   '工资总和'  
  from       aaa  
   
  drop   table   aaaTop

14 楼aniude(重返荣耀)回复于 2006-04-11 17:03:25 得分 0

-----------  
  select   sum(account)   from   表a   where   name<>'工资'    
  union   all  
  select   sum(account)   from   表a   where   name='工资'Top

15 楼FoxLinn(业余编程爱好者)回复于 2006-04-11 18:36:45 得分 0

to:itblog(每天进步一点点)  
   
  你的效果是这样:  
  工资       其他  
  xxx         null  
  null       xxx  
   
  还是不在一行  
  Top

16 楼marco08(天道酬勤)回复于 2006-04-12 10:14:47 得分 0

这年头争分不容易啊Top

17 楼ad_lee(阳光)回复于 2006-04-12 10:59:58 得分 0

想说的都被说了,帮顶Top

18 楼itblog(Just for wife!)回复于 2006-04-12 11:19:33 得分 0

to:itblog(每天进步一点点)  
   
  你的效果是这样:  
  工资       其他  
  xxx         null  
  null       xxx  
   
  还是不在一行  
   
   
  --------------------------  
   
  你是不是还有其它字段返回?如果只是上边那两个字段,不应该出现你现的那样~  
  Top

19 楼yuyu1234(杰)回复于 2006-04-12 11:25:30 得分 0

工资的总和:select   sum(account)   as   工资     from     表A     where   name='工资',  
  其它的总和:select   sum(account)   as   工资     from     表A     where   name<>'工资',  
   
   
   
  你的我试过了,很满意。不过,出来的结果是2行,能否实现这样的效果:  
  工资     其他费用  
  xx           xxx  
  解:  
  declare   @qita   real,@gongzi   real  
  select   @qita=(select   sum(account)   as   其它     from     表A     where   name<>'工资'),  
                @gongzi==(select   sum(account)   as   工资     from     表A     where   name='工资'),  
  from     表A  
   
  create   table   表B  
  {  
  ID   int   not   null   primary   key,  
  工资   real,  
  其它费用   real,  
  }  
  insert   into   表B(ID,工资,其它费用)values   ('1',@gongzi,@qita)  
  Top

20 楼yuyu1234(杰)回复于 2006-04-12 11:26:52 得分 0

把上面as     工资  
  那里变成   表ATop

21 楼gutlgutl(学习学习)回复于 2006-04-12 11:50:24 得分 0

 
  select   sum   (case   name   when   '工资'   then   accont   end)   工资   ,sum(case   when   name='餐费'or   name='津贴'or   name='车费'then   accont   end)   其他   from   aaa  
   
   
  看看这样行不行Top

相关问题

  • SQL菜鸟问题
  • 菜鸟求教sql函数
  • 菜鸟求一SQL语句
  • 菜鸟的SQL 问题
  • 菜鸟请教 SQL Server!
  • SQL查询菜鸟问题
  • 菜鸟求一SQL语句!
  • 菜鸟的sql问题?
  • 菜鸟问:sql查询?
  • 菜鸟问问;分类统计??

关键词

  • 效果
  • real
  • 工资
  • accont
  • 费用
  • sum
  • account
  • qita
  • 总
  • 车费

得分解答快速导航

  • 帖主:FoxLinn

相关链接

  • SQL Server类图书

广告也精彩

反馈

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