创建一个视图,其中包含来自六个不同记录的每一行数据[PostgreSQL]

时间:2016-02-13 22:54:22

标签: sql postgresql view sql-function

我有一张包含加油站价格的表格。每个加油站有六种不同燃料类型(柴油,常规,超级)和服务类型(自动服务,全面服务)的价格记录。例如,我有" id_gas_station"加油站的价格记录。表格中的= 155" gas_station_prices"

id_price|price_gas_station|id_gas_station|id_fuel_type|id_service_type
3041    |2.29             |155           |2           |1
2631    |2.52             |155           |1           |1
3861    |2.43             |155           |1           |2    
4681    |1.84             |155           |3           |2    
3451    |1.93             |155           |3           |1    
4271    |2.2              |155           |2           |2    

我有两个目录,其中包含燃料类型和服务类型

fuel_type目录:

id_fuel_type |name_fuel_type
1            |SUPER
2            |REGULAR
3            |DIESEL

service_type目录:

id_service_type |name_service_type
1               |FULL SERVICE
2               |AUTO SERVICE

我需要创建一个SQL VIEW,其中包含该列的六个记录价格的并集" price_gas_station"每个gas_station的单行如下:

id_gas_station|fs_sup_p |fs_reg_p |fs_die_p |as_sup_p |as_reg_p|as_die_p|
155           |2.52     |2.29     |1.93     |2.43     |2.2     |1.84

2 个答案:

答案 0 :(得分:0)

您可以使用条件聚合执行此操作:

SELECT id_gas_station,
       MAX(CASE WHEN id_fuel_type = 1 AND 
                     id_service_type = 1 
                THEN price_gas_station 
           END) AS fs_sup_p, 
       MAX(CASE WHEN id_fuel_type = 2 AND 
                     id_service_type = 1
                THEN price_gas_station 
           END) AS fs_reg_p, 
       MAX(CASE WHEN id_fuel_type = 3 AND 
                     id_service_type = 1
                THEN price_gas_station 
           END) AS fs_die_p, 
       ... etc
FROM mytable
GROUP BY id_gas_station

答案 1 :(得分:0)

以下是使用CROSSTAB(http://www.postgresql.org/docs/current/static/tablefunc.html

的另一种方法
SELECT *
FROM crosstab(
'
SELECT
    gsp.id_gas_station,
    LOWER(LEFT(st.name_service_type,4) || ft.name_fuel_type) AS price_type,
    gsp.price_gas_station
FROM
    gas_station_prices gsp
JOIN
    fuel_type ft
    ON  (ft.id_fuel_type = gsp.id_fuel_type)
JOIN
    service_type st
    ON  (st.id_service_type = gsp.id_service_type)
ORDER BY
    1,2
'
) AS ct (
id_gas_station INT,
auto_diesel FLOAT,
auto_regular FLOAT,
auto_super FLOAT,
full_diesel FLOAT,
full_regular FLOAT,
full_super FLOAT
);

Giorgos解决方案应该同样好 - 也许更具可读性?这在很大程度上是一个偏好的问题。我只想给你看一个替代方案。