南宫文凯
2017-08-10 02:02
采纳率: 40%
浏览 1.7k

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 表的关联
//和 某年这个月,同比数

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

4条回答 默认 最新

相关推荐 更多相似问题