0

我的表结构是:table_system:

"ID"               NUMBER NOT NULL ENABLE,
"COUNTRY"          VARCHAR2(10 BYTE) NOT NULL ENABLE,
"COMPANYCODE"      VARCHAR2(50 BYTE) NOT NULL ENABLE,
"SYSTEM"           VARCHAR2(50 BYTE) NOT NULL ENABLE,
"NOTSTARTED"       NUMBER,
"RUNNING"          NUMBER,
"COMPLETED"        NUMBER,
"ACTUALSTARTTIME"  VARCHAR2(5 BYTE),
"ACTUALENDTIME"    VARCHAR2(5 BYTE),
"SEQUENCE"         NUMBER,
"PLANNEDSTARTTIME" VARCHAR2(5 BYTE),
"PLANNEDENDTIME"   VARCHAR2(5 BYTE),
"ESTIMATEDENDTIME" VARCHAR2(5 BYTE),
CONSTRAINT "SYSTEMRUNTIME_PK" PRIMARY KEY ("ID", "COUNTRY", "COMPANYCODE", "SYSTEM") USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645 PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT) TABLESPACE "SYSTEM" ENABLE

我需要一个输出来获取以下输出:

COMPANYCODE                                         SYSTEM1 SYSTEM2 SYSTEM3 SYSTEM4 SYSTEM5 SYSTEM6 SYSTEM7 SYSTEM8 …   SYSTEM N
--------------------------------------------------  --------------------------- ------------------- ----------------------- ------------------------------  ------------------------------  --------------------    --------------  --------------  --------------  --------------

其中系统根据“SEQUENCE”属性进行排序。

我试过这个查询:

    select distinct companycode, sequence, system,notstarted,running,completed
    from table_system
    where id = (select max(id) from table_system)
    order by companycode, sequence

这让我得到以下信息

COMPANYCODE SEQUENCE    SYSTEM  NOTSTARTED  RUNNING COMPLETED
--------------------------------------------------  ----------------------  --------------------------------------------------  ----------------------  ----------------------  ----------------------
1001 Helsinki Branch    1   GAP 2 / Datastage GL    0   0   3
1001 Helsinki Branch    2   SAP GL  0   0   2
1001 Helsinki Branch    3   SAP BW  0   0   2
1002 Copenhagen Branch  1   GAP 2 / Datastage GL    0   0   3
1002 Copenhagen Branch  2   SAP GL  0   0   2
1002 Copenhagen Branch  3   SAP BW  0   0   2
1003 Oslo Branch    1   GAP 2 / Datastage GL    0   0   3
1003 Oslo Branch    2   SAP GL  0   0   2
1003 Oslo Branch    3   SAP BW  0   0   2
1004 (publ) (EUR)   1   EKO 0   0   13
1004 (publ) (EUR)   2   HA Core 0   0   6
1004 (publ) (EUR)   3   HA Post Processor   0   0   5
1004 (publ) (EUR)   4   Datastage GL    3   0   10
1004 (publ) (EUR)   5   Datastage Recon 1   0   3
1004 (publ) (EUR)   11  SAP GL  0   0   4
1004 (publ) (EUR)   21  SAP BW  0   0   4

但我希望输出为:

COMPANYCODE                                         SYSTEM1 SYSTEM2 SYSTEM3 SYSTEM4 SYSTEM5 SYSTEM6 SYSTEM7 SYSTEM8 …   SYSTEM N
--------------------------------------------------  --------------------------- ------------------- ----------------------- ------------------------------  ------------------------------  --------------------    --------------  --------------  --------------  --------------
1001 Helsinki Branch            GAP 2 / Datastage GL    SAP GL  SAP BW                          
1002 Copenhagen Branch          GAP 2 / Datastage GL    SAP GL  SAP BW                          
1003 Oslo Branch                GAP 2 / Datastage GL    SAP GL  SAP BW                          
1004 (publ) (EUR)                       EKO HA Core HA Post Processor   Datastage GL    Datastage Recon SAP GL  SAP BW      

上述任何提示将不胜感激。谢谢你 vinayak

4

2 回答 2

0

试试看:

select companycode, COLLECT(system) as systems
    from table_system
    where id = (select max(id) from table_system)    
    group by companycode
    order by companycode, sequence
于 2012-07-24T12:21:14.090 回答
0

您可以为此使用枢轴操作;但不能处理未知数量的系统(因为您需要在解析时知道所选列的数量):

select * from
(
    select companycode, system,
        row_number() over (partition by id, country, companycode
            order by sequence) as rn
    from table_system
    where id = (select max(id) from table_system)
)
pivot (max(system) for rn in (1 as system1, 2 as system2, 3 as system3,
    4 as system4, 5 as system5, 6 as system6, 7 as system7, 8 as system8))
order by company code;


COMPANYCODE                                        SYSTEM1                                            SYSTEM2                                            SYSTEM3                                            SYSTEM4                                            SYSTEM5                                            SYSTEM6                                            SYSTEM7                                            SYSTEM8
-------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- --------------------------------------------------
1001 Helsinki Branch                               GAP 2 / Datastage GL                               SAP GL                                             SAP BW
1002 Copenhagen Branch                             GAP 2 / Datastage GL                               SAP GL                                             SAP BW
1003 Oslo Branch                                   GAP 2 / Datastage GL                               SAP GL                                             SAP BW
1004 (publ) (EUR)                                  EKO                                                HA Core                                            HA Post Processor                                  Datastage GL                                       Datastage Recon                                    SAP GL                                             SAP BW

因此,您需要确定您将拥有的最大系统数,并将子句添加到枢轴 ( 9 as system9, ...) 以容纳所有系统。将row_number()序列号转换为连续的数字,因此您在公司的第 5 和第 6 系统之间没有太大的差距1004;除了其他任何事情之外,您还需要枢轴来处理最大可能的序列号,而不是最大的系统数。

于 2012-07-24T13:45:25.407 回答