AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / dba / 问题 / 177957
Accepted
Lee
Lee
Asked: 2017-07-05 04:58:46 +0800 CST2017-07-05 04:58:46 +0800 CST 2017-07-05 04:58:46 +0800 CST

如果某些列在多行中相同,如何设置值

  • 772

我正在进行数据迁移,最终得到一个临时表,如下所示:

curid cuid  rtid    cd          dd      rm
10     4    4   2016-01-02  2016-07-02  
16     4    4   2016-06-12  2016-12-12  Remarks Jun 12
18     5    3   2016-07-18  2017-07-31  
8      5    3   2015-06-21  2016-06-30  Add some test
11     6    4   2017-01-01  2017-07-01  
9      7    3   2017-01-01  2018-01-31  

我需要将数据分成两个表。基于相同的数据,它应该是这样的:

Table A
    id  curid cuid  rtid
    1    10    4    4   
    2    18    5    3   
    3    11    6    4   
    4     9    7    3

这是每个不同的行(cuid, rtid)加上curid从每组重复项中选择的值。id只是一个序号。

Table B
    id curid    cd           dd         rm
    1   10     2016-01-02   2016-07-02  
    2   10     2016-06-12   2016-12-12  Remarks Jun 12
    3   18     2016-07-18   2017-07-31  
    4   18     2015-06-21   2016-06-30  Add some test
    5   11     2017-01-01   2017-07-01  
    6    9     2017-01-01   2018-01-31

实际curid是无关紧要的,只要中的记录Table B与中的关联记录相匹配Table A(因此我们甚至可以使用临时序列或其他东西来设置curid)。

postgresql update
  • 3 3 个回答
  • 119 Views

3 个回答

  • Voted
  1. Best Answer
    Erwin Brandstetter
    2017-07-05T08:06:17+08:002017-07-05T08:06:17+08:00

    您的测试设置

    (最好在您的问题中以这种方式提供- 提示!)

    CREATE TEMP TABLE tmp (
      curid int
    , cuid  int
    , rtid  int
    , cd    date
    , dd    date
    , rm    text);
    
    INSERT INTO tmp VALUES
      (10, 4, 4, '2016-01-02', '2016-07-02', NULL)  
     ,(16, 4, 4, '2016-06-12', '2016-12-12', 'Remarks Jun 12')
     ,(18, 5, 3, '2016-07-18', '2017-07-31', NULL)  
     ,(8 , 5, 3, '2015-06-21', '2016-06-30', 'Add some test')
     ,(11, 6, 4, '2017-01-01', '2017-07-01', NULL)
     ,(9 , 7, 3, '2017-01-01', '2018-01-31', NULL);
    

    解决方案

    如果目标表不存在则创建它们:

    CREATE TEMP TABLE a (
       id    serial
     , curid int  -- UNIQUE?
     , cuid  int
     , rtid  int
    );
    
    CREATE TEMP TABLE b (
       id    serial
     , curid int
     , cd    date
     , dd    date
     , rm    text
    );
    

    用于DISTINCT ON表A:

    INSERT INTO a (curid, cuid, rtid)
    SELECT DISTINCT ON (cuid, rtid)
           curid, cuid, rtid
    FROM   tmp
    ORDER  BY  cuid, rtid, curid  -- pick smallest curid per group
    RETURNING *; 
    
    编号 | 咖喱 | CUID | 实时标识符
    -: | ----: | ---: | ---:
     1 | 10 | 4 | 4个
     2 | 8 | 5 | 3个
     3 | 11 | 6 | 4个
     4 | 9 | 7 | 3个
    

    详细解释在这里:

    • 选择每个 GROUP BY 组中的第一行?

    对表 B使用一个简单的窗口函数:

    INSERT INTO b (curid, cd, dd, rm)
    SELECT min(curid) OVER (PARTITION BY cuid, rtid), cd, dd, rm
    FROM   tmp
    ORDER  BY cuid, rtid  -- optional
    RETURNING *; 
    
    编号 | 咖喱 | 光盘 | 日 | R M            
    -: | ----: | :-------- | :-------- | :------------
     1 | 10 | 2016-01-02 | 2016-07-02 | 无效的          
     2 | 10 | 2016-06-12 | 2016-12-12 | 备注 6月12日
     3 | 8 | 2016-07-18 | 2017-07-31 | 无效的          
     4 | 8 | 2015-06-21 | 2016-06-30 | 添加一些测试
     5 | 11 | 2017-01-01 | 2017-07-01 | 空          
     6 | 9 | 2017-01-01 | 2018-01-31 | 无效的          
    

    curid保证匹配,因为我们在两个查询中都选择了每组中最小的一个。

    dbfiddle在这里

    • 3
  2. U01SFA3
    2017-07-05T05:50:21+08:002017-07-05T05:50:21+08:00

    我想你要找的是dense_rank()

    create table tmp (curid int, cuid int, rtid int) ;
    
    insert into temp
    values
        (176,231,23),
        (192,231,23),
        (145,231,23),
        (122,231,23),
        (122,231,24),
        (122,231,25) ;
    
    select dense_rank() over (order by cuid, rtid) as curid,
           cuid, 
           rtid
    from temp ;
    
    • 1
  3. Robert Carnegie
    2017-07-06T00:13:05+08:002017-07-06T00:13:05+08:00

    澄清一下,我在 Microsoft SQL 中要做的是,

    SELECT cuid, rtid, IDENTITY(int, 1, 1) AS curid,
    COUNT(*) AS how_many
    INTO another_table
    FROM original_table 
    GROUP BY cuid, rtid
    ORDER BY cuid, rtid
    

    这会创建一个包含 (cuid, rtid) 的每个组合的新表 [another_table],在新表中有一个行 id。

    现在,其中有多少适用于 PostgreSQL?

    • 0

相关问题

  • 我可以在使用数据库后激活 PITR 吗?

  • 运行时间偏移延迟复制的最佳实践

  • 存储过程可以防止 SQL 注入吗?

  • PostgreSQL 中 UniProt 的生物序列

  • PostgreSQL 9.0 Replication 和 Slony-I 有什么区别?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目

    • 12 个回答
  • Marko Smith

    如何让sqlplus的输出出现在一行中?

    • 3 个回答
  • Marko Smith

    选择具有最大日期或最晚日期的日期

    • 3 个回答
  • Marko Smith

    如何列出 PostgreSQL 中的所有模式?

    • 4 个回答
  • Marko Smith

    列出指定表的所有列

    • 5 个回答
  • Marko Smith

    如何在不修改我自己的 tnsnames.ora 的情况下使用 sqlplus 连接到位于另一台主机上的 Oracle 数据库

    • 4 个回答
  • Marko Smith

    你如何mysqldump特定的表?

    • 4 个回答
  • Marko Smith

    使用 psql 列出数据库权限

    • 10 个回答
  • Marko Smith

    如何从 PostgreSQL 中的选择查询中将值插入表中?

    • 4 个回答
  • Marko Smith

    如何使用 psql 列出所有数据库和表?

    • 7 个回答
  • Martin Hope
    Jin 连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目 2014-12-02 02:54:58 +0800 CST
  • Martin Hope
    Stéphane 如何列出 PostgreSQL 中的所有模式? 2013-04-16 11:19:16 +0800 CST
  • Martin Hope
    Mike Walsh 为什么事务日志不断增长或空间不足? 2012-12-05 18:11:22 +0800 CST
  • Martin Hope
    Stephane Rolland 列出指定表的所有列 2012-08-14 04:44:44 +0800 CST
  • Martin Hope
    haxney MySQL 能否合理地对数十亿行执行查询? 2012-07-03 11:36:13 +0800 CST
  • Martin Hope
    qazwsx 如何监控大型 .sql 文件的导入进度? 2012-05-03 08:54:41 +0800 CST
  • Martin Hope
    markdorison 你如何mysqldump特定的表? 2011-12-17 12:39:37 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 对 SQL 查询进行计时? 2011-06-04 02:22:54 +0800 CST
  • Martin Hope
    Jonas 如何从 PostgreSQL 中的选择查询中将值插入表中? 2011-05-28 00:33:05 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 列出所有数据库和表? 2011-02-18 00:45:49 +0800 CST

热门标签

sql-server mysql postgresql sql-server-2014 sql-server-2016 oracle sql-server-2008 database-design query-performance sql-server-2017

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve