Sql server 在“中给出所有位置的方法”;“在什么情况下”;不在查询中列出它们?

Sql server 在“中给出所有位置的方法”;“在什么情况下”;不在查询中列出它们?,sql-server,Sql Server,我试图做一个“case-when”块,但是,有很多情况。除了列出所有的可能性,还有其他方法吗?(所有这3个字段的值都存储在我的数据库中) 现在我所做的是: left join [RISK].[dbo].[FILiquidityBuckets] FB7 on FB7.Metric='Industry sector/Group' and(((sm.scotiaIndustrySector='Corporate' and sm.scotiaIndustryGroup='Energy') and FB

我试图做一个“case-when”块,但是,有很多情况。除了列出所有的可能性,还有其他方法吗?(所有这3个字段的值都存储在我的数据库中)

现在我所做的是:

left join [RISK].[dbo].[FILiquidityBuckets] FB7
on FB7.Metric='Industry sector/Group'
and(((sm.scotiaIndustrySector='Corporate' and sm.scotiaIndustryGroup='Energy') and FB7.LiquidityScore=3)
or
((sm.scotiaIndustrySector='Corporate' and sm.scotiaIndustryGroup='Financial') and FB7.LiquidityScore=2)
or
((sm.scotiaIndustrySector='Corporate' and sm.scotiaIndustryGroup='Infrastructure') and FB7.LiquidityScore=3)
or
((sm.scotiaIndustrySector='Corporate' and sm.scotiaIndustryGroup='Communication') and FB7.LiquidityScore=3)
or
((sm.scotiaIndustrySector='Corporate' and sm.scotiaIndustryGroup='Securitization') and FB7.LiquidityScore=3)
or
((sm.scotiaIndustrySector='Corporate' and sm.scotiaIndustryGroup='Real Estate') and FB7.LiquidityScore=3)
or
((sm.scotiaIndustrySector='Corporate' and sm.scotiaIndustryGroup='Industrial') and FB7.LiquidityScore=3)
or
((sm.scotiaIndustrySector='Government' and sm.scotiaIndustryGroup='Provincial' and sm.scotiaIndustrySubGroup='Ontario') and FB7.LiquidityScore=1)
or
((sm.scotiaIndustrySector='Government' and sm.scotiaIndustryGroup='Provincial' and sm.scotiaIndustrySubGroup='Alberta') and FB7.LiquidityScore=2)
or
((sm.scotiaIndustrySector='Government' and sm.scotiaIndustryGroup='Federal' and sm.scotiaIndustrySubGroup='Agency') and FB7.LiquidityScore=2)
or
((sm.scotiaIndustrySector='Government' and sm.scotiaIndustryGroup='Provincial' and sm.scotiaIndustrySubGroup='New Brunswick') and FB7.LiquidityScore=3)
or
((sm.scotiaIndustrySector='Government' and sm.scotiaIndustryGroup='Provincial' and sm.scotiaIndustrySubGroup='Newfoundland') and FB7.LiquidityScore=3)
or
((sm.scotiaIndustrySector='Government' and sm.scotiaIndustryGroup='Provincial' and sm.scotiaIndustrySubGroup='British Columbia') and FB7.LiquidityScore=3)
or
((sm.scotiaIndustrySector='Government' and sm.scotiaIndustryGroup='Federal' and sm.scotiaIndustrySubGroup='Non-Agency') and FB7.LiquidityScore=1)
or
((sm.scotiaIndustrySector='Government' and sm.scotiaIndustryGroup='Municipal' and sm.scotiaIndustrySubGroup='Ontario') and FB7.LiquidityScore=2)
or
((sm.scotiaIndustrySector='Government' and sm.scotiaIndustryGroup='Provincial' and sm.scotiaIndustrySubGroup='Quebec') and FB7.LiquidityScore=1)
or
((sm.scotiaIndustrySector='Government' and sm.scotiaIndustryGroup='Provincial' and sm.scotiaIndustrySubGroup='Saskatchewan') and FB7.LiquidityScore=3)
or
((sm.scotiaIndustrySector='Government' and sm.scotiaIndustryGroup='Provincial' and sm.scotiaIndustrySubGroup='Manitoba') and FB7.LiquidityScore=3)
or
((sm.scotiaIndustrySector='Government' and sm.scotiaIndustryGroup='Municipal' and sm.scotiaIndustrySubGroup='Manitoba') and FB7.LiquidityScore=3)
or
((sm.scotiaIndustrySector='Government' and sm.scotiaIndustryGroup='Municipal' and sm.scotiaIndustrySubGroup='British Columbia') and FB7.LiquidityScore=3)
or
((sm.scotiaIndustrySector='Government' and sm.scotiaIndustryGroup='Municipal' and sm.scotiaIndustrySubGroup is Null) and FB7.LiquidityScore=4)
or
((sm.scotiaIndustrySector='Government' and sm.scotiaIndustryGroup='Municipal' and sm.scotiaIndustrySubGroup='Quebec') and FB7.LiquidityScore=2)
or
((sm.scotiaIndustrySector='Government' and sm.scotiaIndustryGroup='Provincial' and sm.scotiaIndustrySubGroup='Nova Scotia') and FB7.LiquidityScore=3)
or
((sm.scotiaIndustrySector='Government' and sm.scotiaIndustryGroup='Municipal' and sm.scotiaIndustrySubGroup='Newfoundland') and FB7.LiquidityScore=4)
or
((sm.scotiaIndustrySector='Preferred Equity' and sm.scotiaIndustryGroup='Preferred Equity') and FB7.LiquidityScore=2)
or
((sm.scotiaIndustrySector is null and sm.scotiaIndustryGroup is null) and FB7.LiquidityScore=5)
)

请帮忙,谢谢

向数据库中添加新表。它将如下所示:

IndustrySector IndustryGroup IndustrySubGroup LiquidityScore Corporate Financial 2 Corporate Infrastructure 3 Corporate Communication 3 Corporate Securitization 3 Corporate Real Estate 3 Corporate Industrial 3 Government Provincial Ontario 1 Government Provincial Alberta 2 Government Federal Agency 2 Government Provincial New Brunswick 3 Government Provincial Newfoundland 3 Government Provincial British Columbia 3 Government Federal Non-Agency 1 Government Municipal Ontario 2 Government Provincial Quebec 1 Government Provincial Saskatchewan 3 您可能需要为现有表中的
度量
字段添加一列。此外,如果可能存在多个匹配项(在这种情况下似乎不太可能),则可以使用
应用
在存在的地方
而不是联接,并且您可能需要在子组比较中使用
合并()
,如下所示:
LG.IndustrySubGroup=COALESCE(sm.scotiainustrysubgroup.),


最好的部分是,您可以在新表上设置索引,这可以极大地提高性能,并且您可以根据业务规则或评分随时间的变化轻松调整此表,而无需返回并重新编写查询。

将新表添加到数据库中。它将如下所示:

IndustrySector IndustryGroup IndustrySubGroup LiquidityScore Corporate Financial 2 Corporate Infrastructure 3 Corporate Communication 3 Corporate Securitization 3 Corporate Real Estate 3 Corporate Industrial 3 Government Provincial Ontario 1 Government Provincial Alberta 2 Government Federal Agency 2 Government Provincial New Brunswick 3 Government Provincial Newfoundland 3 Government Provincial British Columbia 3 Government Federal Non-Agency 1 Government Municipal Ontario 2 Government Provincial Quebec 1 Government Provincial Saskatchewan 3 您可能需要为现有表中的
度量
字段添加一列。此外,如果可能存在多个匹配项(在这种情况下似乎不太可能),则可以使用
应用
在存在的地方
而不是联接,并且您可能需要在子组比较中使用
合并()
,如下所示:
LG.IndustrySubGroup=COALESCE(sm.scotiainustrysubgroup.),


这其中最好的部分是,您可以在新表上设置索引,这可以极大地提高性能,并且您可以根据业务规则或评分随时间的变化轻松调整此表,而无需返回并重新编写查询。

感谢您的帮助!谢谢你的帮助!