分隔栏-Oracle

时间:2018-08-02 09:35:02

标签: oracle

enter image description here

你好/我需要把这张桌子分开

我尝试使用它,但效果不好

SELECT MIN(y1),
       MAX(y1),
       MIN(redempamount1),
       MAX(redempamount1),
       MIN(f1),
       MIN(f2),
       MAX(f1),
       MAX(f2)
FROM   (SELECT to_number(y1) y1,
               redempamount1,
               add_months('02.03.2018', 12 * y1 - 12) f1,
               add_months('02.03.2018', 12 * y1) - 1 f2,
               row_number() over(PARTITION BY ntl ORDER BY rownum DESC) grp
        FROM   (SELECT to_number(t.y1) y1,
                       redempamount1,
                       add_months('02.03.2018', 12 * y1 - 12) f1,
                       add_months('02.03.2018', 12 * y1) - 1 f2,
                       ntile(2) over(ORDER BY rownum DESC) ntl
                FROM   inspolreport t))
GROUP  BY grp;

enter image description here

Y1 REDEMPAMOUNT1 F1         F2
-- ------------- ---------- ----------
 1             0 18.12.2008 17.12.2009
 2       3362.54 18.12.2009 17.12.2010
 3       6030.84 18.12.2010 17.12.2011
 4       8873.52 18.12.2011 17.12.2012
 5       11830.9 18.12.2012 17.12.2013
 6      15041.83 18.12.2013 17.12.2014
 7      18382.37 18.12.2014 17.12.2015
 8      21857.15 18.12.2015 17.12.2016
 9      25472.97 18.12.2016 17.12.2017
10      27359.51 18.12.2017 17.12.2018
11      31690.65 18.12.2018 17.12.2019
12         36195 18.12.2019 17.12.2020
13      40879.06 18.12.2020 17.12.2021
14       45750.5 18.12.2021 17.12.2022
15      53491.44 18.12.2022 17.12.2023

预期产量

Y1 REDEMPAMOUNT1 F1         F2         Y2 REDEMPAMOUNT1 F3         F4
-- ------------- ---------- ---------- -- ------------- ---------- ----------
 1             0 18.12.2008 17.12.2009  9      25472.97 18.12.2016 17.12.2017
 2       3362.54 18.12.2009 17.12.2010 10      27359.51 18.12.2017 17.12.2018
 3       6030.84 18.12.2010 17.12.2011 11      31690.65 18.12.2018 17.12.2019
 4       8873.52 18.12.2011 17.12.2012 12         36195 18.12.2019 17.12.2020
 5       11830.9 18.12.2012 17.12.2013 13      40879.06 18.12.2020 17.12.2021
 6      15041.83 18.12.2013 17.12.2014 14       45750.5 18.12.2021 17.12.2022
 7      18382.37 18.12.2014 17.12.2015 15      53491.44 18.12.2022 17.12.2023
 8      21857.15 18.12.2015 17.12.2016

1 个答案:

答案 0 :(得分:0)

如果您想要将结果分成两组并排的数据,那么您将需要相应地旋转数据。

我可以看到这就是您要尝试的操作,但是您需要使用条件聚合来生成列。

所以你想要类似的东西:

WITH your_table AS (SELECT 1 y1, 0 redempamount1, to_date('18/12/2008', 'dd/mm/yyyy') f1, to_date('17/12/2009', 'dd/mm/yyyy') f2 FROM dual UNION ALL
                    SELECT 2 y1, 3362.54 redempamount1, to_date('18/12/2009', 'dd/mm/yyyy') f1, to_date('17/12/2010', 'dd/mm/yyyy') f2 FROM dual UNION ALL
                    SELECT 3 y1, 6030.84 redempamount1, to_date('18/12/2010', 'dd/mm/yyyy') f1, to_date('17/12/2011', 'dd/mm/yyyy') f2 FROM dual UNION ALL
                    SELECT 4 y1, 8873.52 redempamount1, to_date('18/12/2011', 'dd/mm/yyyy') f1, to_date('17/12/2011', 'dd/mm/yyyy') f2 FROM dual UNION ALL
                    SELECT 5 y1, 15041.83 redempamount1, to_date('18/12/2012', 'dd/mm/yyyy') f1, to_date('17/12/2011', 'dd/mm/yyyy') f2 FROM dual UNION ALL
                    SELECT 6 y1, 15041.83 redempamount1, to_date('18/12/2013', 'dd/mm/yyyy') f1, to_date('17/12/2011', 'dd/mm/yyyy') f2 FROM dual UNION ALL
                    SELECT 7 y1, 18382.37 redempamount1, to_date('18/12/2014', 'dd/mm/yyyy') f1, to_date('17/12/2011', 'dd/mm/yyyy') f2 FROM dual UNION ALL
                    SELECT 8 y1, 21857.15 redempamount1, to_date('18/12/2015', 'dd/mm/yyyy') f1, to_date('17/12/2011', 'dd/mm/yyyy') f2 FROM dual UNION ALL
                    SELECT 9 y1, 25472.97 redempamount1, to_date('18/12/2016', 'dd/mm/yyyy') f1, to_date('17/12/2011', 'dd/mm/yyyy') f2 FROM dual UNION ALL
                    SELECT 10 y1, 27359.51 redempamount1, to_date('18/12/2017', 'dd/mm/yyyy') f1, to_date('17/12/2011', 'dd/mm/yyyy') f2 FROM dual UNION ALL
                    SELECT 11 y1, 31690.65 redempamount1, to_date('18/12/2018', 'dd/mm/yyyy') f1, to_date('17/12/2011', 'dd/mm/yyyy') f2 FROM dual UNION ALL
                    SELECT 12 y1, 36195 redempamount1, to_date('18/12/2019', 'dd/mm/yyyy') f1, to_date('17/12/2020', 'dd/mm/yyyy') f2 FROM dual UNION ALL
                    SELECT 13 y1, 40879.06 redempamount1, to_date('18/12/2020', 'dd/mm/yyyy') f1, to_date('17/12/2021', 'dd/mm/yyyy') f2 FROM dual UNION ALL
                    SELECT 14 y1, 45750.5 redempamount1, to_date('18/12/2021', 'dd/mm/yyyy') f1, to_date('17/12/2022', 'dd/mm/yyyy') f2 FROM dual UNION ALL
                    SELECT 15 y1, 53491.44 redempamount1, to_date('18/12/2022', 'dd/mm/yyyy') f1, to_date('17/12/2023', 'dd/mm/yyyy') f2 FROM dual)
SELECT MAX(CASE WHEN grp = 1 THEN y1 END) AS y1,
       MAX(CASE WHEN grp = 1 THEN redempamount1 END) AS redempamount1,
       MAX(CASE WHEN grp = 1 THEN f1 END) AS f1,
       MAX(CASE WHEN grp = 1 THEN f2 END) AS f2,
       MAX(CASE WHEN grp = 2 THEN y1 END) AS y2,
       MAX(CASE WHEN grp = 2 THEN redempamount1 END) AS redempamount2,
       MAX(CASE WHEN grp = 2 THEN f1 END) AS f3,
       MAX(CASE WHEN grp = 2 THEN f2 END) AS f4
FROM   (SELECT y1,
               redempamount1,
               f1,
               f2,
               grp,
               row_number() OVER (PARTITION BY grp ORDER BY y1) rn
        FROM   (SELECT y1,
                       redempamount1,
                       f1,
                       f2,
                       NTILE(2) OVER (ORDER BY y1) grp
                FROM   your_table))
GROUP BY rn
ORDER BY rn;

        Y1 REDEMPAMOUNT1 F1          F2                  Y2 REDEMPAMOUNT2 F3          F4
---------- ------------- ----------- ----------- ---------- ------------- ----------- -----------
         1             0 18/12/2008  17/12/2009           9      25472.97 18/12/2016  17/12/2011
         2       3362.54 18/12/2009  17/12/2010          10      27359.51 18/12/2017  17/12/2011
         3       6030.84 18/12/2010  17/12/2011          11      31690.65 18/12/2018  17/12/2011
         4       8873.52 18/12/2011  17/12/2011          12         36195 18/12/2019  17/12/2020
         5      15041.83 18/12/2012  17/12/2011          13      40879.06 18/12/2020  17/12/2021
         6      15041.83 18/12/2013  17/12/2011          14       45750.5 18/12/2021  17/12/2022
         7      18382.37 18/12/2014  17/12/2011          15      53491.44 18/12/2022  17/12/2023
         8      21857.15 18/12/2015  17/12/2011                                       

但是,如果您的体重在11g及以上,则可以使用PIVOT命令,我认为这会使您的意图更清晰:

SELECT o_y1 AS y1,
       o_redempamount1 AS redempamount1,
       o_f1 AS f1,
       o_f2 AS f2,
       t_y1 AS y2,
       t_redempamount1 AS redempamount2,
       t_f1 AS f3,
       t_f2 AS f4
FROM   (SELECT y1,
               redempamount1,
               f1,
               f2,
               grp,
               row_number() OVER (PARTITION BY grp ORDER BY y1) rn
        FROM   (SELECT y1,
                       redempamount1,
                       f1,
                       f2,
                       NTILE(2) OVER (ORDER BY y1) grp
                FROM   your_table))
PIVOT (MAX(y1) AS y1, MAX(redempamount1) AS redempamount1, MAX(f1) AS f1, MAX(f2) AS f2
       FOR grp IN (1 AS o, 2 AS t))
ORDER BY rn;