2016-03-22 12 views
0

私は正しい結果を返すこのクエリを持っています。PostgreSQLの同じクエリから、YearカラムとDisplay Yearカラムを同じカラムから水平カラムに抽出します

SELECT distinct(companies.name), 
       date_part('year', scopes.time_stamp) as fy, 
       count(def.dp_code)as answ_count  
FROM companies companies, 
    scopes scopes, 
    values dp, 
    definition def, 
    content cd 
where scopes.company_id = companies.company_id 
    and scopes.scope_id=dp.scope_id 
    and dp.content_id=cd.definition_id 
    and def.definition_id=cd.definition_id 
group by companies.name, date_part('year',scopes.time_stamp) 
ORDER BY companies.name 

以下は上記のクエリの結果です。

name   fy   count 

3M Co  2002 200 
3M Co  2003 100 
3M Co  2004 150 
3M Co  2005 160 
3M Co  2006 169 
AB SKF  2002 212 
AB SKF  2003 214 
AB SKF  2004 215 
AB SKF  2005 237 
AB SKF  2006 456 
3i Group plc 2002 546 
3i Group plc 2003 214 
3i Group plc 2004 215 
3i Group plc 2005 237 
3i Group plc 2006 456 

私は水平に、このような結果を表示する必要があります:修正クエリ

name   2002 2003 2004 2005 2006 
3M Co  200 100 150 160 169 
AB SKF   
3i Group plc 546 214 215 237 456 

しかし、私は正しいcount.Iは、そのために私を助けてans_count.Pleaseを計算する方法を知らない得ていないのです。派生テーブルで

SELECT 
    distinct(companies.name), 
    count(CASE WHEN date_part('year', scopes.time_stamp) = 2002 THEN answ_count end) AS Year1, 
    count(CASE WHEN date_part('year', scopes.time_stamp) = 2003 THEN answ_count end) AS Year2, 
    count(CASE WHEN date_part('year', scopes.time_stamp) = 2004 THEN answ_count end) AS Year3, 
    count(CASE WHEN date_part('year', scopes.time_stamp) = 2005 THEN answ_count end) AS Year4, 
    count(CASE WHEN date_part('year', scopes.time_stamp) = 2006 THEN answ_count end) AS Year5, 
    count(CASE WHEN date_part('year',scopes.time_stamp) = 2007 THEN answ_count end) AS Year6, 
    count(CASE WHEN date_part('year', scopes.time_stamp) = 2008 THEN answ_count end) AS Year7, 
    count(CASE WHEN date_part('year', scopes.time_stamp) = 2009 THEN answ_count end) AS Year8, 
    count(CASE WHEN date_part('year', scopes.time_stamp) = 2010 THEN answ_count end) AS Year9, 
    count(CASE WHEN date_part('year', scopes.time_stamp) = 2011 THEN answ_count end) AS Year10, 
    count(CASE WHEN date_part('year', scopes.time_stamp) = 2012 THEN answ_count end) AS Year11, 
    count(CASE WHEN date_part('year', scopes.time_stamp) = 2013 THEN answ_count end) AS Year12, 
    count(CASE WHEN date_part('year', scopes.time_stamp) = 2014 THEN answ_count end) AS Year13, 
    count(CASE WHEN date_part('year', scopes.time_stamp) = 2015 THEN answ_count end) AS Year14, 
    count(CASE WHEN date_part('year', scopes.time_stamp) = 2016 THEN answ_count end) AS Year16 
    FROM 
    companies companies,value_scopes value_scopes,dp_values dp,dp_definition def,dp_content_definition cd 
    where 
    value_scopes.company_id = companies.company_id 
    and value_scopes.value_scope_id=dp.value_scope_id 
    and dp.dp_content_definition_id=cd.dp_content_definition_id 
    and def.dp_definition_id=cd.dp_definition_id 
    and value_scopes.is_partial='f' 
    group by companies.name 
    ORDER BY companies.name; 
+0

DISTINCTが(列の)関数ではありません、それは 'SELECT DISTINCT'の一部だし、作品選択された行全体に適用されます。すなわち、SELECT DISTINCT c1、c2' eq 'SELECT DISTINCT c1、(c2)'を選択する。 – jarlh

+0

現代的で明示的なJOIN構文に切り替えます。 (エラーなしで)書きやすく、読みやすく(維持しやすい)、必要に応じて外部結合に変換する方が簡単です。 – jarlh

+0

あなたの提案を歓迎します...そのようなちょっとしたディスプレイのクエリを構成する方法はわかりません。 – SUDARSHAN

答えて

0

現代、明示的なJOIN年代、年-もの:

select name, 
     count(case when year = 2002 then 1 end) as Year2002, 
     count(case when year = 2003 then 1 end) as Year2003, 
     ... 
     count(case when year = 2016 then 1 end) as Year2016 
FROM 
(
    SELECT companies.name, 
      date_part('year', scopes.time_stamp) AS Year 
    FROM companies companies 
    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 
    WHERE value_scopes.is_partial = 'f' 
) dt 
group by name 
ORDER BY name 
+0

このクエリでも同じ結果が返されますが、クエリの方がはっきりしていますが、結果はまだ間違っています.Yearの賢明なカウントはまだ間違っています。このクエリの年間賢明なカウントは計算されていません。 – SUDARSHAN

+0

ロット。 – SUDARSHAN

+0

今年はハードコードされているので、同じクエリから年を出してその年を渡してください。アドバンスでお礼します。 – SUDARSHAN

関連する問題