mysql多数据表查询统计报表有什么方法可以简化

mysql多数据表查询统计报表有什么方法可以简化,第1张

select 

 (select u._netName from t_gw_netuser u where u._netName = a._uid) _netName,  

 (select w._urlName from t_gw_websiteurl w where w._urlId = w._url) _urlName,

 count(distinct a._url) CUrl,

 count(a._host) CHost,

 sum(a._count) SCount,

 SUl,

 SDl,

 a._logtime 

from t_gw_merge_log a join 

 (select sum(p._ul) SUl,sum(p._dl) SDl,p._appId from t_gw_applog p group by p._appId) x on a._app=x._appId 

group by a._uid,a._url

order by _urlName

考虑下这个SQL

select date_format(date,'%Y-%m-%d') as day, count(case when xinghao='a' then 1 end) as 'A', count(case when xinghao='b' then 1 end) as 'B', count(case when xinghao='c' then 1 end) as 'C' from table group by day.

我没有测试,在这里count也可以用sum代替

创建表,数据

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')

执行:

SELECT

a.项目号,

a.总金额,

sum(

CASE

WHEN a.项目号 = b.项目号 THEN

到款

ELSE

0

END

) 到款总额

FROM

a

LEFT JOIN b ON a.项目号 = b.项目号

GROUP BY

a.项目号,

a.总金额

结果:


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/7539318.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-06
下一篇 2023-04-06

发表评论

登录后才能评论

评论列表(0条)

保存