2017-07-13 13 views
0

"移動する"宇宙のための望ましい出力を得るコードがあります。この宇宙では、各ピリオドを見て、私に情報を与えます。同じ出力が得られますが、異なる選択のために、「現在の」宇宙。SQL - ケース内の選択

現在のユニバースは、今のように合計を行いますが、データベースの最新の日付に条件を満たす企業にのみ適用されます。

今のところ、Market_Capが0〜10000 2015の企業ではEBIT/Sales 2015が得られますが、MarketCapが現在0〜10000の企業(EBIT/Sales 2015) Market_Capでdate_month)

私は、私は別の合計で基準(ケース...その後)「などの構文とc.company_idを挿入しようとした

(のcompany_idを選択

のMicrosoft SQL Server Management Studioを使用していますMarket_capからMarket_Capを0〜10000、Date = '2017-06-30))、エラーが表示されます。

集約関数またはサブクエリを含む式で集計関数を実行できません。今

コード:

select m.date_month  
    ,sum(case when y.date_year = 2015 AND c.Country_Id in (4,5,6) AND c.factSet_Level1_Id between 1 and 100 AND mm.Market_Cap between 0 and 10000 then n.EBIT end)/sum(case when y.date_year = 2015 AND c.Country_Id in (4,5,6) AND c.factSet_Level1_Id between 1 and 100 AND mm.Market_Cap between 0 and 10000 then s.Sales end) as 'EBIT/Sales 2015' 
    ,sum(case when y.date_year = 2016 AND c.Country_Id in (4,5,6) AND c.factSet_Level1_Id between 1 and 100 AND mm.Market_Cap between 0 and 10000 then n.EBIT end)/sum(case when y.date_year = 2016 AND c.Country_Id in (4,5,6) AND c.factSet_Level1_Id between 1 and 100 AND mm.Market_Cap between 0 and 10000 then s.Sales end) as 'EBIT/Sales 2016' 
    ,sum(case when y.date_year = 2017 AND c.Country_Id in (4,5,6) AND c.factSet_Level1_Id between 1 and 100 AND mm.Market_Cap between 0 and 10000 then n.EBIT end)/sum(case when y.date_year = 2017 AND c.Country_Id in (4,5,6) AND c.factSet_Level1_Id between 1 and 100 AND mm.Market_Cap between 0 and 10000 then s.Sales end) as 'EBIT/Sales 2017' 
from EBIT as n 
    inner join Sales as s on s.company_id = n.company_id 
          and s.date_month_id = n.date_month_id 
          and s.date_year_id = n.date_year_id 
    inner join date_year as y on y.date_year_id = n.date_year_id 
    inner join date_month as m on m.date_month_id = n.date_month_id 
    inner join Market_Cap as mm on mm.Date_Month_Id = n.Date_Month_Id 
          and mm.Company_Id = n.Company_Id 
    inner join Company as c on c.Company_Id = n.Company_Id 
where y.date_year between 2015 and 2017  
    and n.EBIT<> 0 
    and s.Sales<> 0 
group by m.date_month; 

正しい出力:

答えて

1

あなたはここに含まれる選択コードではなく、実際には時価総額に基づいてフィルタリングしたコードだったコードのみ。 max(Date_Month_Id)のようなものにmm.Date_Month_Idを比較する

inner join Market_Cap as mm on mm.Date_Month_Id = n.Date_Month_Id 
         and mm.Company_Id = n.Company_Id 

:あなたはこれらの行を変更したいと思いませんか?

+0

は、私は...私はSQLの原則を忘れてしまったサム(ケース...その後)構文に巻き込まになった、それが動作ありがとう今 –

0

正しいクエリ(と思う):

select m.date_month  
    ,sum(case when y.date_year = 2015 AND c.Country_Id in (4,5,6) AND c.factSet_Level1_Id between 1 and 100 then n.EBIT end)/sum(case when y.date_year = 2015 AND c.Country_Id in (4,5,6) AND c.factSet_Level1_Id between 1 and 100 then s.Sales end) as 'EBIT/Sales 2015' 
    ,sum(case when y.date_year = 2016 AND c.Country_Id in (4,5,6) AND c.factSet_Level1_Id between 1 and 100 then n.EBIT end)/sum(case when y.date_year = 2016 AND c.Country_Id in (4,5,6) AND c.factSet_Level1_Id between 1 and 100 then s.Sales end) as 'EBIT/Sales 2016' 
    ,sum(case when y.date_year = 2017 AND c.Country_Id in (4,5,6) AND c.factSet_Level1_Id between 1 and 100 then n.EBIT end)/sum(case when y.date_year = 2017 AND c.Country_Id in (4,5,6) AND c.factSet_Level1_Id between 1 and 100 then s.Sales end) as 'EBIT/Sales 2017' 
from EBIT as n 
    inner join Sales as s on s.company_id = n.company_id 
          and s.date_month_id = n.date_month_id 
          and s.date_year_id = n.date_year_id 
    inner join date_year as y on y.date_year_id = n.date_year_id 
    inner join date_month as m on m.date_month_id = n.date_month_id 
    inner join Market_Cap as mm on mm.Date_Month_Id = n.Date_Month_Id 
          and mm.Company_Id = n.Company_Id 
    inner join Company as c on c.Company_Id = n.Company_Id 
where y.date_year between 2015 and 2017 and c.Company_Id in (Select Company_Id from Market_Cap Where Market_Cap between 0 and 1000) 
    and n.EBIT<> 0 
    and s.Sales<> 0 
group by m.date_month 
order by m.Date_Month asc; 
関連する問題