我目前正在制作一份报告,向我展示销售团队所涵盖的所有邮政编码。
每个团队涵盖100多个邮政编码。我想要做的是创建一个报告,将邮件代码中的客户端带回来。目前我的代码看起来像这样。
SELECT * FROM tbl_ClientFile
WHERE CLNTPOST1 LIKE ('B79%')
OR CLNTPOST1 LIKE ('BB1%')
OR CLNTPOST1 LIKE ('BB10%')
OR CLNTPOST1 LIKE ('BB11%')
OR CLNTPOST1 LIKE ('BB12%')
OR CLNTPOST1 LIKE ('BB18%')
OR CLNTPOST1 LIKE ('BB2%')
OR CLNTPOST1 LIKE ('BB3%')
OR CLNTPOST1 LIKE ('BB4%')
OR CLNTPOST1 LIKE ('BB5%')
OR CLNTPOST1 LIKE ('BB6%')
OR CLNTPOST1 LIKE ('BB8%')
OR CLNTPOST1 LIKE ('BB9%')
OR CLNTPOST1 LIKE ('BB94%')
OR CLNTPOST1 LIKE ('BD1%')
OR CLNTPOST1 LIKE ('BD10%')
OR CLNTPOST1 LIKE ('BD11%')
OR CLNTPOST1 LIKE ('BD12%')
OR CLNTPOST1 LIKE ('BD13%')
OR CLNTPOST1 LIKE ('BD14%')
OR CLNTPOST1 LIKE ('BD15%')
OR CLNTPOST1 LIKE ('BD16%')
OR CLNTPOST1 LIKE ('BD17%')
OR CLNTPOST1 LIKE ('BD18%')
OR CLNTPOST1 LIKE ('BD19%')
OR CLNTPOST1 LIKE ('BD2%')
OR CLNTPOST1 LIKE ('BD20%')
OR CLNTPOST1 LIKE ('BD21%')
OR CLNTPOST1 LIKE ('BD22%')
OR CLNTPOST1 LIKE ('BD3%')
OR CLNTPOST1 LIKE ('BD4%')
OR CLNTPOST1 LIKE ('BD5%')
OR CLNTPOST1 LIKE ('BD6%')
我所希望的是有更快更简单的方法。任何建议将不胜感激。有没有办法为每个销售团队创建一个变量,如@SalesTeam1 = SELECT * FROM tbl_ClientFile WHERE POSTCODE1 like '' or like ''
真正钓到想法。干杯
答案 0 :(得分:5)
可能的解决方案之一。创建一个表Prefix(v varchar(4))
,您可以在其中插入这些值。然后解决方案是:
SELECT *
FROM tbl_ClientFile cf
JOIN Prefix p on cf.CLNTPOST1 LIKE p.v + '%'
如果某些前缀包含另一个前缀,例如BB1
,BB10
,BB15
...:
SELECT DISTINCT cf.*
FROM tbl_ClientFile cf
JOIN Prefix p on cf.CLNTPOST1 LIKE p.v + '%'
答案 1 :(得分:4)
WITH CTE AS ( SELECT VALUE FROM ( VALUES ('B79'), ('BB1'), ('BB10'), ('BB11'), ('BB12'), ('BB18'), ('BB2'), ('BB3'), ('BB4'), ('BB5'), ('BB6'), ('BB8'), ('BB9'), ('BB94'), ('BD1'), ('BD10'), ('BD11'), ('BD12'), ('BD13'), ('BD14'), ('BD15'), ('BD16'), ('BD17'), ('BD18'), ('BD19'), ('BD2'), ('BD20'), ('BD21'), ('BD22'), ('BD3'), ('BD4'), ('BD5'), ('BD6') ) V(VALUE) )SELECT * FROM tbl_ClientFile T WHERE EXISTS ( SELECT TOP 1 1 FROM CTE WHERE T.CLNTPOST1 LIKE CTE.VALUE + '%')
答案 2 :(得分:1)
你的大多数喜欢已被其他喜欢的人所覆盖。您可以使用具有多个值的喜欢,并获得相同的结果:
SELECT * FROM tbl_ClientFile
WHERE
CLNTPOST1 LIKE ('B79%')
OR CLNTPOST1 LIKE ('BB[12345689]%')
OR CLNTPOST1 LIKE ('BD[1-6]%')
如果您检查CLNTPOST1是否与BB1%相同,那么您不必检查BB11%或BB12%