SQL - 案例中的选择
SQL - Selection within Case when
我有一个代码可以为 "moving" 宇宙产生所需的输出,它查看每个时期并给我 information.I 希望有另一个产生相同输出的查询但是对于不同的选择,"current" 宇宙。
当前的 universe 会像现在一样计算总和,但只针对在数据库中最新日期符合标准的公司。
现在,对于 2015 年 Market_Cap 介于 0 和 10000 之间的公司,代码会产生 EBIT/Sales 2015 等等,但我宁愿希望它为那些公司产生 EBIT/Sales 2015当前 Market_Cap 在 0 到 10000 之间(Market_Cap 中的最新 date_month)
我正在使用 Microsoft SQL Server Management Studio
我尝试在 sum(case when ... then) 语法中插入另一个条件作为 "And c.company_id in(Select company_id from 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;
正确输出:
您在此处包含的唯一代码是选择代码,而不是实际根据市值进行筛选的代码。您不想更改这些行吗:
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)
?
进行比较
正确查询(我认为):
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;
我有一个代码可以为 "moving" 宇宙产生所需的输出,它查看每个时期并给我 information.I 希望有另一个产生相同输出的查询但是对于不同的选择,"current" 宇宙。
当前的 universe 会像现在一样计算总和,但只针对在数据库中最新日期符合标准的公司。
现在,对于 2015 年 Market_Cap 介于 0 和 10000 之间的公司,代码会产生 EBIT/Sales 2015 等等,但我宁愿希望它为那些公司产生 EBIT/Sales 2015当前 Market_Cap 在 0 到 10000 之间(Market_Cap 中的最新 date_month)
我正在使用 Microsoft SQL Server Management Studio
我尝试在 sum(case when ... then) 语法中插入另一个条件作为 "And c.company_id in(Select company_id from 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;
正确输出:
您在此处包含的唯一代码是选择代码,而不是实际根据市值进行筛选的代码。您不想更改这些行吗:
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)
?
正确查询(我认为):
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;