0

我需要以以下方式提取数据。

progress_CMC_status    progress_cmc_status_numbers    cmc_status_numbers
Mature Draft                         2                         5
Review                               0                         3
Final Draft                          0                         3
Final Comment                        0                         3
Final Document                       2                         3
Archived Document                    1                         1

但我得到不同的输出

progress_CMC_status    progress_cmc_status_numbers    cmc_status_numbers
Mature Draft                         2                         5
Review                               0                         0
Final Draft                          0                         0
Final Comment                        0                         0
Final Document                       2                         3
Archived Document                    1                         1

尝试了以下代码:

  WITH sta AS
  (SELECT 'Mature' "status1"
  FROM DUAL
  UNION
  SELECT 'Review'
    FROM DUAL
  UNION
  SELECT 'Final Draft'
    FROM DUAL
  UNION
  SELECT 'Final Comment'
    FROM DUAL
  UNION
  SELECT 'Final Document'
    FROM DUAL
  UNION
  SELECT 'Archived'
    FROM DUAL)
  SELECT   DECODE ("stat",
             'Mature', 'Mature Draft',
             'Review', 'Review',
             'Final Draft', 'Final Draft',
             'Final Comment', 'Final Comment',
             'Final Document', 'Final Document',
             'Archived', 'Archived Document'
            ) AS "progress_CMC_status",
  NVL
        (TO_CHAR ("progress_cmc_status_numbers"),
         0
        ) AS "progress_cmc_status_numbers",
  NVL (TO_CHAR ("cmc_status_numbers"), 0) AS "cmc_status_numbers"
  FROM (SELECT "status1" AS "stat",
             NVL
                (TO_CHAR (NULL),
                 "progress_cmc_status_numbers"
                ) AS "progress_cmc_status_numbers",
             NVL (TO_CHAR (NULL),
                  "cmc_status_numbers"
                 ) AS "cmc_status_numbers"
   FROM sta
   LEFT JOIN
   (SELECT   VALUE AS "progress_CMC_status",
                       COUNT (*) AS "progress_cmc_status_numbers",
                       SUM (COUNT (*)) OVER (ORDER BY DECODE
                                                        (VALUE,
                                                         'Mature', 0,
                                                         'Review', 1,
                                                         'Final Draft', 2,
                                                         'Final Comment', 3,
                                                         'Final Document', 4,
                                                         'Archived', 5,
                                                         6
                                                        ) DESC ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
                                                  AS "cmc_status_numbers"
                  FROM ATTRIBUTES
                 WHERE attribute_type_id IN (
                                            SELECT attribute_type_id
                                              FROM attribute_types
                                             WHERE name_display_code =
                                                                      'RD')
                   AND VALUE IN
                          ('Mature', 'Review', 'Final Draft',
                           'Final Comment', 'Final Document', 'Archived')
                   AND object_id IN (SELECT obj_id
                                       FROM docs)
              GROUP BY VALUE) a1 ON sta."status1" =
                                                  a1."progress_CMC_status"
             )
  ORDER BY DECODE ("progress_CMC_status",
             'Mature Draft', 0,
             'Review', 1,
             'Final Draft', 2,
             'Final Comment', 3,
             'Final Document', 4,
             'Archived Document', 5,
             6
            )

样本数据

          CREATE TABLE ATTRIBUTES
         (
         object_id NUMBER(4),
         attribute_type_id NUMBER(4),
         name_display_code VARCHAR2(5),
         VALUE VARCHAR2(25)
          );

         begin
         insert into attributes values (101,201,'RMD','A100');
         insert into attributes values (102,201,'RMD','A200');
         insert into attributes values (103,201,'RMD','A300');
         insert into attributes values (104,200,'RD','Mature');
         insert into attributes values (105,200,'RD','Mature');
         insert into attributes values (106,200,'RD','Mature');
         insert into attributes values (107,200,'RD','Mature');
         insert into attributes values (108,200,'RD','Mature');
         insert into attributes values (109,200,'RD','Archived');
         insert into attributes values (110,200,'RD','Archived');
         insert into attributes values (111,200,'RD','Archived');
         insert into attributes values (112,200,'RD','Archived');
         insert into attributes values (113,200,'RD','Mature');
         insert into attributes values (114,200,'RD','Mature');
         insert into attributes values (115,200,'RD','Mature');
         insert into attributes values (116,200,'RD','Mature');
         insert into attributes values (117,200,'RD','Final Document');
         insert into attributes values (118,200,'RD','Final Document');
         insert into attributes values (119,201,'RMD','A400');
         insert into attributes values (120,201,'RMD','A500');
         end;
         /

        CREATE TABLE docs
        (
        obj_id NUMBER(4)
        );


        BEGIN
        INSERT INTO docs
        VALUES (100);

        INSERT INTO docs
        VALUES (104);

        INSERT INTO docs
        VALUES (109);

        INSERT INTO docs
        VALUES (117);

        INSERT INTO docs
        VALUES (118);

        INSERT INTO docs
        VALUES (119);

        INSERT INTO docs
        VALUES (120);  
        END;
        /


       CREATE TABLE attribute_types
       (
       attribute_type_id NUMBER(4),
       name_display_code VARCHAR2(5)
       );


       INSERT INTO attribute_types
       VALUES (200, 'RD');


       INSERT INTO attribute_types
       VALUES (201, 'RMD');
4

1 回答 1

1
WITH sta AS
  (SELECT 'Mature' "status1" FROM DUAL UNION
  SELECT 'Review' FROM DUAL UNION
  SELECT 'Final Draft' FROM DUAL UNION
  SELECT 'Final Comment' FROM DUAL UNION
  SELECT 'Final Document' FROM DUAL UNION
  SELECT 'Archived' FROM DUAL)
SELECT 
  DECODE ("stat",
             'Mature', 'Mature Draft',
             'Review', 'Review',
             'Final Draft', 'Final Draft',
             'Final Comment', 'Final Comment',
             'Final Document', 'Final Document',
             'Archived', 'Archived Document'
  ) AS "progress_CMC_status",
  NVL("progress_cmc_status_numbers", 0) AS "progress_cmc_status_numbers",
  SUM (NVL("progress_cmc_status_numbers", 0)) OVER (ORDER BY 
            -DECODE ("stat",
            'Mature', 0,
            'Review', 1,
            'Final Draft', 2,
            'Final Comment', 3,
            'Final Document', 4,
            'Archived', 5,
            6) 
   ) AS "cmc_status_numbers"
FROM 
  (
    SELECT 
      "status1" AS "stat",
      "progress_cmc_status_numbers"
    FROM 
      sta
      LEFT JOIN (
        SELECT   
          VALUE AS "progress_CMC_status",
          COUNT (*) AS "progress_cmc_status_numbers"
        FROM ATTRIBUTES
        WHERE 
          attribute_type_id IN (
            SELECT attribute_type_id
            FROM attribute_types
            WHERE name_display_code = 'RD'
          )
          AND VALUE IN ('Mature', 'Review', 'Final Draft',
            'Final Comment', 'Final Document', 'Archived')
          AND object_id IN (SELECT obj_id FROM docs)
        GROUP BY VALUE
      ) a1 ON sta."status1" = a1."progress_CMC_status"
  )
  ORDER BY DECODE ("progress_CMC_status",
             'Mature Draft', 0,
             'Review', 1,
             'Final Draft', 2,
             'Final Comment', 3,
             'Final Document', 4,
             'Archived Document', 5,
             6)

小提琴

于 2013-04-08T15:06:34.573 回答