SQL数据库多表连接问题,求助!

cdsqyqwsj 2011-08-12 12:44:06
我有三个表分别为,A、B、C
A:(车的基本参数)
Id bchao
1 1号
2 2号
3 3号
4 4号

B表:(这辆车每天的收入情况,流水账记录)
Id bchao sr sj
1 1号 50 2011-07-25
2 2号 40 2011-07-21
3 1号 36 2011-08-09
4 2号 80 2011-08-11
5 2号 60 2011-08-19
C表:(记录这些车每月加油的情况)
Id bchao jy sj
1 1号 50 2011-07-08
2 3号 100 2011-07-12
3 1号 20 2011-08-09
4 2号 80 2011-08-09
现在我想得到这些车一月的投入和产出情况,这个语句应该怎么写?
希望得到结果为:
Id bchao sr jy sj
1 1号 50 50 2011-07
2 2号 40 0 2011-07
3 3号 0 100 2011-07
4 4号 0 0 2011-07
5 1号 36 20 2011-08
6 2号 140 80 2011-08
7 3号 0 0 2011-08
8 4号 0 0 2011-08
...全文
181 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
AcHerat 2011-08-12
  • 打赏
  • 举报
回复

;with ach as
(
select convert(varchar(7),sj,120) date,bchao,sum(sr) as sr,0 as jy
from b
group by convert(varchar(7),sj,120),bchao
union all
select convert(varchar(7),sj,120) date,bchao,0 as sr,sum(jy) as jy
from c
group by convert(varchar(7),sj,120),bchao
)

select a.bchao,a.date,isnull(b.sr,0) sr,isnull(b.jy,0) jy
from (select distinct a.bchao,b.date from a cross join ach b) a
left join (select date,bchao,sum(sr) sr,sum(jy) jy from ach group by date,bchao) b
on a.bchao = b.bchao and a.date = b.date
cdsqyqwsj 2011-08-12
  • 打赏
  • 举报
回复
晕,显示好像有问题,重发一次!
我有三个表分别为,A、B、C
A:(车的基本参数)
Id bchao
1 1号
2 2号
3 3号
4 4号

B表:(这辆车每天的收入情况,流水账记录)
Id bchao sr sj
1 1号 50 2011-07-25
2 2号 40 2011-07-21
3 1号 36 2011-08-09
4 2号 80 2011-08-11
5 2号 60 2011-08-19
C表:(记录这些车每月加油的情况)
Id bchao jy sj
1 1号 50 2011-07-08
2 3号 100 2011-07-12
3 1号 20 2011-08-09
4 2号 80 2011-08-09
现在我想得到这些车一月的投入和产出情况,这个语句应该怎么写?
希望得到结果为:
Id bchao sr jy sj
1 1号 50 50 2011-07
2 2号 40 0 2011-07
3 3号 0 100 2011-07
4 4号 0 0 2011-07
5 1号 36 20 2011-08
6 2号 140 80 2011-08
7 3号 0 0 2011-08
8 4号 0 0 2011-08
cdsqyqwsj 2011-08-12
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 acherat 的回复:]
引用 10 楼 cdsqyqwsj 的回复:

SQL code
select distinct a.bchao,b.date from a cross join ach b
中的cross join ach b什么意思?


cross join 交叉连接!楼主可以看看join的相关内容!


SQL code

select date,bchao,sr,jy
in……
[/Quote]
调试结果:sum or average aggregate 运算不能以 char 数据类型作为参数
cdsqyqwsj 2011-08-12
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 acherat 的回复:]
引用 10 楼 cdsqyqwsj 的回复:

SQL code
select distinct a.bchao,b.date from a cross join ach b
中的cross join ach b什么意思?


cross join 交叉连接!楼主可以看看join的相关内容!


SQL code

select date,bchao,sr,jy
in……
[/Quote]
恩,谢谢,交叉我明白,但是为什么和b交叉?
AcHerat 2011-08-12
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 cdsqyqwsj 的回复:]

SQL code
select distinct a.bchao,b.date from a cross join ach b
中的cross join ach b什么意思?
[/Quote]

cross join 交叉连接!楼主可以看看join的相关内容!


select date,bchao,sr,jy
into #tb
from
(
select convert(varchar(7),sj,120) date,bchao,sum(sr) as sr,0 as jy
from b
group by convert(varchar(7),sj,120),bchao
union all
select convert(varchar(7),sj,120) date,bchao,0 as sr,sum(jy) as jy
from c
group by convert(varchar(7),sj,120),bchao
) t

select a.bchao,a.date,isnull(b.sr,0) sr,isnull(b.jy,0) jy
from (select distinct a.bchao,b.date from a cross join #tb b) a
left join (select date,bchao,sum(sr) sr,sum(jy) jy from #tb group by date,bchao) b
on a.bchao = b.bchao and a.date = b.date

drop table #tb
cdsqyqwsj 2011-08-12
  • 打赏
  • 举报
回复
select distinct a.bchao,b.date from a cross join ach b
中的cross join ach b什么意思?
cdsqyqwsj 2011-08-12
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 gogodiy 的回复:]
数据库什么版本?
[/Quote]
server 2000
gogodiy 2011-08-12
  • 打赏
  • 举报
回复
数据库什么版本?
cdsqyqwsj 2011-08-12
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 acherat 的回复:]
SQL code

;with ach as
(
select convert(varchar(7),sj,120) date,bchao,sum(sr) as sr,0 as jy
from b
group by convert(varchar(7),sj,120),bchao
union all
select convert(varch……
[/Quote]
恩,谢谢你的解释,我用你这代码调试了,但是提示with附近有语法错误!
AcHerat 2011-08-12
  • 打赏
  • 举报
回复

;with ach as
(
select convert(varchar(7),sj,120) date,bchao,sum(sr) as sr,0 as jy
from b
group by convert(varchar(7),sj,120),bchao
union all
select convert(varchar(7),sj,120) date,bchao,0 as sr,sum(jy) as jy
from c
group by convert(varchar(7),sj,120),bchao
)
--这里是将两表按年月及车牌号进行合并,其中收入占一列,加油的占另一列,方便统计
--就是上访的sum() as ..,0 as .. 等

select a.bchao,a.date,isnull(b.sr,0) sr,isnull(b.jy,0) jy
from (select distinct a.bchao,b.date from a cross join ach b) a
-- 这个a表是为了得到所有车牌号所有年月的一个笛卡尔积,就是全排列,把参与统计的年月和车牌号全排列形成一个对照表
left join (select date,bchao,sum(sr) sr,sum(jy) jy from ach group by date,bchao) b
--别名b表对年月和车牌分组,将收入和加油的费用统计后去和对照表a进行左连接得到特定年月及车牌号的信息
on a.bchao = b.bchao and a.date = b.date
cdsqyqwsj 2011-08-12
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 cdsqyqwsj 的回复:]
引用 2 楼 acherat 的回复:
SQL code

;with ach as
(
select convert(varchar(7),sj,120) date,bchao,sum(sr) as sr,0 as jy
from b
group by convert(varchar(7),sj,120),bchao
union all
select convert(varch……
[/Quote]
有没有朋友帮解释下这段代码,谢谢了,我菜鸟!
cdsqyqwsj 2011-08-12
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 acherat 的回复:]
SQL code

;with ach as
(
select convert(varchar(7),sj,120) date,bchao,sum(sr) as sr,0 as jy
from b
group by convert(varchar(7),sj,120),bchao
union all
select convert(varch……
[/Quote]

请问这段代码是什么意思?把时间转换了?
cdsqyqwsj 2011-08-12
  • 打赏
  • 举报
回复
有没有朋友还有其他方式?感觉好难!
cdsqyqwsj 2011-08-12
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 zailushang_ 的回复:]
引用 13 楼 cdsqyqwsj 的回复:
引用 11 楼 acherat 的回复:
引用 10 楼 cdsqyqwsj 的回复:

SQL code
select distinct a.bchao,b.date from a cross join ach b
中的cross join ach b什么意思?


cross join 交叉连接!楼主可以看看join的相关内容!
……
[/Quote]
是decimal
zailushang_ 2011-08-12
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 cdsqyqwsj 的回复:]
引用 11 楼 acherat 的回复:
引用 10 楼 cdsqyqwsj 的回复:

SQL code
select distinct a.bchao,b.date from a cross join ach b
中的cross join ach b什么意思?


cross join 交叉连接!楼主可以看看join的相关内容!


SQL code

select ……
[/Quote]
sr和jy字段是int类型吗

22,209

社区成员

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

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