50分求助,SQL三表累加问题
TableADate2012-02-012012-02-022012-02-032012-02-04TableBdate差旅费午餐费2012-02-01100502012...
TableA
Date
2012-02-01
2012-02-02
2012-02-03
2012-02-04
TableB
date 差旅费 午餐费
2012-02-01 100 50
2012-02-01 200 200
2012-02-04 300 100
TableC
Date 通讯费 福利费
2012-02-01 150 0
2012-02-03 0 200
2012-02-01 0 100
想累加之后输出成:
Date 差旅费 误餐费 通讯费 福利费
2012-02-01 300 250 150 0
2012-02-02 0 0 0 0
2012-02-03 0 0 0 300
2012-02-04 300 100 0 0
希望高手帮忙,万分感谢~~~ 展开
Date
2012-02-01
2012-02-02
2012-02-03
2012-02-04
TableB
date 差旅费 午餐费
2012-02-01 100 50
2012-02-01 200 200
2012-02-04 300 100
TableC
Date 通讯费 福利费
2012-02-01 150 0
2012-02-03 0 200
2012-02-01 0 100
想累加之后输出成:
Date 差旅费 误餐费 通讯费 福利费
2012-02-01 300 250 150 0
2012-02-02 0 0 0 0
2012-02-03 0 0 0 300
2012-02-04 300 100 0 0
希望高手帮忙,万分感谢~~~ 展开
6个回答
展开全部
--下面是我通过临时表模拟了你的数据 最后面就是查询的语句其实很简单就用连个左连接就OK了!
DECLARE @TableA TABLE(date datetime)
DECLARE @TableB TABLE(date datetime,差旅费 money,午餐费 money)
DECLARE @TableC TABLE(date datetime,通讯费 money,福利费 money)
INSERT INTO @TableA(date) values('2012-02-01')
INSERT INTO @TableA(date) values('2012-02-02')
INSERT INTO @TableA(date) values('2012-02-03')
INSERT INTO @TableA(date) values('2012-02-04')
INSERT INTO @TableB(date,差旅费,午餐费) values('2012-02-01',100,50)
INSERT INTO @TableB(date,差旅费,午餐费) values('2012-02-01',200,200)
INSERT INTO @TableB(date,差旅费,午餐费) values('2012-02-04',300,100)
INSERT INTO @TableC(date,通讯费,福利费) values('2012-02-01',150,0)
INSERT INTO @TableC(date,通讯费,福利费) values('2012-02-03',0,200)
INSERT INTO @TableC(date,通讯费,福利费) values('2012-02-01',0,100)
SELECT a.date,isnull(b.差旅费,0) as 差旅费,isnull(b.午餐费,0) as 午餐费,
isnull(c.通讯费,0) AS 通讯费,isnull(c.福利费,0) AS 福利费
FROM @TableA a
LEFT JOIN (SELECT date,SUM(isnull(差旅费,0)) as 差旅费,SUM(isnull(午餐费,0)) as 午餐费
FROM @TableB GROUP BY date) b on a.date=b.date
LEFT JOIN (SELECT date,SUM(isnull(通讯费,0)) AS 通讯费,SUM(isnull(福利费,0)) AS 福利费
FROM @TableC GROUP BY date) c on a.date=c.date
DECLARE @TableA TABLE(date datetime)
DECLARE @TableB TABLE(date datetime,差旅费 money,午餐费 money)
DECLARE @TableC TABLE(date datetime,通讯费 money,福利费 money)
INSERT INTO @TableA(date) values('2012-02-01')
INSERT INTO @TableA(date) values('2012-02-02')
INSERT INTO @TableA(date) values('2012-02-03')
INSERT INTO @TableA(date) values('2012-02-04')
INSERT INTO @TableB(date,差旅费,午餐费) values('2012-02-01',100,50)
INSERT INTO @TableB(date,差旅费,午餐费) values('2012-02-01',200,200)
INSERT INTO @TableB(date,差旅费,午餐费) values('2012-02-04',300,100)
INSERT INTO @TableC(date,通讯费,福利费) values('2012-02-01',150,0)
INSERT INTO @TableC(date,通讯费,福利费) values('2012-02-03',0,200)
INSERT INTO @TableC(date,通讯费,福利费) values('2012-02-01',0,100)
SELECT a.date,isnull(b.差旅费,0) as 差旅费,isnull(b.午餐费,0) as 午餐费,
isnull(c.通讯费,0) AS 通讯费,isnull(c.福利费,0) AS 福利费
FROM @TableA a
LEFT JOIN (SELECT date,SUM(isnull(差旅费,0)) as 差旅费,SUM(isnull(午餐费,0)) as 午餐费
FROM @TableB GROUP BY date) b on a.date=b.date
LEFT JOIN (SELECT date,SUM(isnull(通讯费,0)) AS 通讯费,SUM(isnull(福利费,0)) AS 福利费
FROM @TableC GROUP BY date) c on a.date=c.date
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
SELECT TableA.Date,
Sum(CASE WHEN TableB.差旅费 IS NULL THEN 0 ELSE TableB.差旅费 END),
Sum(CASE WHEN TableB.午餐费 IS NULL THEN 0 ELSE TableB.午餐费 END),
Sum(CASE WHEN TableC.通讯费 IS NULL THEN 0 ELSE TableC.通讯费 END),
Sum(CASE WHEN TableC.福利费 IS NULL THEN 0 ELSE TableC.福利费 END)
FROM TableA
left join TableB on TableA.Date = TableB.Date
LEFT JOIN TableC on TableA.Date= TableC.Date
group by TableA.Date
Sum(CASE WHEN TableB.差旅费 IS NULL THEN 0 ELSE TableB.差旅费 END),
Sum(CASE WHEN TableB.午餐费 IS NULL THEN 0 ELSE TableB.午餐费 END),
Sum(CASE WHEN TableC.通讯费 IS NULL THEN 0 ELSE TableC.通讯费 END),
Sum(CASE WHEN TableC.福利费 IS NULL THEN 0 ELSE TableC.福利费 END)
FROM TableA
left join TableB on TableA.Date = TableB.Date
LEFT JOIN TableC on TableA.Date= TableC.Date
group by TableA.Date
更多追问追答
追问
呵呵,上次就是麻烦你帮我解决的,我这样sum好像加了两遍,我实际语句是
sum(case
when isnull(cast(co1.new_chuchaishinei as decimal(10,2)),0)
+isnull(cast(co2.new_richanginside as decimal(10,2)),0) = 0.00 then null
else
isnull(cast(co1.new_chuchaishinei as decimal(10,2)),0)
+isnull(cast(co2.new_richanginside as decimal(10,2)),0) end) '市内公交,出通行,停车费'
这样sum之后,发现会加两遍。。。
追答
我看看你的代码先,知道原因了,两次left join造成的,小改动下就可以了
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
select
a.[Date],
isnull(b.差旅费,0) as 差旅费,
isnull(b.误餐费,0) as 误餐费,
isnull(c.通讯费,0) as 通讯费,
isnull(c.福利费,0) as 福利费
from TableA a
left join TableB b on a.[date]=b.[date]
left join TableC c on a.[date]=c.[date]
a.[Date],
isnull(b.差旅费,0) as 差旅费,
isnull(b.误餐费,0) as 误餐费,
isnull(c.通讯费,0) as 通讯费,
isnull(c.福利费,0) as 福利费
from TableA a
left join TableB b on a.[date]=b.[date]
left join TableC c on a.[date]=c.[date]
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
select distinct A.Date,NVL(B.差旅费,0),NVL(B.福利费,0),NVL(C.通讯费,0),NVL(C.福利费,0) from TableA A
left join
(select Date,sum(差旅费) 差旅费,sum(福利费) 福利费 from TableB group by Date) B
on A.Date = B.Date
left join
(select Date,sum(通讯费) 通讯费,sum(福利费) 福利费 from TableC group by Date) C
on A.Date = C.Date
left join
(select Date,sum(差旅费) 差旅费,sum(福利费) 福利费 from TableB group by Date) B
on A.Date = B.Date
left join
(select Date,sum(通讯费) 通讯费,sum(福利费) 福利费 from TableC group by Date) C
on A.Date = C.Date
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
有个麻烦的方法。
B 单独分组 C单独分组 然后A BC 在Inner join 这样可以不可以
select sum(通讯费 ),sum(福利费) from TableC group by date
select sum( 差旅费 ),sum(午餐费) from TableB group by date
这2个表 然后inner join
B 单独分组 C单独分组 然后A BC 在Inner join 这样可以不可以
select sum(通讯费 ),sum(福利费) from TableC group by date
select sum( 差旅费 ),sum(午餐费) from TableB group by date
这2个表 然后inner join
追问
select
........
sum.....
from @t as Temp
left join (select
createdbyname,
new_date
sum(new_chuchaijintie)
from filterednew_chuchaidetailfee where createdbyname = 'a' and new_moneytypename= 'cny'
group by
createdbyname,
new_date
)as co1 on left(convert(nvarchar(20),dateadd(hour,8,co1.new_date),120),10) = temp.date
left join....
这样之后,在第一个select下做sum是不行的
参考资料: c
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询