我有一个代码可以为"移动"产生所需的输出。它查看每个时期并给我信息的宇宙。我希望有另一个查询产生相同的输出但是对于不同的选择,一个"当前"宇宙。
当前的Universe会像现在一样进行汇总,但仅适用于数据库中最新日期符合条件的公司。
目前,对于Market_Cap介于2015年0到10000之间的公司等,代码会产生EBIT / Sales 2015,但我宁愿希望它为目前市场营销介于0到10000之间的公司产生EBIT / Sales 2015(最新date_month在Market_Cap)
我正在使用Microsoft SQL Server Management Studio
我试图在总和中插入另一个标准(当时的情况......然后)语法为"并且c.company_id在(从Market_cap中选择company_id,其中Market_Cap介于0和10000之间,日期=& #39; 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;
正确输出:
答案 0 :(得分:1)
此处包含的唯一代码是选择代码,而不是基于市值实际过滤的代码。你想不想改变这些界限:
inner join Market_Cap as mm on mm.Date_Month_Id = n.Date_Month_Id
and mm.Company_Id = n.Company_Id
将mm.Date_Month_Id
与max(Date_Month_Id)
?
答案 1 :(得分: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;