博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ORACLE
阅读量:5103 次
发布时间:2019-06-13

本文共 2296 字,大约阅读时间需要 7 分钟。

WITH date_info

     AS (SELECT TO_DATE ('2017-03-07', 'yyyy-mm-dd') AS start_date,
                  TO_DATE ('2017-03-14', 'yyyy-mm-dd')
                - TO_DATE ('2017-03-07', 'yyyy-mm-dd')
                + 1
                   AS dt
           FROM DUAL),
     date_group
     AS (    SELECT start_date + (LEVEL - 1) AS start_date,
                    start_date + LEVEL AS end_date
               FROM date_info
         CONNECT BY LEVEL <= date_info.dt),
     data_info
     AS (SELECT *
           FROM (SELECT t3.factory,
                        t3.client,
                        t3.real_value,
                        NVL (t4.target_value, 0) target_value,
                        t3.create_time
                   FROM (  SELECT t.factory,
                                  t.client,
                                  SUM (t.real_value) real_value,
                                  t.create_time
                             FROM (SELECT factory,
                                          client,
                                          ROUND (
                                             DECODE (person,
                                                     0, 0,
                                                     product_qty / person),
                                             2)
                                             real_value,
                                          reamark,
                                          create_time
                                     FROM D9DATA_TABLE_DAY_UPPD) t
                                  LEFT JOIN D9DATA_TABLE_MONTH_TARGET t2
                                     ON     t.factory = t2.factory
                                        AND t.client = t2.client
                                        AND TO_CHAR (t.create_time, 'yyyy-mm') =
                                               TO_CHAR (
                                                  TO_DATE (
                                                        t2.year_info
                                                     || '-'
                                                     || t2.month_info,
                                                     'yyyy-mm'),
                                                  'yyyy-mm')
                         GROUP BY t.factory,
                                  t.client,
                                  real_value,
                                  t.create_time
                         ORDER BY t.factory, t.client, t.create_time) t3
                        LEFT JOIN D9DATA_TABLE_MONTH_TARGET t4
                           ON     t4.factory = t3.factory
                              AND t4.client = t3.client
                              AND TO_CHAR (
                                     TO_DATE (
                                        t4.year_info || '-' || t4.month_info,
                                        'yyyy-mm'),
                                     'yyyy-mm') =
                                     TO_CHAR (t3.create_time, 'yyyy-mm')) t5)
  SELECT t5.start_date,
         DECODE (t5.factory,
                 1, '比亚迪第九事业部',
                 6, '第六工厂',
                 3, '第一工厂',
                 5, '第五工厂',
                 4, '第二工厂',
                 49, '第七工厂',
                 48, '第三工厂')
            factory,
         t5.client,
         NVL (t5.target_value, 0),
         NVL (t5.target_value1, 0),
         NVL (t5.real_value, 0),
         NVL (t5.balance, 0),
         t5.rate
    FROM (SELECT t.start_date,
                 NVL (t1.factory, t.factory) factory,
                 NVL (t1.client, t.client) client,
                 t1.target_value,
                 t1.target_value target_value1,
                 t1.real_value,
                 t1.real_value - t1.target_value balance,
                 DECODE (
                    NVL (t2.real_value, 0),
                    0, 0,
                    TO_CHAR (
                       ROUND (
                            (NVL (t1.real_value, 0) - NVL (t2.real_value, 0))
                          / NVL (t2.real_value, 0)
                          * 100,
                          2),
                       'fm99999999990.00'))
                    rate
            FROM (SELECT t3.start_date, t4.factory, t4.client
                    FROM date_group t3,
                         (SELECT DISTINCT factory, client FROM data_info) t4) t
                 LEFT JOIN data_info t1
                    ON     t.start_date = t1.create_time
                       AND t.factory = t1.factory
                       AND t.client = t1.client
                 LEFT JOIN data_info t2
                    ON     t.start_date - 1 = t2.create_time
                       AND t1.factory = t2.factory
                       AND t1.client = t2.client) t5
   WHERE 1 = 1
ORDER BY t5.factory, t5.client, t5.start_date
 

转载于:https://www.cnblogs.com/qq1988627/p/6606848.html

你可能感兴趣的文章
前端监控
查看>>
centos6.5 mysql忘记登入密码
查看>>
Trusted Execution Technology (TXT) --- 启动控制策略(LCP)篇
查看>>
clipboard.js使用方法
查看>>
绘图库:Matplotlib
查看>>
0906第一次作业
查看>>
Ceph Monitor基础架构与模块详解
查看>>
dbca:Exception in thread "main" java.lang.UnsatisfiedLinkError: get
查看>>
hdu 1232 畅通工程(并查集)
查看>>
移动开发平台-应用之星app制作教程
查看>>
jquery validate使用笔记
查看>>
主要的几个脑网络——整理自eegfmri的博客
查看>>
leetcode 459. 重复的子字符串(Repeated Substring Pattern)
查看>>
CABasicAnimation animationWithKeyPath Types
查看>>
JavaScript--eval
查看>>
iOS6与iOS7屏幕适配技巧
查看>>
获取视图尺寸大小方法
查看>>
mysql 历史记录查询
查看>>
sqoop连接Oracle数据库错误异常
查看>>
伪类与超链接
查看>>