仅包含当前记录的 Oracle 表;使用 max(date) 减少重复项

发布于 2024-11-02 17:40:22 字数 1304 浏览 5 评论 0原文

我需要在 Oracle 中创建一个新表,其中仅包含每条记录的最新日期(步骤 1),并计算之间的天数(步骤 2)。

非常感谢您的建议:)))

第 1 步:首先,我需要从表 USERS 中找到每条记录的最大值(Mod_date)。

表:用户

姓名................Mod_Date

Jason Martin....25-JUL-89

Al Mathews......... 21-MAR- 89

詹姆斯·史密斯........ 88年12月12日

罗伯特·布莱克....... 84年1月15日

贾森·马丁....... 99年7月25日

艾尔·马修斯.... ..... 96 年 3 月 21 日

詹姆斯·史密斯 ........ 98 年 12 月 12 日

罗伯特·布莱克 ...... 94 年 1 月 15 日

*TABLE_DESIRED_RESULTS_step1

名称 ........ ....... Max(Mod_Date)

Jason Martin....... 99 年 7 月 25 日

阿尔·马修斯......... 96 年 3 月 21 日

詹姆斯·史密斯...... .12-DEC-98

Robert Black.......15-JAN-94

步骤 2:计算“Regist_Date 和 Mod_Date 之间的天数”&将其添加到表中。

表:注册

名称................注册_日期

Jason Martin.........20-7-JUL-99

Al Mathews....... 96 年 3 月 23 日

罗伯特·布莱克.........94 年 1 月 20 日

*TABLE_DESIRED_RESULTS_step2

名称.........................最大(Mod_Date).....数量Regist_Date 和 Mod_Date 之间的天数

Jason Martin......99 年 7 月 25 日......5

Al Mathews......96 年 3 月 21 日...... ...-2

詹姆斯·史密斯......98 年 12 月 12 日......无效

罗伯特·布莱克......94 年 1 月 15 日...... ...-5

*请注意,这些数据是编造的,我已经有现有的联合和联接,我必须向其中添加此逻辑。谢谢,祝你有美好的一天!

I need to create a new table in oracle with only the most current date for each record (step 1), and calculate days between (step 2).

Your suggestions are greatly appreciated:)))

Step 1: First I need to find the max (Mod_date) for each record from table USERS.

TABLE: USERS

Name................Mod_Date

Jason Martin....... 25-JUL-89

Al Mathews......... 21-MAR-89

James Smith........ 12-DEC-88

Robert Black....... 15-JAN-84

Jason Martin....... 25-JUL-99

Al Mathews......... 21-MAR-96

James Smith........ 12-DEC-98

Robert Black....... 15-JAN-94

*TABLE_DESIRED_RESULTS_step1

Name............... Max(Mod_Date)

Jason Martin....... 25-JUL-99

Al Mathews......... 21-MAR-96

James Smith........12-DEC-98

Robert Black.......15-JAN-94

Step 2: Calculate “Number of Days Between Regist_Date and Mod_Date” & add it to the table.

TABLE: REGISTRATION

Name................Regist_Date

Jason Martin.........20-JUL-99

Al Mathews...........23-MAR-96

Robert Black.........20-JAN-94

*TABLE_DESIRED_RESULTS_step2

Name...............Max(Mod_Date).....Number of Days Between Regist_Date and Mod_Date

Jason Martin...... 25-JUL-99..........5

Al Mathews........ 21-MAR-96.........-2

James Smith....... 12-DEC-98..........null

Robert Black...... 15-JAN-94..........-5

*Please note, this data is made up and I already have existing unions and joins to which I have to add this logic. Thanks and have a nice day!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

卷耳 2024-11-09 17:40:22

这是我的更新答案和示例。

重要的是您的日期列具有 DATE 类型。
这是符合您的规格的表格和数据。

CREATE TABLE USERS
   (
    ID_USER NUMBER(6)  NOT NULL,
    NAME VARCHAR2(64)  NOT NULL,
    MOD_DATE DATE  NOT NULL,
    CONSTRAINT PK_user PRIMARY KEY (ID_USER)
   ) ;

    INSERT INTO USERS VALUES (1,'Jason Martin',TO_DATE('25-07-1989','DD-MM-YYYY'));
    INSERT INTO USERS VALUES (2,'Al Mathews',TO_DATE('21-03-1989','DD-MM-YYYY'));
    INSERT INTO USERS VALUES (3,'James Smith',TO_DATE('12-12-1988','DD-MM-YYYY'));
    INSERT INTO USERS VALUES (4,'Robert Black',TO_DATE('15-01-1984','DD-MM-YYYY'));
    INSERT INTO USERS VALUES (5,'Jason Martin',TO_DATE('25-07-1999','DD-MM-YYYY'));
    INSERT INTO USERS VALUES (6,'Al Mathews',TO_DATE('21-03-1996','DD-MM-YYYY'));
    INSERT INTO USERS VALUES (7,'James Smith',TO_DATE('12-12-1998','DD-MM-YYYY'));
    INSERT INTO USERS VALUES (8,'Robert Black',TO_DATE('15-01-1994','DD-MM-YYYY'));


CREATE TABLE REGISTRATION
   (
    ID_REG NUMBER(6)  NOT NULL,
    NAME VARCHAR2(64)  NOT NULL,
    REGIST_DATE DATE  NOT NULL,
    CONSTRAINT PK_reg PRIMARY KEY (ID_REG)
   ) ;

    INSERT INTO REGISTRATION VALUES (1,'Jason Martin',TO_DATE('20-07-1999','DD-MM-YYYY'));
    INSERT INTO REGISTRATION VALUES (2,'Al Mathews',TO_DATE('23-03-1996','DD-MM-YYYY'));
    INSERT INTO REGISTRATION VALUES (3,'Robert Black',TO_DATE('20-01-1994','DD-MM-YYYY'));

第一步

   CREATE  TABLE TABLE_DESIRED_RESULTS_step1
   AS ( 
   SELECT
         u.NAME
        , max(u.MOD_DATE) as "maxi"
       FROM USERS u
       GROUP BY u.NAME);

第二步

CREATE TABLE TABLE_DESIRED_RESULTS_step2 
AS (
SELECT 
    t.NAME 
    ,t."maxi"
    , (t."maxi" - r.REGIST_DATE ) as "Nbdays bw RegD and Mod_D"
FROM TABLE_DESIRED_RESULTS_step1 t LEFT OUTER JOIN REGISTRATION r 
ON t.NAME = r.NAME);

这里的技巧是,如果连接不匹配,LEFT OUTER JOIN 允许空值。


但我有一个数据库设计问题。
如果您有 2 个同名用户,您将把 2 个用户合并为一个。
这里有一个使用 ID 并在 ID 上进行连接的解决方案。

CREATE TABLE USERS
   (
    ID_USER NUMBER(6)  NOT NULL,
    NAME VARCHAR2(64)  NOT NULL,
    CONSTRAINT PK_user PRIMARY KEY (ID_USER)
   ) ; 

CREATE TABLE MOD_USERS
   (
    ID_MOD NUMBER(6)  NOT NULL,
    ID_USER NUMBER(6)  NOT NULL,
    CONSTRAINT PK_usermod PRIMARY KEY (ID_MOD)
   ) ; 

ALTER TABLE MOD_USERS ADD (
     CONSTRAINT FK_user_mod
          FOREIGN KEY (ID_USER)
               REFERENCES USERS (ID_USER));

CREATE TABLE REGISTRATION
   (
    ID_REG NUMBER(6)  NOT NULL,
    ID_USER VARCHAR2(64)  NOT NULL,
    REGIST_DATE DATE  NOT NULL,
    CONSTRAINT PK_reg PRIMARY KEY (ID_REG)
   ) ;

ALTER TABLE REGISTRATION ADD (
     CONSTRAINT FK_user_reg
          FOREIGN KEY (ID_USER)
               REFERENCES USERS (ID_USER))

;

第一步

   CREATE  TABLE TABLE_DESIRED_RESULTS_step1
   AS ( 
   SELECT
         m.ID_USER , u.NAME
        , max(u.MOD_DATE) as "maxi"
       FROM USERS u INNER JOIN MOD_USERS m
           ON u.ID_USER = m.ID_USER
       GROUP BY m.ID_USER , u.NAME);

第二步

CREATE TABLE TABLE_DESIRED_RESULTS_step2 
AS (
SELECT 
    t.ID_USER , t.NAME 
    ,t."maxi"
    , (t."maxi" - r.REGIST_DATE ) as "Nbdays bw RegD and Mod_D"
FROM TABLE_DESIRED_RESULTS_step1 t LEFT OUTER JOIN REGISTRATION r 
ON t.ID_USER = r.ID_USER);

here is my updated answer with a sample.

The important thing is that your date column have the DATE type.
Here is the tables and data following your specification.

CREATE TABLE USERS
   (
    ID_USER NUMBER(6)  NOT NULL,
    NAME VARCHAR2(64)  NOT NULL,
    MOD_DATE DATE  NOT NULL,
    CONSTRAINT PK_user PRIMARY KEY (ID_USER)
   ) ;

    INSERT INTO USERS VALUES (1,'Jason Martin',TO_DATE('25-07-1989','DD-MM-YYYY'));
    INSERT INTO USERS VALUES (2,'Al Mathews',TO_DATE('21-03-1989','DD-MM-YYYY'));
    INSERT INTO USERS VALUES (3,'James Smith',TO_DATE('12-12-1988','DD-MM-YYYY'));
    INSERT INTO USERS VALUES (4,'Robert Black',TO_DATE('15-01-1984','DD-MM-YYYY'));
    INSERT INTO USERS VALUES (5,'Jason Martin',TO_DATE('25-07-1999','DD-MM-YYYY'));
    INSERT INTO USERS VALUES (6,'Al Mathews',TO_DATE('21-03-1996','DD-MM-YYYY'));
    INSERT INTO USERS VALUES (7,'James Smith',TO_DATE('12-12-1998','DD-MM-YYYY'));
    INSERT INTO USERS VALUES (8,'Robert Black',TO_DATE('15-01-1994','DD-MM-YYYY'));


CREATE TABLE REGISTRATION
   (
    ID_REG NUMBER(6)  NOT NULL,
    NAME VARCHAR2(64)  NOT NULL,
    REGIST_DATE DATE  NOT NULL,
    CONSTRAINT PK_reg PRIMARY KEY (ID_REG)
   ) ;

    INSERT INTO REGISTRATION VALUES (1,'Jason Martin',TO_DATE('20-07-1999','DD-MM-YYYY'));
    INSERT INTO REGISTRATION VALUES (2,'Al Mathews',TO_DATE('23-03-1996','DD-MM-YYYY'));
    INSERT INTO REGISTRATION VALUES (3,'Robert Black',TO_DATE('20-01-1994','DD-MM-YYYY'));

First step

   CREATE  TABLE TABLE_DESIRED_RESULTS_step1
   AS ( 
   SELECT
         u.NAME
        , max(u.MOD_DATE) as "maxi"
       FROM USERS u
       GROUP BY u.NAME);

second step

CREATE TABLE TABLE_DESIRED_RESULTS_step2 
AS (
SELECT 
    t.NAME 
    ,t."maxi"
    , (t."maxi" - r.REGIST_DATE ) as "Nbdays bw RegD and Mod_D"
FROM TABLE_DESIRED_RESULTS_step1 t LEFT OUTER JOIN REGISTRATION r 
ON t.NAME = r.NAME);

The trick here is that LEFT OUTER JOIN allows null value if there is no match with the join.


But there is a database design concern for me.
If you have 2 users with the exact same name , you will merge 2 persons in one.
Here a solution using IDs and doing the join on the IDs.

CREATE TABLE USERS
   (
    ID_USER NUMBER(6)  NOT NULL,
    NAME VARCHAR2(64)  NOT NULL,
    CONSTRAINT PK_user PRIMARY KEY (ID_USER)
   ) ; 

CREATE TABLE MOD_USERS
   (
    ID_MOD NUMBER(6)  NOT NULL,
    ID_USER NUMBER(6)  NOT NULL,
    CONSTRAINT PK_usermod PRIMARY KEY (ID_MOD)
   ) ; 

ALTER TABLE MOD_USERS ADD (
     CONSTRAINT FK_user_mod
          FOREIGN KEY (ID_USER)
               REFERENCES USERS (ID_USER));

CREATE TABLE REGISTRATION
   (
    ID_REG NUMBER(6)  NOT NULL,
    ID_USER VARCHAR2(64)  NOT NULL,
    REGIST_DATE DATE  NOT NULL,
    CONSTRAINT PK_reg PRIMARY KEY (ID_REG)
   ) ;

ALTER TABLE REGISTRATION ADD (
     CONSTRAINT FK_user_reg
          FOREIGN KEY (ID_USER)
               REFERENCES USERS (ID_USER))

;

First step

   CREATE  TABLE TABLE_DESIRED_RESULTS_step1
   AS ( 
   SELECT
         m.ID_USER , u.NAME
        , max(u.MOD_DATE) as "maxi"
       FROM USERS u INNER JOIN MOD_USERS m
           ON u.ID_USER = m.ID_USER
       GROUP BY m.ID_USER , u.NAME);

second step

CREATE TABLE TABLE_DESIRED_RESULTS_step2 
AS (
SELECT 
    t.ID_USER , t.NAME 
    ,t."maxi"
    , (t."maxi" - r.REGIST_DATE ) as "Nbdays bw RegD and Mod_D"
FROM TABLE_DESIRED_RESULTS_step1 t LEFT OUTER JOIN REGISTRATION r 
ON t.ID_USER = r.ID_USER);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文