一个SQL的简单问题

zxj971140 2008-01-10 09:22:30
表名:BankrollDayReport,字段:Type 01收入02支出,Summary 收入或支出摘要,Money 收入或支出金额,当Type='01'是,此笔记录视为收入,当Type='02'时,此笔记录视为输出,现在我要写SQL语句得到如下结果:
收入摘要 收入金额 支出摘要 支出金额
客户收款 10000 客户付款 10000

------------------------------------------------------
如上所示,就是BankrollDayReport中有两笔记录,一笔是收入Type='01',一笔是支出,请教如何写如上结果的语句,注意,要是收入一笔都没有时,就是如下结果。
收入摘要 收入金额 支出摘要 支出金额
客户付款 10000
要是支出没一笔记录时
收入摘要 收入金额 支出摘要 支出金额
客户收款 10000
...全文
162 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
wuxinyuzhu 2008-01-11
  • 打赏
  • 举报
回复


select 收款摘要=case when type=1 then 摘要+'收款' else ''end,
收款金额=case when type=1 then 金额 else 0 end,
付款摘要=case when type=2 then 摘要+'付款' else ''end,
付款金额=case when type=2 then 金额 else 0 end
from BankrollDayReport
ydlchina 2008-01-11
  • 打赏
  • 举报
回复
up
SeerMi 2008-01-11
  • 打赏
  • 举报
回复
收入摘要 收入金额 支出摘要 支出金额

楼主这样的表结构列出来的结果,实际上没有什么意义,因为同行的收入和支出没有什么关系,对不楼主?

loworth 2008-01-11
  • 打赏
  • 举报
回复
原表:
type 摘要 金额
1 a 3000
1 b 230
1 c 2366
2 a 4300
2 b 3506
2 d 5600



收款摘要 收入金额 付款摘要 支出金额
a收款 3000 a付款 4300
b收款 230 b付款 3506
c收款 2366 NULL NULL
NULL NULL d付款 5600
loworth 2008-01-11
  • 打赏
  • 举报
回复

/*检查是否存在该表*/
IF OBJECT_ID(N'[tempdb].dbo.[#BankrollDayReport]') IS NOT NULL
DROP TABLE [#BankrollDayReport]
/*创建该表*/
CREATE TABLE [#BankrollDayReport]
(
[Type] varchar(2),
[Summary] varchar(10),
[money] decimal(9,2)
)
/*插入数据*/
INSERT INTO [#BankrollDayReport]
SELECT '01','a','3000'
UNION
SELECT '02','a','4300'
UNION
SELECT '01','b','230'
UNION
SELECT '02','b','3506'
UNION
SELECT '01','c','2366'
UNION
SELECT '02','d','5600'

/*原表数据*/
SELECT * FROM [#BankrollDayReport]
/*查询结果*/
SELECT CASE WHEN [收入金额] IS NOT NULL THEN [收款摘要] ELSE NULL END AS [收款摘要],[收入金额],
CASE WHEN [支出金额] IS NOT NULL THEN [付款摘要] ELSE NULL END AS [付款摘要],[支出金额]
FROM
(
SELECT
[Summary]+'收款' AS [收款摘要],
SUM(CASE [type] WHEN '01' THEN [money] ELSE NULL END) AS [收入金额],
[Summary]+'付款' AS [付款摘要],
SUM(CASE [type] WHEN '02' THEN [money] ELSE NULL END) AS [支出金额]
FROM
[#BankrollDayReport]
GROUP BY [Summary]
) [TTemp]
/*查询结束 销毁表*/
DROP TABLE [#BankrollDayReport]
dobear_0922 2008-01-10
  • 打赏
  • 举报
回复
你的摘要怎么统计呢?
wzy_love_sly 2008-01-10
  • 打赏
  • 举报
回复

declare @BankrollDayReport table(Summary varchar(10),money int,type varchar(10))
insert into @BankrollDayReport select '1',10,'01'
insert into @BankrollDayReport select '1',20,'02'
insert into @BankrollDayReport select '2',30,'01'
insert into @BankrollDayReport select '2',40,'02'

select summary,ru=(select sum(money) from @BankrollDayReport where summary=a.summary and type='01'),
summary ,chu=(select sum(money) from @BankrollDayReport where summary=a.summary and type='02')
from @BankrollDayReport a

group by summary


summary ru summary chu
1 10 1 20
2 30 2 40

这样?
pt1314917 2008-01-10
  • 打赏
  • 举报
回复
三个字段。把表里面的数据贴一部分出来。啥事都解决了。。
zxj971140 2008-01-10
  • 打赏
  • 举报
回复
是三个字段,你看上面语句啊。
pt1314917 2008-01-10
  • 打赏
  • 举报
回复
TS收款 1025.0120 福龙付款 10000.0000
TS收款 1025.0120 供应商付款1000.0000
ONITY收款 1000.0000 福龙付款 10000.0000
ONITY收款 1000.0000 供应商付款1000.0000
----------------
你不是三个字段的吗?怎么冒出四列数据来了。还是把你正常的表结构和部分数据贴出来吧。。。
zxj971140 2008-01-10
  • 打赏
  • 举报
回复
问题没有解决呢,在线等待各位高手。
zxj971140 2008-01-10
  • 打赏
  • 举报
回复
1楼写的还是有问题,当有多笔记录,我调试了一下程序,得出如下结果。
TS收款 1025.0120 福龙付款 10000.0000
TS收款 1025.0120 供应商付款1000.0000
ONITY收款 1000.0000 福龙付款 10000.0000
ONITY收款 1000.0000 供应商付款1000.0000
其实我录入的数据各两笔,但他显示得复了
sp4 2008-01-10
  • 打赏
  • 举报
回复
haha...

LZ太浪费了。呵呵
wzy_love_sly 2008-01-10
  • 打赏
  • 举报
回复
楼主在搞什么啊?:)
utpcb 2008-01-10
  • 打赏
  • 举报
回复
select a.Summary [收入摘要],a.money [收入金额],b.Summary [支出摘要],b.money [支出金额]
from BankrollDayReport a,BankrollDayReport b where a.type='01' and b.type='02'


150分 太..JF
pt1314917 2008-01-10
  • 打赏
  • 举报
回复

个人认为:这收入和支出应该通过一个字段来关联的,除非数据库里就只有两条记录。那这样的表跟废表也毫无区别了。。

zxj971140 2008-01-10
  • 打赏
  • 举报
回复
晕,这么简单,我想了N久都不知道怎么办。
pt1314917 2008-01-10
  • 打赏
  • 举报
回复

这个收入和支出两条数据没有关联?
仅仅是:
select a.Summary [收入摘要],a.money [收入金额],b.Summary [支出摘要],b.money [支出金额]
from BankrollDayReport a,BankrollDayReport b where a.type='01' and b.type='02'

34,575

社区成员

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

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