求一个SQL 语句 在线等!

castlooo 2007-02-07 05:09:55
BillExtDetail(收入表)
----------------------------————
cCusCode | gBusiExtract | cInvDep|
---------------------------—————
001 1000 部门1
--------------------------------------
001 200 部门2
-------------------------------------
001 300 部门1
------------------------------------



PayOutItem(支出表)
----------------------------——
cCusCode | outFee | cInvDep|
---------------------------——
001 800 部门1
-------------------------------
001 300 部门3


要求结果:要求结果:要求结果:要求结果:要求结果:要求结果:要求结果:
阐述 1、如果这个部门有收入那么从收入表中汇总并减 得出 剩余总额
2、如果这个部门没有收入数据,那么从汇总显示 得出负数
特别是没有收入数据的
没有收入数据没有收入数据的
部门名称 收入总额 支出总额 剩余总额
————————————————————————————————
部门1 1300 800 500
————————————————————————————————
部门2 200 0 200
----------------------------------------------------------------
部门3 0 300 -300
----------------------------------------------------------------
...全文
221 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
castlooo 2007-02-07
  • 打赏
  • 举报
回复
roy_88(中国风_燃烧你的激情!!!)

正解!
正解!
正解!
正解!
castlooo 2007-02-07
  • 打赏
  • 举报
回复
Top
libin_ftsafe(子陌红尘:当libin告别ftsafe) (
正解!
中国风 2007-02-07
  • 打赏
  • 举报
回复

declare @BillExtDetail table(cCusCode char(3),gBusiExtract int,cInvDep varchar(5))
insert @BillExtDetail
select '001',1000,'部门1'
union all
select '001',200,'部门2'
union all
select '001',300,'部门1'


declare @PayOutItem table(cCusCode char(3),outFee int,cInvDep varchar(5))
insert @PayOutItem
select '001',800,'部门1'
union all select '001',300,'部门3'

select [部门名称]=isnull(a.cInvDep,b.cInvDep),
[收入]=isnull((select sum(isnull(gBusiExtract,0)) from @BillExtDetail where cInvDep=a.cInvDep),0),
[支出]=isnull((select sum(isnull(outFee,0)) from @PayOutItem where cInvDep=b.cInvDep),0),
[剩余总额]=isnull((select sum(isnull(gBusiExtract,0)) from @BillExtDetail where cInvDep=a.cInvDep),0)-
isnull((select sum(isnull(outFee,0)) from @PayOutItem where cInvDep=b.cInvDep),0)
from @BillExtDetail a full join @PayOutItem b on a.cInvDep=b.cInvDep
group by a.cInvDep,b.cInvDep order by 部门名称 asc

(3 行受影响)

(2 行受影响)
部门名称 收入 支出 剩余总额
----- ----------- ----------- -----------
部门1 1300 800 500
部门2 200 0 200
部门3 0 300 -300

(3 行受影响)

castlooo 2007-02-07
  • 打赏
  • 举报
回复
在看!
w75251455 2007-02-07
  • 打赏
  • 举报
回复
create table 收入表( cCusCode varchar(5), gBusiExtract int, cInvDep varchar(5))
insert 收入表 select '001', 1000, '部门1'
union all select '001', 200, '部门2'
union all select '001', 300, '部门1'



create table 支出表(cCusCode varchar(5), outFee int, cInvDep varchar(5))
insert 支出表 select '001', 800, '部门1'
union all select '001', 300, '部门3'


select *,(收入总额-支出总额)剩余总额 from
(select isnull(a.cInvDep,b.cInvDep)部门名称,
isnull(a.gBusiExtract,0)收入总额,
isnull(b.outFee*,0)支出总额
from (select sum(gBusiExtract)gBusiExtract,cInvDep from 收入表 group by cInvDep)a
full join 支出表 b on a.cInvDep=b.cInvDep)a
w75251455 2007-02-07
  • 打赏
  • 举报
回复
部门名称 收入总额 支出总额 剩余总额
————————————————————————————————
部门1 1300 800 500
————————————————————————————————
部门2 200 0 200
----------------------------------------------------------------
部门3 0 300 -300
中国风 2007-02-07
  • 打赏
  • 举报
回复

declare @BillExtDetail table(cCusCode char(3),gBusiExtract int,cInvDep varchar(5))
insert @BillExtDetail
select '001',1000,'部门1'
union all
select '001',200,'部门2'
union all
select '001',300,'部门1'


declare @PayOutItem table(cCusCode char(3),outFee int,cInvDep varchar(5))
insert @PayOutItem
select '001',800,'部门1'
union all select '001',300,'部门3'

select [部门名称]=isnull(a.cInvDep,b.cInvDep),
[收入]=isnull(a.gBusiExtract,0),
[支出]=isnull(a.outFee,0),
[剩余总额]=isnull(a.gBusiExtract,0)-isnull(a.outFee,0)
from @BillExtDetail a full join @PayOutItem b on a.cInvDep=b.cInvDep
w75251455 2007-02-07
  • 打赏
  • 举报
回复

create table 收入表( cCusCode varchar(5), gBusiExtract int, cInvDep varchar(5))
insert 收入表 select '001', 1000, '部门1'
union all select '001', 200, '部门2'
union all select '001', 300, '部门1'



create table 支出表(cCusCode varchar(5), outFee int, cInvDep varchar(5))
insert 支出表 select '001', 800, '部门1'
union all select '001', 300, '部门3'


select *,(收入总额+支出总额)剩余总额 from
(select isnull(a.cInvDep,b.cInvDep)部门名称,
isnull(a.gBusiExtract,0)收入总额,
isnull(b.outFee*-1,0)支出总额
from (select sum(gBusiExtract)gBusiExtract,cInvDep from 收入表 group by cInvDep)a
full join 支出表 b on a.cInvDep=b.cInvDep)a
子陌红尘 2007-02-07
  • 打赏
  • 举报
回复
declare @BillExtDetail table(cCusCode char(3),gBusiExtract int,cInvDep varchar(5))
insert @BillExtDetail
select '001',1000,'部门1'
union all
select '001',200,'部门2'
union all
select '001',300,'部门1'


declare @PayOutItem table(cCusCode char(3),outFee int,cInvDep varchar(5))
insert @PayOutItem
select '001',800,'部门1' union
select '001',300,'部门3'


select
部门名称=a.cInvDep,
收入总额=isnull(b.gBusiExtract,0),
支出总额=isnull(c.outFee,0),
剩余总额=isnull(b.gBusiExtract,0)-isnull(c.outFee,0)
from
(select distinct cInvDep from @BillExtDetail
union
select distinct cInvDep from @PayOutItem) a
left join
(select cInvDep,sum(gBusiExtract) as gBusiExtract from @BillExtDetail group by cInvDep) b
on
a.cInvDep=b.cInvDep
left join
(select cInvDep,sum(outFee) as outFee from @PayOutItem group by cInvDep) c
on
a.cInvDep=c.cInvDep
order by
a.cInvDep


/*
部门名称 收入总额 支出总额 剩余总额
-------- ----------- ----------- -----------
部门1 1300 800 500
部门2 200 0 200
部门3 0 300 -300
*/
子陌红尘 2007-02-07
  • 打赏
  • 举报
回复
select
部门名称=a.cInvDep,
收入总额=isnull(b.gBusiExtract,0),
支出总额=isnull(c.outFee,0),
剩余总额=isnull(b.gBusiExtract,0)-isnull(c.outFee,0)
from
(select distinct cInvDep from BillExtDetail
union
select distinct cInvDep from PayOutItem) a
left join
(select cInvDep,sum(gBusiExtract) as gBusiExtract from BillExtDetail group by cInvDep) b
on
a.cInvDep=b.cInvDep
left join
(select cInvDep,sum(outFee) as outFee from PayOutItem group by cInvDep) c
on
a.cInvDep=c.cInvDep
order by
a.cInvDep
akuzou 2007-02-07
  • 打赏
  • 举报
回复
declare @BillExtDetail table(cCusCode char(3),gBusiExtract int,cInvDep varchar(5))
insert @BillExtDetail
select '001',1000,'部门1'
union all
select '001',200,'部门2'
union all
select '001',300,'部门1'


declare @PayOutItem table(cCusCode char(3),outFee int,cInvDep varchar(5))
insert @PayOutItem
select '001',800,'部门1'
union all select '001',300,'部门3'

------查询-------------------------------
select isnull(a.cInvDep,b.cInvDep) 部门名称,
isnull(a.gBusiExtract,0) 收入总额,
isnull(b.outFee,0) 支出总额,
isnull(a.gBusiExtract,0)-isnull(b.outFee,0) 剩余总额
from (select cInvDep,
sum(gBusiExtract) gBusiExtract
from @BillExtDetail
group by cInvDep) a
full join
(select cInvDep,
sum(outFee) outFee
from @PayOutItem
Group by cInvDep) b
on a.cInvDep = b.cInvDep
--结果
部门名称 收入总额 支出总额 剩余总额
----- ----------- ----------- -----------
部门1 1300 800 500
部门2 200 0 200
部门3 0 300 -300

(所影响的行数为 3 行)
castlooo 2007-02-07
  • 打赏
  • 举报
回复
---测试数据---------------------------------
declare @BillExtDetail table(cCusCode char(3),gBusiExtract int,cInvDep varchar(5))
insert @BillExtDetail
select '001',1000,'部门1'
union all
select '001',200,'部门2'
union all
select '001',300,'部门1'


declare @PayOutItem table(cCusCode char(3),outFee int,cInvDep varchar(5))
insert @PayOutItem
select '001',800,'部门1'

------查询-------------------------------
select a.cInvDep 部门名称,
isnull(a.gBusiExtract,0) 收入总额,
isnull(b.outFee,0) 支出总额,
isnull(a.gBusiExtract,0)-isnull(b.outFee,0) 剩余总额
from (select cInvDep,
sum(gBusiExtract) gBusiExtract
from @BillExtDetail
group by cInvDep) a
left outer join
(select cInvDep,
sum(outFee) outFee
from @PayOutItem
Group by cInvDep) b
on a.cInvDep = b.cInvDep
---结果---------------------------------
-- 部门名称 收入总额 支出总额 剩余总额
-- ----- ----------- ----------- -----------
-- 部门1 1300 800 500
-- 部门2 200 0 200
--
-- (所影响的行数为 2 行)


+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++===============================********************************************************************************************
不能实现 ::如果这个部门没有收入数据,那么从汇总显示 得出负数

***********************************************************
——————————————+——+——++——+——+——+——+——+——

22,210

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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