嵌套表在Oracle中提供了意外表

问题描述 投票:0回答:2

这些天我正在使用此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)
                 )
        )
);

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

运行后,它给了我这张表

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

但是我期望的表是

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

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

您可以在here中测试该脚本

oracle oracle11g sqlplus
2个回答
0
投票

当您使用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具有自己的[[breaking功能。

0
投票
您可以如下使用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 from countries c, table(c.virus) v) ORDER BY RN /

请参见db<>fiddle demo

© www.soinside.com 2019 - 2024. All rights reserved.