SQL - 如何在POSTGRES中消除以下查询中的重复项

时间:2016-06-20 23:07:34

标签: sql postgresql duplicates pivotal-cloud-foundry

我一直在研究以下查询。基本上有两个表。 Realtime_Input和Realtime_Output。当我加入两个表并获取必要的列时,我将其作为一个视图,当我查询视图时,我得到重复。

我做错了什么?当我使用distinct关键字进行测试时,我获得了60个独特的行,但间歇性地我得到了重复。我的数据库是在云代工云(postgres)上。是因为那个吗?请帮忙!

select i2.key_ts_long, 
    case 
        when i2.revenue_activepower = 'NA' 
        then (-1 * CAST(io.min5_forecast as real)) 
        else (CAST(i2.revenue_activepower AS real) - CAST(io.min5_forecast as real)) 
    end as diff 
 from realtime_analytic_input i2, 
     (select i.farm_id, 
             i.key_ts_long, 
             o.min5_forecast, 
             o.min5_timestamp_seconds 
        from realtime_analytic_input i, 
             realtime_analytic_output o 
       where i.farm_id = o.farm_id 
         and i.key_ts_long = o.key_ts_long 
         and o.farm_id = 'MW1'
    ) io 
    where i2.key_ts_long = CAST(io.min5_timestamp_seconds AS bigint) 
      and i2.farm_id = io.farm_id 
      and i2.farm_id = 'MW1' 
      and io.key_ts_long between 1464738953169 and 1466457841 
    order by io.key_ts_long desc

0 个答案:

没有答案