求一个SQL语句

haozhangpk 2003-12-31 11:40:13
no name debit credit categoryid
1001 现金 12.00000000 212.00000000 1
1002 银行存款 200.00000000 NULL 1
1009 其它货币资金 NULL 100.00000000 1
1101 短期投资 500.00000000 NULL 1
1141 坏帐准备 NULL 500.00000000 1
2141 代销商品款 200.00000000 NULL 2
5601 营业外支出 NULL 100.00000000 4

我想得到的结果为
no name debit credit
1001 现金 12.00000000 212.00000000
1002 银行存款 200.00000000 NULL
1009 其它货币资金 NULL 100.00000000
1101 短期投资 500.00000000 NULL
1141 坏帐准备 NULL 500.00000000
712.00000000 812.00000000
2141 代销商品款 200.00000000 NULL
200.0000000
5601 营业外支出 NULL 100.00000000
100.0000000

表脚本如下:
CREATE TABLE [dbo].[tempt] (
[accountnum] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[accountname] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[debit] [decimal](38, 8) NULL ,
[credit] [decimal](38, 8) NULL ,
[categoryid] [int] NOT NULL
) ON [PRIMARY]
...全文
27 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
sunrisehy2003 2004-01-05
  • 打赏
  • 举报
回复
要想別人回答,首先要將問題講清楚,不要捉迷藏
haozhangpk 2004-01-05
  • 打赏
  • 举报
回复
没有一个人能搞定,只有自己来了。

select accountnum,accountname,debit,credit from (Select CategoryID - 0.5 as seq, CategoryID, AccountNum, AccountName, Debit, Credit from tempt
Union
Select CategoryID as seq, CategoryID, null, case categoryid
when 1 then '资产小计' when 2 then '负债小计' when 3 then '所有者权益小计' when 4 then '损益小计' when 5 then '成本小计' end,
sum(Debit) as Debit, sum(Credit) as Credit
from tempt Group by CategoryID) DD
haozhangpk 2003-12-31
  • 打赏
  • 举报
回复
大哥们帮帮忙,有没有会的呀?
haozhangpk 2003-12-31
  • 打赏
  • 举报
回复
no name debit credit
1001 现金 12.00000000 212.00000000
1002 银行存款 200.00000000 NULL
1009 其它货币资金 NULL 100.00000000
1101 短期投资 500.00000000 NULL
1141 坏帐准备 NULL 500.00000000
712.00000000 812.00000000--是对categoryid=1 的借、贷方的小计
2141 代销商品款 200.00000000 NULL
200.0000000 //是对categroyid=2的借\贷方的小计
5601 营业外支出 NULL 100.00000000
100.0000000 ////是对categroyid=3的借\贷方的小计
tomhuang 2003-12-31
  • 打赏
  • 举报
回复
能说清楚点吗??
twht 2003-12-31
  • 打赏
  • 举报
回复
要实现什么?好难看得懂!……-……
真会晕了
jingxijun 2003-12-31
  • 打赏
  • 举报
回复
--测试数据
create table t1(no varchar(10),name varchar(20),debit [decimal](38, 8),credit [decimal](38, 8))
insert t1 select '1001','现金',12.00000000,212.00000000
union all select '1002','银行存款',1.00000000,2.00000000
union all select '1009','它货币资金',2.00000000,12.00000000
union all select '2141','代销商品款',12.00000000,300.00000000
union all select '5601','营业外支出',100.00000000,100.00000000
select * from t1
union all
select left(no,1)+'AAA','',debit=sum(debit),credit=sum(credit) from t1 group by left(no,1)+'AAA'
order by no
drop table t1

结果:
1001 现金 12.00000000 212.00000000
1002 银行存款 1.00000000 2.00000000
1009 它货币资金 2.00000000 12.00000000
1AAA 15.00000000 226.00000000
2141 代销商品款 12.00000000 300.00000000
2AAA 12.00000000 300.00000000
5601 营业外支出 100.00000000 100.00000000
5AAA 100.00000000 100.00000000
sysroger 2003-12-31
  • 打赏
  • 举报
回复
要实现什么?好难看得懂!……-……
zlqs 2003-12-31
  • 打赏
  • 举报
回复
试一下!
selelect * from (
select categoryid||'999' no,'小计' name,sum(debit),sum(credit) from tab_name group by categoryid
union
select * from tab_name) tmp order tmp.no
zlqs 2003-12-31
  • 打赏
  • 举报
回复
试一下!
selelect * from (
select categoryid||'999' no,'小计' name,sum(debit),sum(credit) from tab_name
union
select * from tab_name) tmp order tmp.no
jingxijun 2003-12-31
  • 打赏
  • 举报
回复
参见下帖
『http://expert.csdn.net/Expert/topic/2616/2616825.xml?temp=.473324』

34,591

社区成员

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

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