ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

SQL绕口令

2022-07-12 17:05:33  阅读:125  来源: 互联网

标签:sum when month yearmonth 绕口令 购进 SQL 总金额


 

        select 
        yearmonth,
        type,
        name,
        last_buy_month  as 上次购进时间,
        sum(A去年购进总金额) as A去年购进总金额,
        sum(B去年购进总金额) as B去年购进总金额,
        sum(C去年购进总金额) as C去年购进总金额,
        sum(D去年购进总金额) as D去年购进总金额,
        sum(A上次购进总金额) as A上次购进总金额,
        sum(B上次购进总金额) as B上次购进总金额,
        sum(C上次购进总金额) as C上次购进总金额,
        sum(D上次购进总金额) as D上次购进总金额
        from (
        select '202206' as yearmonth,
        c.name,
        sum(case when left(yearmonth,4) = left(to_char(to_date('202206','yyyymm') - interval '12 month','yyyymm'),4) and 
        b.category_name = 'OTC-A'
        then a.amount else 0 end ) as A去年购进总金额,
        sum(case when left(yearmonth,4) = left(to_char(to_date('202206','yyyymm') - interval '12 month','yyyymm'),4) and 
        b.category_name = 'OTC-B'
        then a.amount else 0 end ) as B去年购进总金额,
        sum(case when left(yearmonth,4) = left(to_char(to_date('202206','yyyymm') - interval '12 month','yyyymm'),4) and 
        b.category_name = 'OTC-C'
        then a.amount else 0 end ) as C去年购进总金额,
        sum(case when left(yearmonth,4) = left(to_char(to_date('202206','yyyymm') - interval '12 month','yyyymm'),4) and 
        b.category_name = 'OTC-D'
        then a.amount else 0 end ) as D去年购进总金额,
        d.last_buy_month,
        sum(case when yearmonth = d.last_buy_month and b.category_name = 'OTC-A' then a.amount else 0 end ) as A上次购进总金额,
        sum(case when yearmonth = d.last_buy_month and b.category_name = 'OTC-B' then a.amount else 0 end ) as B上次购进总金额,
        sum(case when yearmonth = d.last_buy_month and b.category_name = 'OTC-C' then a.amount else 0 end ) as C上次购进总金额,
        sum(case when yearmonth = d.last_buy_month and b.category_name = 'OTC-D' then a.amount else 0 end ) as D上次购进总金额,
        case when c.level1 in ('3001','3002','6030') then '连锁'
        when a.label @> '{14}' then '总经理关注'
        when a.label @> '{16}' then '大区样板'
        when a.label @> '{15}' then '大区重点'
        when c.level1 in ('3003') then '单点'
        when c.level1 in ('4001','4002','4003','2032','2033','2086','2087') then '诊所'
        when c.level1 in ('1004') then '非协议'
        end as type,a.newdepartmentid
        from flowmonth a 
        join goods b on a.goodscode = b.code
        join client c on a.clientid = c.id
        left join (
            select 
            clientid,
            max(case when yearmonth < to_char(to_date('202206','yyyymm') - interval '2 month','yyyymm') and amount > 0 then yearmonth end ) as last_buy_month
            from flowmonth a
            where sfcx = '1'
            and exists (select clientid from flowmonth where yearmonth = '202206' and sfcx = '1' and clientid = a.clientid and (amount <= 0 or amount is null))
            and exists (select clientid from flowmonth where yearmonth = to_char(to_date('202206','yyyymm') - interval '1 month','yyyymm') and sfcx = '1' and clientid = a.clientid and (amount <= 0 or amount is null))
            and exists (select clientid from flowmonth where yearmonth = to_char(to_date('202206','yyyymm') - interval '2 month','yyyymm') and sfcx = '1' and clientid = a.clientid and (amount <= 0 or amount is null))
            GROUP BY clientid
        ) d on a.clientid = d.clientid
        where sfcx = '1'
        and b.category_name like 'OTC-%'
        and exists (select clientid from flowmonth where yearmonth = '202206' and sfcx = '1' and clientid = a.clientid and (amount <= 0 or amount is null))
        and exists (select clientid from flowmonth where yearmonth = to_char(to_date('202206','yyyymm') - interval '1 month','yyyymm') and sfcx = '1' and clientid = a.clientid and (amount <= 0 or amount is null))
        and exists (select clientid from flowmonth where yearmonth = to_char(to_date('202206','yyyymm') - interval '2 month','yyyymm') and sfcx = '1' and clientid = a.clientid and (amount <= 0 or amount is null))
        and ( c.level1 in ('3001','3002','6030','3003','3003','4001','4002','4003','2032','2033','2086','2087','1004')
        or a.label @> '{14}' or a.label @> '{15}' or a.label @> '{16}')
        GROUP BY c.name,d.last_buy_month,c.level1,a.label,a.newdepartmentid
        ) a 
        join view_person_department_otc d on a.newdepartmentid = d.departmentid
        GROUP BY yearmonth,name,last_buy_month,type;

 

标签:sum,when,month,yearmonth,绕口令,购进,SQL,总金额
来源: https://www.cnblogs.com/lrzy/p/16470716.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有