使用WHERE和AS从DB中提取数据

时间:2018-05-23 07:02:35

标签: sql postgresql where

我有一张表格,其中包含不同年份的建筑物的模拟数据,每年只有一行。列标题是

id / year / avg_heat_demand / renovation_level / co2_emission / [and some more]

我现在想创建一个只有avg_heat_demand的表作为库存的几年,比如

id / avg_heat_demand_2015 / avg_heat_demand_2025 / avg_heat_demand_2050

我知道我想说的是什么,但我不能把SQL语句按正确的顺序排列。我尝试过(例如,我知道这不是正确的方法,但它可以解释我想要做的最好的事情):

CREATE TABLE output_time AS

SELECT id, 
       year, 
       (avg_heat_demand WHERE year = 2015) AS avg_heat_demand_m2_2015, 
       (avg_heat_demand WHERE year = 2050) AS avg_heat_demand_m2_2025, 
       (avg_heat_demand WHERE year = 2050) AS avg_heat_demand_m2_2050 
FROM simulation_output;

示例数据:

id | year | avg_heat_demand | etc
----+-----+-----------------+----
11 | 2015 | 55              +
12 | 2015 | 40              +
11 | 2016 | 48              +
12 | 2016 | 49              +
11 | 2025 | 45              +
12 | 2025 | 43              +
11 | 2050 | 50              +
12 | 2050 | 52              +

我想要的结果:

id | avg_heat_demand_2015 | avg_heat_demand_2025 | avg_heat_demand_2050 
---+----------------------+----------------------+---------------------
11 | 55                   | 45                   | 50
12 | 40                   | 43                   | 52

2 个答案:

答案 0 :(得分:1)

根据这些数据:

 id | avg_heat_demand | year 
----+-----------------+------
 11 |              55 | 2015
 12 |              40 | 2015
 11 |              45 | 2025
 12 |              43 | 2025
 11 |              50 | 2050
 12 |              52 | 2050
(6 Zeilen)

尝试使用子查询:

SELECT
(SELECT AVG(avg_heat_demand) FROM simulation_output WHERE year = 2015) AS avg_2015,
(SELECT AVG(avg_heat_demand) FROM simulation_output WHERE year = 2025) AS avg_2025,
(SELECT AVG(avg_heat_demand) FROM simulation_output WHERE year = 2050) AS avg_2050;

      avg_2015       |      avg_2025       |      avg_2050       
---------------------+---------------------+---------------------
 47.5000000000000000 | 44.0000000000000000 | 51.0000000000000000
(1 Zeile)

如果您能够将结果分成多行,请尝试以下查询:

SELECT year,AVG(avg_heat_demand)
FROM simulation_output
GROUP BY year
ORDER BY year

 year |         avg         
------+---------------------
 2015 | 47.5000000000000000
 2025 | 44.0000000000000000
 2050 | 51.0000000000000000
(3 Zeilen)

编辑:基于OP的编辑(不同的所需输出格式)

SELECT DISTINCT i.id, 
(SELECT avg_heat_demand FROM simulation_output j WHERE year=2015 AND j.id = i.id) AS avg_heat_demand_2015,
(SELECT avg_heat_demand FROM simulation_output j WHERE year=2025 AND j.id = i.id) AS avg_heat_demand_2025,
(SELECT avg_heat_demand FROM simulation_output j WHERE year=2050 AND j.id = i.id) AS avg_heat_demand_2050
FROM simulation_output i

 id | avg_heat_demand_2015 | avg_heat_demand_2025 | avg_heat_demand_2050 
----+----------------------+----------------------+----------------------
 11 |                   55 |                   45 |                   50
 12 |                   40 |                   43 |                   52
(2 Zeilen)

答案 1 :(得分:0)

我知道使用交叉表功能找到了一个洗脱液:

SELECT * 
FROM crosstab('select id, year, avg_heat_demand FROM simulation_output order by 1,2') 
AS final_result(id character varying, avg_heat_demand_m2_2015 double precision, avg_heat_demand_m2_2025 double precision, avg_heat_demand_m2_2050 double precision);

http://www.vertabelo.com/blog/technical-articles/creating-pivot-tables-in-postgresql-using-the-crosstab-function

上描述