简体   繁体   English

CASE 语句 - 已达到表达式服务限制

[英]CASE Statement - An expression services limit has been reached

I'm getting the following error:我收到以下错误:

An expression services limit has been reached.已达到表达服务限制。 Please look for potentially complex expressions in your query, and try to simplify them.请在您的查询中寻找可能复杂的表达式,并尝试简化它们。

I'm attempting to run the below query, however it appears there is one line too many in my case statement (when i remove the "London" Line, it works perfectly) or "Scotland" for example.我正在尝试运行以下查询,但在我的案例语句中似乎有一行太多(当我删除“伦敦”行时,它运行得很好)或“苏格兰”。

I can't think of the best way to split this statement.我想不出拆分此语句的最佳方法。 If i split it into 2 queries and union all, it does work.如果我将它分成 2 个查询并合并所有,它确实有效。 however the ELSE 'No Region' becomes a problem.然而, ELSE 'No Region'成为一个问题。 Everything which is included in the first part of the query shows as "No Region" for the second part of the query, and vice versa.对于查询的第二部分,查询第一部分中包含的所有内容都显示为“无区域”,反之亦然。

(My end goal is essentially to create a list of customers per region) I can then use this as the foundation of a regional sales report. (我的最终目标基本上是为每个地区创建一个客户列表)然后我可以将其用作区域销售报告的基础。

Many Thanks非常感谢

Andy安迪

SELECT T0.CardCode, T0.CardName, T0.PostCode,
  
  CASE
WHEN T0.PostCodeABR IN  ('DG','KW','IV','PH','AB','DD','PA','FK','KY','G','EH','ML','KA','TD') THEN 'Scotland' 
WHEN T0.PostCodeABR IN  ('BT') THEN 'Ireland' 
WHEN T0.PostCodeABR IN  ('CA','NE','DH','SR','TS','DL','LA','BD','HG','YO','HX','LS','FY','PR','BB','L','WN','BL','OL') THEN 'North M62' 
WHEN T0.PostCodeABR IN  ('CH','WA','CW','SK','M','HD','WF','DN','HU','DE','NG','LN','S') THEN 'South M62' 
WHEN T0.PostCodeABR IN  ('LL','SY','LD','SA','CF','NP') THEN 'Wales' 
WHEN T0.PostCodeABR IN  ('NR','IP','CB') THEN 'East Anglia' 
WHEN T0.PostCodeABR IN  ('SN','BS','BA','SP','BH','DT','TA','EX','TQ','PL','TR') THEN 'South West' 
WHEN T0.PostCodeABR IN  ('LU','AL','HP','SG','SL','RG','SO','GU','PO','BN','RH','TN','ME','CT','SS','CM','CO') THEN 'South East' 
WHEN T0.PostCodeABR IN  ('ST','TF','WV','WS','DY','B','WR','HR','GL','OX','CV','NN','MK','PE','LE') THEN 'Midlands' 
WHEN T0.PostCodeABR IN  ('WD','EN','HA','N','NW','UB','W','WC','EC','E','IG','RM','DA','BR','CR','SM','KT','TW','SW') THEN 'London' 

ELSE 'No Region'

END AS 'Region'
   FROM [dbo].[REPS-PostcodeABBR] T0

As I mentioned in the comment, I would suggest you create a "lookup" table for the post codes, then all you need to do is JOIN to the table, and not have a "messy" and large CASE expression (T-SQL doesn't support Case ( Switch ) statements).正如我在评论中提到的,我建议您为邮政编码创建一个“查找”表,然后您需要做的就是JOIN该表,而不是有一个“混乱”和大的CASE表达式(T-SQL 没有't 支持Case ( Switch ) 语句)。

So your lookup table would look a little like this:所以你的查找表看起来有点像这样:

CREATE TABLE dbo.PostcodeRegion (Postcode varchar(2),
                                 Region varchar(20));
GO
--Sample data
INSERT INTO dbo.PostcodeRegion (Postcode,Region)
VALUES('DG','Scotland'),
      ('BT','Ireland'),
      ('LL','Wales');

And then your query would just do a LEFT JOIN :然后你的查询只会做一个LEFT JOIN

SELECT RPA.CardCode,
       RPA.CardName,
       RPA.PostCode,
       COALESCE(PR.Region,'No Region') AS Region
FROM [dbo].[REPS-PostcodeABBR] RPA --T0 is a poor choice of an alias, there is no T not 0 in "REPS-PostcodeABBR"
     LEFT JOIN dbo.PostcodeRegion PR ON RPA.PostCodeABR = PR.Region;

Note you would likely want to INDEX the table as well, and/or apply a UNIQUE CONSTRAINT or PRIMARY KEY to the PostCode column.请注意,您可能还希望对表进行INDEX ,和/或将UNIQUE CONSTRAINTPRIMARY KEY应用于PostCode列。

Thanks for the help... I tried multiple ways mentioned above, and they all did work, however the most efficient seemed to be this way.感谢您的帮助...我尝试了上面提到的多种方法,它们都起作用了,但是最有效的方法似乎是这种方法。

Created a lookup table within SAP;在 SAP 中创建了一个查找表; This table included PostCodeFrom, PostCodeTo, PostCodeABR, Region该表包括 PostCodeFrom、PostCodeTo、PostCodeABR、Region

This would look like;这看起来像; TS00, TS99, TS, North M62 TS00、TS99、TS、北 M62

I then done;然后我完成了;

SELECT OCRD.ZipCode PCLOOKUP.Region, PCLOOKUP.PostCodeABR FROM OCRD T0 LEFT OUTER JOIN PCLOOKUP ON OCRD.ZipCode >= PCLOOKUP.PostCodeFROM AND OCRD.ZipCode <= PCLOOKUP.PostCodeFrom

Basically, if the postcode is between FROM AND To Display the abbreviation and region.基本上,如果邮政编码介于 FROM 和 To 之间,则显示缩写和区域。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM