如何在sql语句中复制记录

时间:2017-03-15 14:10:54

标签: sql postgresql case

我使用sql case语句(PostgreSQL)。如果有两种类型的速度,则应该重复该行。所以我有两行具有相同的id但速度值不同。

例如,我的查询结果是:

 id | speed    | special_speed       | type
----|----------|---------------------|--------
 1  |     180  |       90            |  0

现在我将有两行,包括速度值或特殊速度值,如:

id | speed    | type
---|----------|---------
 1 |  180     |  0
 1 |   90     |  0

这是我工作所必需的。我怎样才能在sql中复制行? 我必须对所有结果行执行此操作...

我使用这种陈述:

    SELECT   a.link_id,

    CASE
        WHEN a.From_Ref_Speed_Limit is null and a.To_Ref_Speed_Limit is not null THEN a.To_Ref_Speed_Limit
        WHEN a.From_Ref_Speed_Limit is not null and a.To_Ref_Speed_Limit is null THEN a.From_Ref_Speed_Limit
        WHEN a.From_Ref_Speed_Limit < a.To_Ref_Speed_Limit THEN a.To_Ref_Speed_Limit
        WHEN a.From_Ref_Speed_Limit > a.To_Ref_Speed_Limit THEN a.From_Ref_Speed_Limit
        WHEN a.From_Ref_Speed_Limit = a.To_Ref_Speed_Limit THEN a.To_Ref_Speed_Limit
        ELSE 0 --unknown
    END as SpeedLimit,

    CASE            
        WHEN a.speed_limit_source = '1' THEN 1 --explizit/posted
        WHEN a.speed_limit_source = '2' THEN 2 --implicit/derived
        ELSE 0 --unknown
    END as LegalType,

    CASE
        WHEN d.dependent_speed_type = 1 THEN 1
        WHEN d.dependent_speed_type = 2 THEN 2
        WHEN d.dependent_speed_type = 3 THEN 3
        WHEN d.dependent_speed_type = 7 THEN 4
        WHEN d.dependent_speed_type = 4 THEN 5
        ELSE 0 -- not related to special situations
    END as LimitType,

    CASE 
        WHEN g.automobiles = 'Y' AND g.trucks = 'Y' AND g.motorcycles = 'Y' THEN 0 
        WHEN g.automobiles = 'Y' AND g.trucks = 'N' AND g.motorcycles = 'N' THEN 1
        WHEN g.automobiles = 'N' AND g.trucks = 'Y' AND g.motorcycles = 'N' THEN 2 
        WHEN g.automobiles = 'N' AND g.trucks = 'N' AND g.motorcycles = 'Y' THEN 3
        ELSE -1
    END as vehicleTypes,

    CASE        
        WHEN d.dependent_speed_type = 4 THEN (SELECT t.time_domain FROM rdf_euw_2015_q4_here_7_10.rdf_time_domain t WHERE d.condition_id = t.feature_id)-- feature_id in RDF_Time_Domain represents condition_id in RDF_Condition
        ELSE ''
    END as timedomain

--INTO rdf_euw_2015_q4_here_7_10.speed_limits_test_LUX

FROM rdf_euw_2015_q4_here_7_10.rdf_nav_link a 


JOIN rdf_euw_2015_q4_here_7_10.rdf_nav_strand l ON a.link_id = l.link_id
JOIN rdf_euw_2015_q4_here_7_10.rdf_condition f ON l.nav_strand_id = f.nav_strand_id
JOIN rdf_euw_2015_q4_here_7_10.rdf_condition_speed d ON f.condition_id = d.condition_id
JOIN rdf_euw_2015_q4_here_7_10.rdf_access g ON f.access_id = g.access_id

因此,如果d.special_speed_limit不为空,则必须使用SpeedLimit列中的d.special_speed_limit值复制该行。添加联合声明的最佳位置在哪里?

1 个答案:

答案 0 :(得分:2)

最简单的方法就是union all

select id, speed, type
from t
union all
select id, special_speed, type
from t;

您可以通过添加where

来实现此条件
select id, speed, type
from t
union all
select id, special_speed, type
from t
where special_speed is not null;

在大多数情况下,这将满足两列的需求。但是,如果多次扫描表格是个问题,您可以这样做:

select id, unnest(array(t.speed, t.special_speed)) as speed, type
from t;

这使用Postgres的数组功能,因此表只扫描一次。

过滤此内容的最简单方法可能是:

select t.*
from (select id, unnest(array(t.speed, t.special_speed)) as speed, type
      from t
     ) t
where speed is not null;