表联立的问题

StillMiss 2011-08-03 03:33:14
各位好:我需要做一个关于餐厅模块的报表 效果如下图:



有两个表,一个存款的,一个离职的时候退款的


餐卡存款表:h_din_pre_money

工号 存款 存款时间 备注 编号(自增列)
emp_id pre_money pre_date remark id

010001 100.00 2011-07-11 10:15:38.000 1
010001 10.00 2011-07-11 11:44:52.517 2
010001 100.00 2011-07-12 10:09:26.063 NULL 4
101781 100.00 2011-07-28 11:19:24.327 NULL 5

餐卡退款表:h_din_tk_money 离职时,需要做退款处理

工号 退款 退款时间 备注 编号(自增列)
emp_id tk_money tk_date remark id

010001 110.00 2011-07-11 11:47:49.690 NULL 1



我的sql语句如下:

select
'收入时间' = convert(char(10),h_din_pre_money.pre_date,120) ,
'摘要' = convert(varchar(8000),h_din_pre_money.remark),
'收入' = sum(case when h_din_pre_money.pre_money>= 0 then h_din_pre_money.pre_money else 0 end) ,
'支出' =SUM (case when h_din_tk_money.tk_money >=0 then h_din_tk_money.tk_money else 0 end),
'余额' = sum(case when h_din_pre_money.pre_money>= 0 then h_din_pre_money.pre_money else 0 end)-SUM (case when h_din_tk_money.tk_money >=0 then h_din_tk_money.tk_money else 0 end)
from h_din_pre_money left outer join h_din_tk_money
on
h_din_pre_money.emp_id is not null
and h_din_pre_money.emp_id <>''
and h_din_tk_money.emp_id is not null
and h_din_tk_money.emp_id <>''
and h_din_pre_money.emp_id = h_din_tk_money.emp_id


group by convert(char(10),h_din_pre_money.pre_date,120),
convert(varchar(8000),h_din_pre_money.remark)



发现的问题是,
因为是做关联,左边 2011-7-11有两条数据,所以,支出这个列会出现220元,其实当天只有支出110
在之前这一天是3条数据的,当时的支出是显示330元, 就是实际数据的3倍。

我不知道怎么解决这个问题,希望大家帮我指正下。


...全文
115 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
StillMiss 2011-08-03
  • 打赏
  • 举报
回复
lsd的刷出来结果是正确的了 我看看语句。没问题就给结贴了。
AcHerat 2011-08-03
  • 打赏
  • 举报
回复

select
'收入时间' = convert(char(10),h_din_pre_money.pre_date,120) ,
'摘要' = convert(varchar(8000),h_din_pre_money.remark),
'收入' = sum(case when h_din_pre_money.pre_money>= 0 then h_din_pre_money.pre_money else 0 end) ,
'支出' =h_din_tk_money.tk_money,
'余额' = sum(case when h_din_pre_money.pre_money>= 0 then h_din_pre_money.pre_money else 0 end)
-h_din_tk_money.tk_money
from h_din_pre_money left outer join h_din_tk_money
on
h_din_pre_money.emp_id is not null
and h_din_pre_money.emp_id <>''
and h_din_tk_money.emp_id is not null
and h_din_tk_money.emp_id <>''
and h_din_pre_money.emp_id = h_din_tk_money.emp_id
where h_din_tk_money.tk_money > 0
group by convert(char(10),h_din_pre_money.pre_date,120),
convert(varchar(8000),h_din_pre_money.remark),
h_din_tk_money.tk_money
StillMiss 2011-08-03
  • 打赏
  • 举报
回复
还是一样的额 。。。
快溜 2011-08-03
  • 打赏
  • 举报
回复
select 
'收入时间' = convert(char(10),h_din_pre_money.pre_date,120) ,
'摘要' = convert(varchar(8000),h_din_pre_money.remark),
'收入' = sum(case when h_din_pre_money.pre_money>= 0 then h_din_pre_money.pre_money else 0 end) ,
'支出' =SUM (case when h_din_tk_money.tk_money >=0 then h_din_tk_money.tk_money else 0 end),
'余额' = sum(case when h_din_pre_money.pre_money>= 0 then h_din_pre_money.pre_money else 0 end)-SUM (case when h_din_tk_money.tk_money >=0 then h_din_tk_money.tk_money else 0 end)
from h_din_pre_money left outer join h_din_tk_money
on h_din_pre_money.emp_id = h_din_tk_money.emp_id

where h_din_pre_money.emp_id is not null
and h_din_pre_money.emp_id <>''
and h_din_tk_money.emp_id is not null
and h_din_tk_money.emp_id <>''

group by convert(char(10),h_din_pre_money.pre_date,120),
convert(varchar(8000),h_din_pre_money.remark)

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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