记录一个SQL语句
有一个SQL表(throughput)是各部门的产量表,其内容如下所示:
date(日期),section(部门),product_name,product_qty,pre_ratio(预计的比例)
20170101 ,total ,mobile ,8000 ,1 (该天总共生产了8000件mobile,因为该行是总产量,ratio为无效字段,故比例为1)
20170101 ,total ,laptop ,1000 ,1 (该天总共生产了1000件laptop)
20170101 ,team1 ,mobile ,900 ,0.15(该天team1生产了120件laptop,预计占总产量的0.15)
20170101 ,team1 ,laptop ,120 ,0.1 (该天team1生产了120件laptop,预计占总产量的0.1)
20170102 ,total ,mobile ,7400 ,1 (该天总共生产了7400件mobile)
20170102 ,total ,laptop ,1200 ,1 (该天总共生产了1200件laptop)
20170102 ,team1 ,mobile ,960 ,0.15(该天team1生产了960件laptop,预计占总产量的0.15)
20170102 ,team1 ,laptop ,110 ,0.1 (该天team1生产了110件laptop,预计占总产量的0.1)
......(还有其他日期,还有team2等部门,还有DesktopPC等产品)......
需求用SQL统计: 从20160701开始,每天,team1的产量占总产量的实际比例:
答:
SELECT sub.date,sub.product_name,sub.qty,full.qty,sub.qty/full.qty,sub.pre_ratio
FROM throughput sub INNER JOIN throughput full
ON sub.date=full.date AND sub.product_name=full.product_name
WHERE sub.section='team1' AND full.section='total' AND date>=20160701;
完。