小编Ran*_*ize的帖子

自连接的替代方案

我在这里问了一个问题:https : //stackoverflow.com/questions/43807566/how-to-divide-two-values-from-the-same-column-but-at-different-rows

关于将同一表中同一列但不同行的值进行除法。现在我的问题是我有更多的分子和分母(不同的uns)。仍然是self join用 Postgres 解决这个问题的好方法还是有更好的解决方案?

例子:

| postcode | value | uns |
|----------|-------|-----|
|       AA |    40 |  53 |
|       BB |    20 |  53 |
|       AA |    10 |  54 |
|       AA |    20 |  55 |
|       AA |    10 |  56 |
|       AA |    30 |  57 |
|       AA |    50 |  58 |
|       BB |    10 |  54 |
|       BB | …
Run Code Online (Sandbox Code Playgroud)

postgresql pivot computed-column

10
推荐指数
2
解决办法
2121
查看次数

使用 Postgres 将值累积到数组中

目前我有这个查询:

select 
    sum(case when my_table.property_type = 'FLAT' then my_table.price else 0 end) as Flat_Current_Asking_Price,
    sum(case when my_table.property_type_mapped = 'SEMIDETACHED' then my_table.price else 0 end) as Semidetached_Current_Asking_Price
from 
    my_table;
Run Code Online (Sandbox Code Playgroud)

所以如果my_table有以下值:

property_type | price
--------------+-------
FLAT          | 5000
SEMIDETACHED  | 9000
FLAT          | 6000
Run Code Online (Sandbox Code Playgroud)

查询将返回:

Flat_Current_Asking_Price | Semidetached_Current_Asking_Price
--------------------------+-----------------------------------
11000                     | 9000
Run Code Online (Sandbox Code Playgroud)

如何替换将sum值累积到数组中以获取?

Flat_Current_Asking_Price | Semidetached_Current_Asking_Price
--------------------------+-----------------------------------
{5000, 6000}              | {9000}
Run Code Online (Sandbox Code Playgroud)

postgresql aggregate array aggregate-filter

6
推荐指数
1
解决办法
4625
查看次数

使用 Postgres 在数组上映射函数

Postgres 中是否有任何方法可以将函数应用于数组(或任何类型的集合)的结果?

例如,如果我有一个create function add1...将数字加一的函数,有什么办法可以做到:

select map add1 array[1,5,8...];
Run Code Online (Sandbox Code Playgroud)

回来

array [2,6,9...]
Run Code Online (Sandbox Code Playgroud)

?

我不确定它是否对 a 有任何意义,select因为它可以直接将函数映射到单个结果上:

select add1(x) from X;
Run Code Online (Sandbox Code Playgroud)

另外,这里有懒惰的概念吗?所有功能都将“严格”应用?

postgresql

6
推荐指数
1
解决办法
5883
查看次数

使用上个月的值填充缺失的日期

我有一个这样的表:

CREATE TABLE my_data (label text, value integer, date date);
INSERT INTO my_data (label, value, date) VALUES 
('AAA', 10, '2014-06-01'),
('AAA', 30, '2014-09-01'),
('AAA', 40, '2014-10-01'),
('AAA', 50, '2015-02-01'),
('BBB', 20, '2014-11-01'),
('BBB', 10, '2015-02-01'),
('BBB', 70, '2015-04-01');
Run Code Online (Sandbox Code Playgroud)

我需要以这种方式填写缺失的日期(想象一个时间序列):

label | value | date
------+-------+------------
AAA   | 10    | 2014-06-01
AAA   | 10    | 2014-07-01
AAA   | 10    | 2014-08-01
AAA   | 30    | 2014-09-01
AAA   | 40    | 2014-10-01
AAA   | 40    | 2014-11-01
AAA   | 40    | 2014-12-01 …
Run Code Online (Sandbox Code Playgroud)

postgresql

5
推荐指数
1
解决办法
4405
查看次数

使用 Postgres 继承缺失值的长序列

我有一张这样的表:

create table foo (foo_label text, foo_price int, foo_date date);

insert into foo (
          values
          ('aaa', 100,  '2017-01-01'),
          ('aaa', NULL, '2017-02-01'),
          ('aaa', NULL, '2017-03-01'),
          ('aaa', NULL, '2017-04-01'),
          ('aaa', 140,  '2017-05-01'),
          ('aaa', NULL, '2017-06-01'),
          ('aaa', 180,  '2017-07-01')
        );
Run Code Online (Sandbox Code Playgroud)

如您所见,foo_price列上的一些值丢失了。

我需要的是缺失值以这种方式用“前一个”可用值填充:

 foo_label | fixed_foo_price | foo_date
-----------+-----------------+------------
 aaa       | 100             | 2017-01-01
 aaa       | 100             | 2017-02-01
 aaa       | 100             | 2017-03-01
 aaa       | 100             | 2017-04-01
 aaa       | 140             | 2017-05-01
 aaa       | 140             | 2017-06-01
 aaa       | 180 …
Run Code Online (Sandbox Code Playgroud)

postgresql null count window-functions gaps-and-islands

5
推荐指数
1
解决办法
2225
查看次数

在 Postgres 上参数化物化视图的好方法是什么?

例如,给出这样的物化视图(Postgres 10.3):

create materialized view my_view as
    select * from my_table where sell_date < '2018-03-01';
Run Code Online (Sandbox Code Playgroud)

sell_date比较值('2018-03-01')有时可以改变,但我想,以避免掉落,每次重新创建物化视图。我想出的唯一想法是使用带有一些元数据值的外部表,例如所需的日期:

create materialized view my_view as
    select * from my_table where sell_date < 
        (select original_sell_date from some_metadata_table);
Run Code Online (Sandbox Code Playgroud)

有没有其他方法可以解决 Postgres 上物化视图的这种限制?

我当前解决方案的问题之一是您可以有两个或多个使用相同值的物化视图,但它们可能需要在某些点使用不同的值。在这种情况下,需要复制元数据表。

postgresql materialized-view

5
推荐指数
1
解决办法
1106
查看次数

使用 Postgres 在多行中投影时间范围

我有一些这样的行:

 avg_income | date_from  | date_to
------------+------------+------------
     1256.9 | 2016-11-21 | 2017-07-10
     4383.2 | 2017-05-06 | 2017-06-01
Run Code Online (Sandbox Code Playgroud)

我想将这些avg_income值投影到相应的月份,如下所示:

 avg_income | month
------------+------------
     1256.9 | 2016-11-01 
     1256.9 | 2016-12-01 
     1256.9 | 2017-01-01 
     1256.9 | 2017-02-01 
     1256.9 | 2017-03-01 
     1256.9 | 2017-04-01 
     1256.9 | 2017-05-01 
     1256.9 | 2017-06-01 
     1256.9 | 2017-07-01 
     4383.2 | 2017-05-01 
     4383.2 | 2017-06-01 
Run Code Online (Sandbox Code Playgroud)

到目前为止,我刚刚提出了一个可怕的问题,FOR/LOOP/generate_series(...)所以我想知道有没有更优雅(和有效)的方法来做到这一点?

postgresql

0
推荐指数
1
解决办法
65
查看次数