创建表,数据
create table a(id int,
项目号 varchar(2),
总金额 int,
上家单位 varchar(4))
insert into a values (1,'01',100,'AAAA')
insert into a values (2,'02',200,'BBBB')
insert into a values (3,'03',250,'CCCC')
create table b
(id int,
项目号 varchar(2),
到款 int,
到款时间 date)
insert into b values (1,'03',50,'2018-07-23')
insert into b values (2,'03',100,'2018-07-24')
insert into b values (3,'01',80,'2018-07-25')
执行:
SELECTa.项目号,
a.总金额,
sum(
CASE
WHEN a.项目号 = b.项目号 THEN
到款
ELSE
0
END
) 到款总额
FROM
a
LEFT JOIN b ON a.项目号 = b.项目号
GROUP BY
a.项目号,
a.总金额
结果:
SELECT m.user_id, b_total, c_total, d_total FROM a m, (SELECT user_id, COUNT(*) b_total FROM b GROUP BY user_id) n,(SELECT user_id, COUNT(*) c_total FROM c GROUP BY user_id) j, (SELECT user_id, COUNT(*) d_total FROM d GROUP BY user_id) k,
WHERE m.user_id = n.user_id AND m.user_id = j.user_id AND m.user_id = k.user_id
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)