【问题标题】:sql select insertion from table 2 to table 1sql select 从表2插入到表1
【发布时间】:2014-01-05 00:38:34
【问题描述】:

我的 sql 数据库中有两个表:

表 1 中的列:

entity ID, contact person,  contact ID,  created date

表 2 中的列:

contact ID   entity ID    modified date   contact person
1             2            10/12/13         MR.A
1             2            11/12/13         MR.B
4             16           17/12/13         MR.C
4             16           19/12/13         MR.D

我想将表 2 中的记录插入到表 1 中,其中同一 contact ID 的修改日期最大。

输出应该是这样的:

表 1

entity ID  contact person  contact ID  created date
2           MR.B              1         11/12/13
16          MR.D              4         19/12/13

请建议如何编写这样的 SQL 查询。我使用了一些带有 max 的查询,但没有得到我的答案,猜猜那不是正确放置的?

提前致谢

【问题讨论】:

  • 同一个实体ID和联系人ID有可能有两个不同的联系人吗?
  • 是的,有可能,主键里面有别的东西

标签: sql insertion


【解决方案1】:

看起来您在 Table2 上没有主键。如果你这样做了,那么这会容易得多,因为你可以在连接中使用它。但是,如果您不这样做,则此 sql 可以解决问题:

INSERT INTO Table1("entity ID", "contact person", "Contact ID", "created date")
SELECT b."entity ID",  b."contact person", b."contact ID",  b."created date"
FROM
(
    SELECT "Contact ID", "entity ID" , MAX("created date")  "created date"                 
    FROM Table2
    GROUP BY "Contact ID", "entity ID" 
) a
JOIN Table2 b ON b."Contact ID" = a."Contact ID" AND
                 b."entity ID" = a."entity ID" AND
                 b."created date" = a."created date"

Sql fiddle可以是found here

【讨论】:

  • 我正要发布很多内容,但想在发布欺骗之前阅读前两篇提交的内容。然后你打败了我:-|
【解决方案2】:

嗯,你需要学习一下,下次尝试找到解决方案。

INSERT INTO  table_1 (entity_ID, contact_person,  contact_ID,  created_date)
SELECT entity_ID, contact_person, contact_ID, Max(created date)
FROM table_2
Group by entity_ID, contact_person, contact_ID

【讨论】:

    【解决方案3】:

    插入table1值(select t2.entityid, t2.contactperson, t2.contactid, (select max (t2.createddate) from table2 t22 where t22.entityid = t2.entityid) from table2 t2 groupby t2.entityid, t2 .contactperson, t2.contactid )

    【讨论】:

    • 请 - 在发布代码时 - 格式化 使用文本编辑器上的 ` { } ` 按钮启用正确的显示和语法高亮显示代码!
    【解决方案4】:

    试试这个:

    INSERT INTO Table1 (EntityID,ContactPerson,ContactID,CreatedDate)
    SELECT 
    EntityID,ContactPerson,ContactID,ModifiedDate
    FROM
    (
    Select Row_NUMBER() Over(Partition By ContactId order by ModifiedDate DESC) ROW_NO,
    EntityID,ContactPerson,ContactID,ModifiedDate 
    from Table2
    ) AS T WHERE T.ROW_NO = 1
    

    【讨论】:

      【解决方案5】:

      使用下面的选择查询获取数据,然后插入数据:

      select 
          t1.contact_id, t1.ENTITY_ID, t1.MODIFIED_DATE, t1.CONTACT_PERSON 
      from 
          test_table t1,
          (select 
               contact_id, max(modified_date) as max_modified_date 
           from 
                test_table 
           group by 
                contact_id) t2 
      where 
           t1.contact_id = t2.contact_id and t1.MODIFIED_DATE = t2.max_modified_date;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-10-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-07-06
        • 2023-03-23
        • 1970-01-01
        • 2012-10-27
        相关资源
        最近更新 更多