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 / 问题 / 206586
Accepted
Gajus
Gajus
Asked: 2018-05-14 02:33:17 +0800 CST2018-05-14 02:33:17 +0800 CST 2018-05-14 02:33:17 +0800 CST

如何有条件地将行附加到结果集中?

  • 772

我有一个复杂的查询表,用于识别和之间的SELECT关系。这是一个表达式的简化:event_idattributeVALUES

SELECT event_id, attribute
FROM (
  VALUES
    (1, '2D'),
    (1, 'IMAX'),
    (2, 'IMAX'),
    (3, '3D')
) event_attribute (event_id, attribute)

我想(event_id, '2D')为每个event_id尚未与3Dor2D属性关联的记录包含一个额外的记录。如何有条件地将行附加到结果集中?

在上表的情况下,预期结果将是:

(1, '2D'),
(1, 'IMAX'),
(2, 'IMAX'),
(2, '2D'),
(3, '3D')

还有一个表格event,每个相关的id.

postgresql
  • 4 4 个回答
  • 1822 Views

4 个回答

  • Voted
  1. Best Answer
    Erwin Brandstetter
    2018-05-14T05:26:06+08:002018-05-14T05:26:06+08:00

    假设(就像稍后添加的那样)一个event具有所有相关唯一性的单独表id- 这有助于提高性能:

    WITH cte(event_id, attribute) AS (
       -- big SELECT query goes here instead of the VALUES expression
       VALUES
        (1, '2D'),
        (1, 'IMAX'),
        (2, 'IMAX'),
        (3, '3D')
       )
    TABLE cte
    UNION ALL
    SELECT e.id, '2D'
    FROM   event e
    LEFT   JOIN cte ON cte.event_id = e.id
                   AND cte.attribute IN ('2D','3D')
    WHERE  cte.event_id IS NULL;
    

    有关的:

    • 选择其他表中不存在的行
    • 优化 GROUP BY 查询以检索每个用户的最新记录
    • SELECT * FROM 有快捷方式吗?

    如果您的查询仅返回 all 的一个子集event_id,则不能使用该表event来进行这样的简化。没有表的替代方案event:

    WITH cte AS (
       -- big SELECT query goes here instead of the VALUES expression
       VALUES
        (1, '2D'),
        (1, 'IMAX'),
        (2, 'IMAX'),
        (3, '3D')
       )
    TABLE cte
    UNION ALL
    SELECT event_id, '2D'
    FROM   cte
    GROUP  BY 1
    HAVING count(*) FILTER (WHERE attribute IN ('2D', '3D')) = 0;
    

    这与您自己回答的内容有些相似,只是更短且更快。特别是总FILTER条款应该是工具性的。有关的:

    • 对于绝对性能,SUM 更快还是 COUNT 更快?

    由于从 CTE 派生的表上没有索引,因此开始第二个查询可能会更快。

    • 5
  2. Gerard H. Pille
    2018-05-14T03:10:47+08:002018-05-14T03:10:47+08:00

    cfr。http://sqlfiddle.com/#!17/82868/6

    create table t as SELECT
      event_attribute.event_id,
      event_attribute.attribute
    FROM (
      VALUES
        (1, '2D'),
        (1, 'IMAX'),
        (2, 'IMAX'),
        (3, '3D')
    ) event_attribute (event_id, attribute);
    
    
    select E, '2D' a from (
      select distinct event_id E from t
       where not exists (
         select null from t t1
          where t1.event_id = t.event_id and t1.attribute = '3D')) X;
    

    对于较大的数据集可能更快:

    select E, '2D' a from (
      select distinct event_id E from t
      except
      select distinct event_id E from t
       where attribute = '3D') T2;
    

    见http://sqlfiddle.com/#!17/82868/14

    因为,根据已编辑的问题,最好避免重复的“2D”条目,“attribute = '3D'”应替换为“attribute in ('3D','2D')”。

    • 2
  3. Gajus
    2018-05-14T03:23:11+08:002018-05-14T03:23:11+08:00

    为了记录,这就是我想出的:

    WITH
      event_attribute AS
      (
        SELECT
          event_attribute.event_id,
          event_attribute.attribute
        FROM (
          VALUES
            (1, '2D'),
            (1, 'IMAX'),
            (2, 'IMAX'),
            (3, '3D')
        ) event_attribute (event_id, attribute)
      ),
      append_attribute_event AS
      (
        SELECT event_id
        FROM event_attribute
        GROUP BY event_id
        HAVING
          MAX(CASE "attribute" WHEN '2D' THEN 1 ELSE 0 END) = 0 AND
          MAX(CASE "attribute" WHEN '3D' THEN 1 ELSE 0 END) = 0
      )
    SELECT * FROM event_attribute
    UNION ALL
    SELECT event_id, '2D' "attribute"
    FROM append_attribute_event
    

    但是,我很确定杰拉德的建议更好。

    • 2
  4. Lennart - Slava Ukraini
    2018-05-14T05:22:32+08:002018-05-14T05:22:32+08:00

    一个直接的解决方案是:

    select event_id, attribute
    from event_attribute
    union all
    select event_id, '2D' 
    from event_attribute t1 
    where not exists (
        select 1 from event_attribute t2 
        where t1.event_id = t2.event_id 
          and t2.attribute in ('2D','3D')
    );
    
    • 1

相关问题

  • 我可以在使用数据库后激活 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