How to create incrementing columns?

寵の児 提交于 2021-02-11 06:42:12

问题


I have table with these column names.

Province/State
Country/Region
Lat
Long
1/22/20
1/23/20
1/24/20
1/25/20
...
...
3/21/20

I know to create first 4 columns but I don't know how create date column and increment it.

How can I implement such number of columns at once?
Thank you!

Infected

Dead

Recovered

Object relational data model created by me

Question -: Submit working Oracle script for your database schema.


回答1:


Don't try to create a column-per-day; just create a table with columns for location, date and for each statistic (i.e. infected, recovered, dead, etc.) and then if you need to pivot them do that in a query (or in whatever middle-tier application [i.e. PHP, Java, .net] you're using to access the database).

Something like:

CREATE TABLE Regions(
  id        VARCHAR2(6)
            CONSTRAINT regions__id__pk PRIMARY KEY,
  parent_id VARCHAR2(6)
            CONSTRAINT regions_parent__fk REFERENCES Regions ( id ),
  name      VARCHAR2(50)
            CONSTRAINT regions__name__nn NOT NULL
            CONSTRAINT regions__name__u UNIQUE,
  latitude  NUMBER
            CONSTRAINT regions__lat__nn NOT NULL,
  longitude NUMBER
            CONSTRAINT regions__long__nn NOT NULL,
  CONSTRAINT regions__id__chk CHECK (
       ( parent_id IS     NULL AND REGEXP_LIKE( id, '^[A-Z]{2}$' ) )
    OR ( parent_id IS NOT NULL AND REGEXP_LIKE( id, '^[A-Z]{2}-[A-Z0-9]{1,3}$' ) )
  )
);
COMMENT ON COLUMN Regions.id        IS 'ISO 3166-2 Alpha-2 Country Code or ISO 3166-2 Province Code';
COMMENT ON COLUMN Regions.name      IS 'ISO 3166-2 English Short Name.';
COMMENT ON COLUMN Regions.latitude  IS 'Latitude of the region''s main city.';
COMMENT ON COLUMN Regions.longitude IS 'Longitude of the region''s main city.';

CREATE TABLE Virus_Statistics(
  id        NUMBER(20,0)
            GENERATED ALWAYS AS IDENTITY
            CONSTRAINT virus_statistics__id__pk PRIMARY KEY,
  location  VARCHAR2(6)
            CONSTRAINT virus_statistics__loc__nn NOT NULL
            CONSTRAINT virus_statistics__loc__fk REFERENCES Regions ( id ),
  datetime  DATE
            CONSTRAINT virus_statistics__dt__nn NOT NULL
            CONSTRAINT virus_statistics__dt__chk CHECK ( datetime = TRUNC( datetime ) ),
  infected  NUMBER(10,0),
  recovered NUMBER(10,0),
  dead      NUMBER(10,0),
  CONSTRAINT virus_statistics__loc__dt__u UNIQUE ( location, datetime )
);

Then you can input your data. For example, the regions would be:

INSERT INTO Regions ( id, parent_id, name, latitude, longitude )
SELECT 'TH',    NULL, 'Thailand',          15.00000,  101.00000 FROM DUAL UNION ALL
SELECT 'JP',    NULL, 'Japan',             36.00000,  138.00000 FROM DUAL UNION ALL
SELECT 'SG',    NULL, 'Singapore',          1.28333,  103.83333 FROM DUAL UNION ALL
SELECT 'NP',    NULL, 'Nepal',             28.16667,   84.25000 FROM DUAL UNION ALL
SELECT 'MY',    NULL, 'Malaysia',           2.50000,  112.50000 FROM DUAL UNION ALL
SELECT 'CA',    NULL, 'Canada',            45.42472, - 75.69500 FROM DUAL UNION ALL
SELECT 'CA-BC', 'CA', 'British Columbia',  48.40733, -123.32977 FROM DUAL;

And the first 3 columns of data would be:

INSERT INTO Virus_Statistics ( location, datetime, infected, recovered, dead )
SELECT 'TH',    DATE '2020-01-22', 2 AS i, 0 AS r, 0 AS d FROM DUAL UNION ALL
SELECT 'TH',    DATE '2020-01-23', 3, 0, 0 FROM DUAL UNION ALL
SELECT 'TH',    DATE '2020-01-24', 5, 0, 0 FROM DUAL UNION ALL
SELECT 'JP',    DATE '2020-01-22', 2, 0, 0 FROM DUAL UNION ALL
SELECT 'JP',    DATE '2020-01-23', 1, 0, 0 FROM DUAL UNION ALL
SELECT 'JP',    DATE '2020-01-24', 2, 0, 0 FROM DUAL UNION ALL
SELECT 'SG',    DATE '2020-01-22', 0, 0, 0 FROM DUAL UNION ALL
SELECT 'SG',    DATE '2020-01-23', 1, 0, 0 FROM DUAL UNION ALL
SELECT 'SG',    DATE '2020-01-24', 3, 0, 0 FROM DUAL UNION ALL
SELECT 'NP',    DATE '2020-01-22', 0, 0, 0 FROM DUAL UNION ALL
SELECT 'NP',    DATE '2020-01-23', 0, 0, 0 FROM DUAL UNION ALL
SELECT 'NP',    DATE '2020-01-24', 0, 0, 0 FROM DUAL UNION ALL
SELECT 'MY',    DATE '2020-01-22', 0, 0, 0 FROM DUAL UNION ALL
SELECT 'MY',    DATE '2020-01-23', 0, 0, 0 FROM DUAL UNION ALL
SELECT 'MY',    DATE '2020-01-24', 0, 0, 0 FROM DUAL UNION ALL
SELECT 'CA-BC', DATE '2020-01-22', 0, 0, 0 FROM DUAL UNION ALL
SELECT 'CA-BC', DATE '2020-01-23', 0, 0, 0 FROM DUAL UNION ALL
SELECT 'CA-BC', DATE '2020-01-24', 0, 0, 0 FROM DUAL;

Then if you want to output it as columns-per-day then use a PIVOT:

SELECT *
FROM   (
  SELECT name,
         latitude,
         longitude,
         datetime,
         infected
  FROM   Virus_Statistics v
         INNER JOIN Regions r
         ON ( r.id = v.location )
)
PIVOT (
  MAX( infected )
  FOR datetime IN (
    DATE '2020-01-22' AS "2020-01-22",
    DATE '2020-01-23' AS "2020-01-23",
    DATE '2020-01-24' AS "2020-01-24"
  )
)

Which outputs:

NAME             | LATITUDE |  LONGITUDE | 2020-01-22 | 2020-01-23 | 2020-01-24
:--------------- | -------: | ---------: | ---------: | ---------: | ---------:
Japan            |       36 |        138 |          2 |          1 |          2
Malaysia         |      2.5 |      112.5 |          0 |          0 |          0
Singapore        |  1.28333 |  103.83333 |          0 |          1 |          3
Nepal            | 28.16667 |      84.25 |          0 |          0 |          0
British Columbia | 48.40733 | -123.32977 |          0 |          0 |          0
Thailand         |       15 |        101 |          2 |          3 |          5

db<>fiddle here



来源:https://stackoverflow.com/questions/60819035/how-to-create-incrementing-columns

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!