美文网首页
oracle聚合查询行转列

oracle聚合查询行转列

作者: 水表学Java | 来源:发表于2019-10-22 19:32 被阅读0次

    表为消费流水表

    关键字段
    mdid,viptype,je,jzrq

    需求统计按各门店,各月份,所有会员以及三种不同会员卡会员的消费笔数、消费笔单价
    with temp as(
    SELECT '全部会员' as viptype,a.mdid,"TO_CHAR"(a.JZRQ, 'yyyy-MM') as yf,count() as hyxfbs,cast(sum(a.je)/count() as number(18,2)) as hyxfbdj
    FROM BFCRM8.HYXFJL a
    group by a.mdid,"TO_CHAR"(a.JZRQ, 'yyyy-MM')
    UNION
    SELECT '积分卡会员' as viptype,a.mdid,"TO_CHAR"(a.JZRQ, 'yyyy-MM') as yf,count() as hyxfbs,cast(sum(a.je)/count() as number(18,2)) as hyxfbdj
    FROM BFCRM8.HYXFJL a
    where VIPTYPE=101
    group by a.mdid,"TO_CHAR"(a.JZRQ, 'yyyy-MM')
    UNION
    SELECT '金卡会员' as viptype,a.mdid,"TO_CHAR"(a.JZRQ, 'yyyy-MM') as yf,count() as hyxfbs,cast(sum(a.je)/count() as number(18,2)) as hyxfbdj
    FROM BFCRM8.HYXFJL a
    where VIPTYPE=108
    group by a.mdid,"TO_CHAR"(a.JZRQ, 'yyyy-MM')
    UNION
    SELECT '钻石卡会员' as viptype,a.mdid,"TO_CHAR"(a.JZRQ, 'yyyy-MM') as yf,count() as hyxfbs,cast(sum(a.je)/count() as number(18,2)) as hyxfbdj
    FROM BFCRM8.HYXFJL a
    where VIPTYPE=107
    group by a.mdid,"TO_CHAR"(a.JZRQ, 'yyyy-MM')
    )
    select
    mdid,
    yf,
    max(decode(viptype,'全部会员',hyxfbs,'')) as 全部会员,
    max(decode(viptype,'积分卡会员',hyxfbs,'')) as 积分卡会员,
    max(decode(viptype,'金卡会员',hyxfbs,'')) as 金卡会员,
    max(decode(viptype,'钻石卡会员',hyxfbs,'')) as 钻石卡会员,

    max(decode(viptype,'全部会员笔单价',hyxfbdj,'')) as 全部会员笔单价,
    max(decode(viptype,'积分卡会员笔单价',hyxfbdj,'')) as 积分卡会员笔单价,
    max(decode(viptype,'金卡会员笔单价',hyxfbdj,'')) as 金卡会员笔单价,
    max(decode(viptype,'钻石卡会员笔单价',hyxfbdj,'')) as 钻石卡会员笔单价
    from temp
    group by mdid,yf
    order by mdid,yf

    相关文章

      网友评论

          本文标题:oracle聚合查询行转列

          本文链接:https://www.haomeiwen.com/subject/jzrcvctx.html