我在这里问了一个问题: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) 目前我有这个查询:
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) 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)
另外,这里有懒惰的概念吗?所有功能都将“严格”应用?
我有一个这样的表:
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) 我有一张这样的表:
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) 例如,给出这样的物化视图(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 上物化视图的这种限制?
我当前解决方案的问题之一是您可以有两个或多个使用相同值的物化视图,但它们可能需要在某些点使用不同的值。在这种情况下,需要复制元数据表。
我有一些这样的行:
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(...)
所以我想知道有没有更优雅(和有效)的方法来做到这一点?