嵌套表在 Oracle 中给出了意外的 table

Nested Tables gives unexpected table in Oracle

这些天我正在使用这个 oracle 脚本。

create type virus_Statistic_t as object(
    vDate date,
    infection int,
    dead int,
    recovered int
)
/

create type virus_Statistic_tlb as table of virus_Statistic_t

create type countries_t as object(
    Province_or_State varchar2(50),
    Country_or_Region varchar2(100),
    Lat Number(10,6),
    Longt Number(10,6),
    virus virus_Statistic_tlb
)
/

create table countries of countries_t (
       primary key(Province_or_State, Country_or_Region)
) nested table virus store as virus_ntb;

INSERT INTO countries VALUES (
       countries_t('British Columbia', 'Canada', 49.2827, -123.1207, 
                 virus_Statistic_tlb(
                        virus_Statistic_t('22-JAN-20', 5, 0, 0), 
                        virus_Statistic_t('23-JAN-20', 10, 2, 5)
                 )
        )
);

INSERT INTO countries VALUES (
       countries_t('Queensland', 'Australia', -28.0167, 153.4, 
                 virus_Statistic_tlb(
                        virus_Statistic_t('22-JAN-20', 20, 0, 0), 
                        virus_Statistic_t('23-JAN-20', 10, 8, 10)
                 )
        )
);

select c.Province_or_State, c.Country_or_Region, c.Lat, c.Longt,  v.vDate, v.infection, v.dead, v.recovered 
from countries c, table(c.virus) v

在我 运行 之后它给了我这个 table

PROVINCE_OR_STATE  COUNTRY_OR_REGION    LAT         LONGT    VDATE     INFECTION    DEAD    RECOVERED
British Columbia    Canada            49.2827   -123.1207   22-JAN-20   5            0      0
British Columbia    Canada            49.2827   -123.1207   23-JAN-20   10           2      5
Queensland          Australia        -28.0167    153.4      22-JAN-20   20           0      0
Queensland          Australia        -28.0167    153.4      23-JAN-20   10           8      10

但我的预期 table 是

PROVINCE_OR_STATE  COUNTRY_OR_REGION    LAT         LONGT    VDATE     INFECTION    DEAD    RECOVERED
British Columbia    Canada            49.2827   -123.1207   22-JAN-20   5            0      0
                                                            23-JAN-20   10           2      5
Queensland          Australia        -28.0167    153.4      22-JAN-20   20           0      0
                                                            23-JAN-20   10           8      10

我应该对我的代码进行哪些更改?

您可以在 here

中测试该脚本

因为你用SQL*Plus 标记,那么break 就是你需要的。

这是你现在拥有的:

SQL> select c.Province_or_State, c.Country_or_Region, c.Lat, c.Longt,
  2         v.vDate, v.infection, v.dead, v.recovered
  3  from countries c, table(c.virus) v;

PROVINCE_OR_STAT COUNTRY_OR_REGION           LAT      LONGT VDATE     INFECTION       DEAD  RECOVERED
---------------- -------------------- ---------- ---------- -------- ---------- ---------- ----------
British Columbia Canada                  49,2827  -123,1207 22.01.20          5          0          0
British Columbia Canada                  49,2827  -123,1207 23.01.20         10          2          5

中断:

SQL> break on province_or_state on country_or_region on lat on longt
SQL> select c.Province_or_State, c.Country_or_Region, c.Lat, c.Longt,
  2         v.vDate, v.infection, v.dead, v.recovered
  3  from countries c, table(c.virus) v;

PROVINCE_OR_STAT COUNTRY_OR_REGION           LAT      LONGT VDATE     INFECTION       DEAD  RECOVERED
---------------- -------------------- ---------- ---------- -------- ---------- ---------- ----------
British Columbia Canada                  49,2827  -123,1207 22.01.20          5          0          0
                                                            23.01.20         10          2          5

SQL>

其他(报告)工具,例如 Oracle Reports Builder 或 Apex Classic Report 具有自己的突破性功能。


插入另一行后,查询(实际上,break)仍然按预期工作:

SQL> /

PROVINCE_OR_STAT COUNTRY_OR_REGION           LAT      LONGT VDATE     INFECTION       DEAD  RECOVERED
---------------- -------------------- ---------- ---------- -------- ---------- ---------- ----------
British Columbia Canada                  49,2827  -123,1207 22.01.20          5          0          0
                                                            23.01.20         10          2          5
Queensland       Australia              -28,0167      153,4 22.01.20         20          0          0
                                                            23.01.20         10          8         10

SQL>

您正在将 c.virus 转换为 table,它包含两条交叉连接到主 table 的记录。因此,您将获得两条记录(1 条记录交叉连接 2 条记录 = 2 条记录)

您可以按如下方式使用analytical function

SELECT CASE WHEN RN = 1 THEN Province_or_State END AS Province_or_State,
       CASE WHEN RN = 1 THEN Country_or_Region END AS Country_or_Region,
       CASE WHEN RN = 1 THEN Lat END AS Lat,
       CASE WHEN RN = 1 THEN Longt END AS Longt,
       vDate, infection, dead, recovered 
  FROM
 (select c.Province_or_State, c.Country_or_Region, c.Lat, c.Longt,  v.vDate, v.infection, v.dead, v.recovered 
 ,ROW_NUMBER() OVER (PARTITION BY c.Province_or_State, c.Country_or_Region, c.Lat, c.Longt ORDER BY V.VDATE) AS RN,
 , DENSE_RANK() OVER (ORDER BY c.Province_or_State, c.Country_or_Region, c.Lat, c.Longt) AS DRN
  from countries c, table(c.virus) v)
 ORDER BY DRN, RN
/

db<>fiddle demo