如何在 PostgreSQL 函数中组合两个选择?

丹尼·埃利斯早年经历

Union 因 JSON 列而崩溃,显然,临时表在 PostgreSQL 函数中几乎没有用。

我来自 MS SQL Server 世界,似乎我在 PostgreSQL 中尝试的一切都是错误的。这就是我想要做的。有人可以告诉我最好的方法吗?或者有什么办法可以做到这一点?

create or replace function getNextStories( previous_id bigint ) 
returns setof output_stories as
$$
    create temp table output_stories (
        "id"            bigint,
        submitted_date  timestamp with time zone,
        author          character varying,
        title           character varying,
        "content"       json,
        pinned          boolean
    );

    insert into output_stories("id", submitted_date, author, title, "content", pinned)
    select "id", submitted_date, categories, author, title, "content", pinned 
    from Story 
    where Pinned = true;

    insert into output_stories("id", submitted_date, author, title, "content", pinned)
    select "id", submitted_date, categories, author, "content", title, pinned 
    from Story 
    where Pinned = false
           and "id" < previous_id
    order by "id" desc
    limit 20;                     

    select * from output_stories
$$
language 'sql' ;

我尝试了很多不同的事情,每次我都会遇到错误。这个特殊的尝试返回了这个。

ERROR:  type "output_stories" does not exist
********** Error **********

ERROR: type "output_stories" does not exist
SQL state: 42704
巴伦

如果我正确理解您的用例,您想选择所有固定的故事和一些参考 ID 后的(按 ID)前 20 个非固定的故事。除非我忽略了您问题中的某些内容,否则在我看来您根本不需要存储函数:

(
    SELECT id, title, body, pinned FROM Story 
    WHERE (id > 4 and not pinned) ORDER BY id DESC LIMIT 20
)
UNION ALL
SELECT id, title, body, pinned FROM Story WHERE pinned;

我用一堆测试做了一个sqlfiddle以供参考:

-- minimal example table
CREATE TABLE story (
  id BIGINT PRIMARY KEY,
  title VARCHAR,
  body JSON,
  pinned BOOLEAN
)

-- minimal example records
INSERT INTO story (id, title, pinned) VALUES
(1, 'pinned', true),
(2, 'not pinned', false),
(4, 'previous id', false),
(5, 'next 1', false),      
(6, 'next 2', false),      
(7, 'next 3', true),       
(8, 'next 4', false);   

-- expectation
-- 1 INCLUDE (pinned)
-- 2 OMIT    (before ref ID)
-- 4 OMIT    (is ref ID)
-- 5 OMIT    (after ref ID, but outside limit 2)
-- 6 INCLUDE (after ref ID, not pinned)
-- 7 INCLUDE (pinned, but don't duplicate!)
-- 8 INCLUDE (after ref ID, not pinned)

-- Query
(
  SELECT id, title, body, pinned FROM Story 
  WHERE (id > 4 and not pinned) ORDER BY id DESC LIMIT 2
)
UNION ALL
  SELECT id, title, body, pinned FROM Story WHERE pinned;

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在Postgresql中将两个选择查询水平组合在同一张表上?

如何在PostgreSQL中通过COLUMN合并/合并两个选择查询?

如何在postgresql中与特定ID相关的两个日期时间之间选择数据

如何在两个hstore postgresql中连接值?

如何在 PostgreSQL 中连接两个表?

如何在R中组合两个函数图?

如何在Python中组合两个函数

Postgresql如何对同一个表中的两个选择使用联合?

如何在PostgreSQL中加入两个“未命名表/选择”?

在PostgreSQL的同一查询中如何使用两个SUM()聚合函数?

如何在PostgreSQL中匹配句子中的最后两个单词?

如何组合两个函数?

如何在 postgresql 中透视

如何从PostgreSQL中的两个IP获取CIDR?

如何在 PostgreSql 中多次选择单行

如何组合PostgreSQL中两列的值?

如何在 postgresql 函数中捕获异常?

如何在java中组合两个不同的Map?

如何在 JavaScript 中组合两个对象数组

如何在 Spark (Scala) 中组合两个 RDD?

如何在theano中组合两个张量

如何在oracle中合并/组合两个表

如何在 Racket 中组合两个功能?

我有两个使用 UNION 组合的选择查询。我在一列中得到两个结果如何在两列中得到它

PostgreSQL:如何合并两个查询

如何对 R 中两个矩阵中两列的每个可能组合应用函数

如何在MSSQL中将两个选择语句组合为一个选择语句?

如何在R中的组中的两个变量的组合上选择具有特定值的行

当其中一个只缺少一列时,如何在 postgresql 中合并两个表?