菜鸟求 SQL:关于分类求和

FoxLinn 2006-03-02 02:50:45
表a:
id name account
1 车费 20
1 餐费 50
2 津贴 100
3 工资 900

求除工资外的其他所有费用总和,以及工资总和
...全文
263 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
gutlgutl 2006-04-12
  • 打赏
  • 举报
回复

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


看看这样行不行
yuyu1234 2006-04-12
  • 打赏
  • 举报
回复
把上面as 工资
那里变成 表A
yuyu1234 2006-04-12
  • 打赏
  • 举报
回复
工资的总和: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)
itblog 2006-04-12
  • 打赏
  • 举报
回复
to:itblog(每天进步一点点)

你的效果是这样:
工资 其他
xxx null
null xxx

还是不在一行


--------------------------

你是不是还有其它字段返回?如果只是上边那两个字段,不应该出现你现的那样~
ad_lee 2006-04-12
  • 打赏
  • 举报
回复
想说的都被说了,帮顶
marco08 2006-04-12
  • 打赏
  • 举报
回复
这年头争分不容易啊
FoxLinn 2006-04-11
  • 打赏
  • 举报
回复
to:itblog(每天进步一点点)

你的效果是这样:
工资 其他
xxx null
null xxx

还是不在一行
aniude 2006-04-11
  • 打赏
  • 举报
回复
-----------
select sum(account) from 表a where name<>'工资'
union all
select sum(account) from 表a where name='工资'
sxdoujg 2006-04-11
  • 打赏
  • 举报
回复
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 aaa
itblog 2006-04-11
  • 打赏
  • 举报
回复
select 工资=sum(case when [name]='工资' then [account] end),其它费用=sum(case when [name]<>'工资' then [account] end)
from 表a
FoxLinn 2006-04-11
  • 打赏
  • 举报
回复
to:mislrb(上班看看早报,上上CSDN,下班看看电影)

你的我试过了,很满意。不过,出来的结果是2行,能否实现这样的效果:
工资 其他费用
xx xxx

现在的结果是:
工资 xxx
其他费用 xxx
msjqd 2006-03-07
  • 打赏
  • 举报
回复
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 test
FoxLinn 2006-03-06
  • 打赏
  • 举报
回复
up
yournose 2006-03-02
  • 打赏
  • 举报
回复
select sun(account) AS 费用 ,费用类型 = case name
when name = '工资' then '工资'
else '其它费用'
end
from a
mislrb 2006-03-02
  • 打赏
  • 举报
回复
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,去掉上面的注解符
accpyy 2006-03-02
  • 打赏
  • 举报
回复
用name等于或不等于来区分是工资总和还是其他费用总和,当需要计算特定id时,可以把id=特定id 当做条件参数传到sql中,默认没有也就是所有的,如指定再找到具体的
FoxLinn 2006-03-02
  • 打赏
  • 举报
回复
谢谢。

不过,如果需要计算特定id的,比如,id=100,又该如何
lsqkeke 2006-03-02
  • 打赏
  • 举报
回复
select [name]='其他费用',
amount=(select sum(amount) from tb where [name]<>'工资')
union all
select '工资',
(select sum(amount) from tb where [name]='工资')
ping3000 2006-03-02
  • 打赏
  • 举报
回复
除工资外的其他所有费用总和:
select sum(account) where name <> '工资'
工资总和:
select sum(account) where name = '工资'
FoxLinn 2006-03-02
  • 打赏
  • 举报
回复
谢谢楼上的。

我的表有10w多的数据,id各不相同(可以重复),name 也有10多个
加载更多回复(1)

34,575

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧