SQL多表关联嵌套子查询同比环比

select to_date('2017-09-01', 'yyyy-mm-dd') timin,
( select count(d.bsc580)
from sc51 b, sc52 c, sc58 d, sc27 e, sc05 f, sc01 g
where c.bsc510 = b.bsc510
and c.bsc520 = d.bsc520
and d.bsc211 = e.bsc211
and e.csc013 = f.bsc010
and e.csc014 = g.bsc001
and c.aae030 >= to_date('2017-08-01', 'yyyy-mm-dd')
and c.aae031 <= to_date('2017-09-01', 'yyyy-mm-dd')
and g.bsc001 = '10000308'
group by g.aab300,
g.bsc001 - (select count(d.bsc580)
from sc51 b, sc52 c, sc58 d, sc27 e, sc05 f, sc01 g
where c.bsc510 = b.bsc510
and c.bsc520 = d.bsc520
and d.bsc211 = e.bsc211
and e.csc013 = f.bsc010
and e.csc014 = g.bsc001
and c.aae030 >= to_date('2017-07-01', 'yyyy-mm-dd')
and c.aae031 <= to_date('2017-08-01', 'yyyy-mm-dd')
and g.bsc001 = '10000308'
group by g.aab300,
g.bsc001)/( select count(d.bsc580)
from sc51 b, sc52 c, sc58 d, sc27 e, sc05 f, sc01 g
where c.bsc510 = b.bsc510
and c.bsc520 = d.bsc520
and d.bsc211 = e.bsc211
and e.csc013 = f.bsc010
and e.csc014 = g.bsc001
and c.aae030 >= to_date('2017-07-01', 'yyyy-mm-dd')
and c.aae031 <= to_date('2017-08-01', 'yyyy-mm-dd')
and g.bsc001 = '10000308'
group by g.aab300,
g.bsc001)*100 tbs
from sc51 b, sc52 c, sc58 d, sc27 e, sc05 f, sc01 g
where c.bsc510 = b.bsc510
and c.bsc520 = d.bsc520
and d.bsc211 = e.bsc211
and e.csc013 = f.bsc010
and e.csc014 = g.bsc001
and c.aae030 >= to_date('2017-08-01', 'yyyy-mm-dd')
and c.aae031 <= to_date('2017-09-01', 'yyyy-mm-dd')
and g.bsc001 = '10000308'
group by g.aab300,g.bsc001
这里为了演示效果直接将时间写出,实际是传入一个时间段比如2017-02-01到2017-10-01,要求是如下公式
select 年 月,(这个月数量 - 上个月数量) / 上个月数量 from 表的关联
//也就是求出 ,某年这个月,环比数
select 年 月,(这个月数量 - 去年这个月数量) / 去年这个月数量 from 表的关联
//和 某年这个月,同比数

select to_date('2017-09-01', 'yyyy-mm-dd') timin,
( select count(d.bsc580)
from sc51 b, sc52 c, sc58 d, sc27 e, sc05 f, sc01 g
where c.bsc510 = b.bsc510
and c.bsc520 = d.bsc520
and d.bsc211 = e.bsc211
and e.csc013 = f.bsc010
and e.csc014 = g.bsc001
and c.aae030 >= to_date('2017-08-01', 'yyyy-mm-dd')
and c.aae031 <= to_date('2017-09-01', 'yyyy-mm-dd')
and g.bsc001 = '10000308'
group by g.aab300,
g.bsc001 - (select count(d.bsc580)
from sc51 b, sc52 c, sc58 d, sc27 e, sc05 f, sc01 g
where c.bsc510 = b.bsc510
and c.bsc520 = d.bsc520
and d.bsc211 = e.bsc211
and e.csc013 = f.bsc010
and e.csc014 = g.bsc001
and c.aae030 >= to_date('2017-07-01', 'yyyy-mm-dd')
and c.aae031 <= to_date('2017-08-01', 'yyyy-mm-dd')
and g.bsc001 = '10000308'
group by g.aab300,
g.bsc001)/( select count(d.bsc580)
from sc51 b, sc52 c, sc58 d, sc27 e, sc05 f, sc01 g
where c.bsc510 = b.bsc510
and c.bsc520 = d.bsc520
and d.bsc211 = e.bsc211
and e.csc013 = f.bsc010
and e.csc014 = g.bsc001
and c.aae030 >= to_date('2017-07-01', 'yyyy-mm-dd')
and c.aae031 <= to_date('2017-08-01', 'yyyy-mm-dd')
and g.bsc001 = '10000308'
group by g.aab300,
g.bsc001)*100 tbs
from sc51 b, sc52 c, sc58 d, sc27 e, sc05 f, sc01 g
where c.bsc510 = b.bsc510
and c.bsc520 = d.bsc520
and d.bsc211 = e.bsc211
and e.csc013 = f.bsc010
and e.csc014 = g.bsc001
and c.aae030 >= to_date('2017-08-01', 'yyyy-mm-dd')
and c.aae031 <= to_date('2017-09-01', 'yyyy-mm-dd')
and g.bsc001 = '10000308'
group by g.aab300,g.bsc001

http://blog.sina.com.cn/s/blog_5cef6d430100krx7.html

最近我也在写同比环比数据查询,同比就是去年同一周期查询数据与之现在的数据对比,但这个日期都是java实现的,而且这些逻辑在spl太复杂了,我也是在java中计算的

你分别查出本月,上月和同期,UNION到一起,再按照公式decode不就OK了