通过设置变量在子查询和父查询之间共享参数-----------------SQLServer

create table biz_recovery_report
(
    id          bigint identity primary key,
    version     bigint      not null,
    rec_id      varchar(50) not null,
    del_flag    varchar(2),
    insert_id   varchar(50),
    insert_time datetime2,
    month       varchar(50),
    recovery    numeric(19, 2),
    sale        numeric(19, 2),
    update_id   varchar(50),
    update_time datetime2,
    year        varchar(50),
    bu          varchar(50),
    slrable     numeric(19, 2)
)
go
                            declare @year int=?
                            declare @bu varchar(10) =?
                            select  (sum(pp.mustRec)/24)/(select sum(a.sale) from biz_recovery_report a where a.year=@year and a.bu=@bu)*360 *
                                    (select sum(p.sale)/( select sum(p1.sale)  from biz_recovery_report p1 where   p1.bu=@bu  ) from biz_recovery_report p  where  p.year=@year  and p.bu=@bu  ) as rs,
                                    (select  distinct p2.dic_ch_name from sys_dic as  p2 where p2.dic_value=CAST(@bu as varchar(10)) and  p2.dic_type='9000' and p2.dele_flag=0) as bu
                            from (
                                       select
                                               m1.mustRec  as  mustRec,
                                              m1.bu
                                       from (
                                             select   sum(slrable) as mustRec,max(bu)  bu
                                             from biz_recovery_report
                                             where bu=@bu and  (year=@year-1 or year=@year ) and month=12
                                             group by year,month
                                            ) as m1
                                       union all
                                       select  m2.mustRec mustRec ,m2.bu
                                       from (
                                               select   sum(slrable)*2 as mustRec,max(bu)  bu
                                               from biz_recovery_report
                                               where bu=@bu and year=@year and month>=1 and  month<=11
                                               group by year,month
                                           ) as m2
                            ) pp
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值