sqlpivotpostgresql-9.1with-clause

missing FROM-clause entry for table in postgresql


I get this error when i run this query .please help me understand the error.But when i use count (*) i dont get this error.I modified the query and it is working but count i am not getting correct

select dt.name,dt.asset4_code,dt.org_id,dt.year_max,dt.oa_perm_id,
        count(CASE WHEN date_part('year', dt.time_stamp) = 2002 THEN 1 end) AS Year2002,
        count(CASE WHEN date_part('year', dt.time_stamp) = 2003 THEN 1 end) AS Year2003,
                count(CASE WHEN date_part('year', dt.time_stamp) = 2004 THEN 1 end) AS Year2004,
                count(CASE WHEN date_part('year', dt.time_stamp) = 2005 THEN 1 end) AS Year2005,
                count(CASE WHEN date_part('year', dt.time_stamp) = 2006 THEN 1 end) AS Year2006,
                count(CASE WHEN date_part('year', dt.time_stamp) = 2007 THEN 1 end) AS Year2007,
                count(CASE WHEN date_part('year', dt.time_stamp) = 2008 THEN 1 end) AS Year2008,
                count(CASE WHEN date_part('year', dt.time_stamp) = 2009 THEN 1 end) AS Year2009,
                count(CASE WHEN date_part('year', dt.time_stamp) = 2010 THEN 1 end) AS Year2010,
                count(CASE WHEN date_part('year', dt.time_stamp) = 2011 THEN 1 end) AS Year2011, 
                count(CASE WHEN date_part('year', dt.time_stamp) = 2012 THEN 1 end) AS Year2012,
                count(CASE WHEN date_part('year', dt.time_stamp) = 2013 THEN 1 end) AS Year2013,
                count(CASE WHEN date_part('year', dt.time_stamp) = 2014 THEN 1 end) AS Year2014,
                count(CASE WHEN date_part('year', dt.time_stamp) = 2015 THEN 1 end) AS Year2015 ,
                count(CASE WHEN date_part('year', dt.time_stamp) = 2016 THEN 1 end) AS Year2016

from
 (
    select companies.name,companies.asset4_code,companies.org_id,companies.year_max,companies.oa_perm_id,value_scopes.time_stamp,row_number(*)
    over (partition by sdp.dp_code order by sdp.dp_code desc ) as rn
     FROM 
                companies companies INNER JOIN value_scopes value_scopes ON value_scopes.company_id = companies.company_id
                JOIN dp_values dp ON value_scopes.value_scope_id=dp.value_scope_id
                JOIN dp_content_definition cd ON dp.dp_content_definition_id=cd.dp_content_definition_id 
                JOIN dp_definition def ON def.dp_definition_id=cd.dp_definition_id 
                right outer join strategic_data_point sdp on def.dp_code=sdp.dp_code 
                where 
        date_part('year', value_scopes.time_stamp)='2012'
                 and value_scopes.is_partial='f'
                and dp.no_answer='f'
                and (dp.dp_value_s IS  not  NULL or dp.dp_value_n IS  not  NULL)
                and companies.asset4_code='35'
                and def.dp_code like '%En%'

 ) dt 
where rn < 2  group by dt.name,dt.asset4_code,dt.org_id,dt.year_max,dt.oa_perm_id order by dt.name;

Solution

  • You use the wrong alias I think.

    Try replacing value_scopes.time_stamp by dt.time_stamp in your COUNT, line 2.