select a.time,sum(regTime) regSum,sum(logTime) logSum from
(select regTime time,1 regTime , 0 logSum from user union all select logTime time,0 regTime , 1 logSum from login) a group by a.time order by a.time
------------------
select a.time,ifnull(b.regSum,0) regSum,ifnull(c.loginSum,0) loginSum from
(select regtime time from user union select logintime time from lgoin) a
left join (select regTime time,count(*) regSum from user group by regTime) b
on (a.time=b.time)
left join (select loginTime time,count(*) logSum from login group by loginTime) c
on (a.time=c.time)
order by a.time
创建表,数据
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.总金额
结果:
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)