SQL - 如何声明要在我的查询中使用的一组人名?
SQL - How do I declare a group of people's names to be used in my query?
我有 5 个查询,我将 运行 同时对 return 5 组不同的数据进行查询 - 它们的共同点是我在下面的陈述:
HAVING ( LEFT(comm.LONG_COMM_LINE_1, CHARINDEX('-', comm.LONG_COMM_LINE_1) - 2) LIKE ('%BRIAN%MILLER%') OR
LEFT(comm.LONG_COMM_LINE_1, CHARINDEX('-', comm.LONG_COMM_LINE_1) - 2) LIKE ('%LATEASHA%CALVERT%') OR
LEFT(comm.LONG_COMM_LINE_1, CHARINDEX('-', comm.LONG_COMM_LINE_1) - 2) LIKE ('%CHERITA%MARTIN%') OR
LEFT(comm.LONG_COMM_LINE_1, CHARINDEX('-', comm.LONG_COMM_LINE_1) - 2) LIKE ('%CARMALITA%MCQUEEN%') OR
LEFT(comm.LONG_COMM_LINE_1, CHARINDEX('-', comm.LONG_COMM_LINE_1) - 2) LIKE ('%CRAIG%NADRATOWSKI%') OR
LEFT(comm.LONG_COMM_LINE_1, CHARINDEX('-', comm.LONG_COMM_LINE_1) - 2) LIKE ('%SCOTT%SMOLINSKI%') OR
LEFT(comm.LONG_COMM_LINE_1, CHARINDEX('-', comm.LONG_COMM_LINE_1) - 2) LIKE ('%ALISHA%WILLIAMS%') OR
LEFT(comm.LONG_COMM_LINE_1, CHARINDEX('-', comm.LONG_COMM_LINE_1) - 2) LIKE ('%SHANNON%MURPHY%') OR
LEFT(comm.LONG_COMM_LINE_1, CHARINDEX('-', comm.LONG_COMM_LINE_1) - 2) LIKE ('%MICHELLE%COLE%') OR
LEFT(comm.LONG_COMM_LINE_1, CHARINDEX('-', comm.LONG_COMM_LINE_1) - 2) LIKE ('%ROGER%KONKEL%') OR
LEFT(comm.LONG_COMM_LINE_1, CHARINDEX('-', comm.LONG_COMM_LINE_1) - 2) LIKE ('%DANIELLE%JONES%') OR
LEFT(comm.LONG_COMM_LINE_1, CHARINDEX('-', comm.LONG_COMM_LINE_1) - 2) LIKE ('%SABRINA%WARNER%') OR
LEFT(comm.LONG_COMM_LINE_1, CHARINDEX('-', comm.LONG_COMM_LINE_1) - 2) LIKE ('%CORINNE%DUBETZ%'))
我想在查询的开头定义这些人,以便稍后在 having 语句或 where 语句中使用,基本上是说
HAVING (left(comm.LONG_COMM_LINE_1, CHARINDEX('-',comm.LONG_COMM_LINE_1) - 2) LIKE [anyone that I've already defined above]
我不确定这是否可行,或者如果可行,我会怎么做。任何帮助将不胜感激!
要扩展 Alex K 的评论,您可以从 table 变量开始:
DECLARE @People TABLE ([Name] VARCHAR(128));
INSERT @People VALUES ('%BRIAN%MILLER%'),('%LATEASHA%CALVERT%'),('%CHERITA%MARTIN%')
现在假设我有一个 Member table 并且我需要根据您的条件在这个 table 中找到成员,查询可能看起来像这样(我只是按名称分组以符合您的示例):
SELECT
M.[Name]
FROM
mem.Member M
GROUP BY
M.[Name]
HAVING
(EXISTS(SELECT 1 FROM @PEOPLE P WHERE M.Name LIKE P.Name))
更具体地针对您的示例:
SELECT
comm.[LONG_COMM_LINE_1]
FROM
comm
GROUP BY
comm.[LONG_COMM_LINE_1]
HAVING
(EXISTS(SELECT 1 FROM @PEOPLE P WHERE LEFT(comm.LONG_COMM_LINE_1, CHARINDEX('-',
comm.LONG_COMM_LINE_1) - 2) LIKE P.Name))
我有 5 个查询,我将 运行 同时对 return 5 组不同的数据进行查询 - 它们的共同点是我在下面的陈述:
HAVING ( LEFT(comm.LONG_COMM_LINE_1, CHARINDEX('-', comm.LONG_COMM_LINE_1) - 2) LIKE ('%BRIAN%MILLER%') OR
LEFT(comm.LONG_COMM_LINE_1, CHARINDEX('-', comm.LONG_COMM_LINE_1) - 2) LIKE ('%LATEASHA%CALVERT%') OR
LEFT(comm.LONG_COMM_LINE_1, CHARINDEX('-', comm.LONG_COMM_LINE_1) - 2) LIKE ('%CHERITA%MARTIN%') OR
LEFT(comm.LONG_COMM_LINE_1, CHARINDEX('-', comm.LONG_COMM_LINE_1) - 2) LIKE ('%CARMALITA%MCQUEEN%') OR
LEFT(comm.LONG_COMM_LINE_1, CHARINDEX('-', comm.LONG_COMM_LINE_1) - 2) LIKE ('%CRAIG%NADRATOWSKI%') OR
LEFT(comm.LONG_COMM_LINE_1, CHARINDEX('-', comm.LONG_COMM_LINE_1) - 2) LIKE ('%SCOTT%SMOLINSKI%') OR
LEFT(comm.LONG_COMM_LINE_1, CHARINDEX('-', comm.LONG_COMM_LINE_1) - 2) LIKE ('%ALISHA%WILLIAMS%') OR
LEFT(comm.LONG_COMM_LINE_1, CHARINDEX('-', comm.LONG_COMM_LINE_1) - 2) LIKE ('%SHANNON%MURPHY%') OR
LEFT(comm.LONG_COMM_LINE_1, CHARINDEX('-', comm.LONG_COMM_LINE_1) - 2) LIKE ('%MICHELLE%COLE%') OR
LEFT(comm.LONG_COMM_LINE_1, CHARINDEX('-', comm.LONG_COMM_LINE_1) - 2) LIKE ('%ROGER%KONKEL%') OR
LEFT(comm.LONG_COMM_LINE_1, CHARINDEX('-', comm.LONG_COMM_LINE_1) - 2) LIKE ('%DANIELLE%JONES%') OR
LEFT(comm.LONG_COMM_LINE_1, CHARINDEX('-', comm.LONG_COMM_LINE_1) - 2) LIKE ('%SABRINA%WARNER%') OR
LEFT(comm.LONG_COMM_LINE_1, CHARINDEX('-', comm.LONG_COMM_LINE_1) - 2) LIKE ('%CORINNE%DUBETZ%'))
我想在查询的开头定义这些人,以便稍后在 having 语句或 where 语句中使用,基本上是说
HAVING (left(comm.LONG_COMM_LINE_1, CHARINDEX('-',comm.LONG_COMM_LINE_1) - 2) LIKE [anyone that I've already defined above]
我不确定这是否可行,或者如果可行,我会怎么做。任何帮助将不胜感激!
要扩展 Alex K 的评论,您可以从 table 变量开始:
DECLARE @People TABLE ([Name] VARCHAR(128));
INSERT @People VALUES ('%BRIAN%MILLER%'),('%LATEASHA%CALVERT%'),('%CHERITA%MARTIN%')
现在假设我有一个 Member table 并且我需要根据您的条件在这个 table 中找到成员,查询可能看起来像这样(我只是按名称分组以符合您的示例):
SELECT
M.[Name]
FROM
mem.Member M
GROUP BY
M.[Name]
HAVING
(EXISTS(SELECT 1 FROM @PEOPLE P WHERE M.Name LIKE P.Name))
更具体地针对您的示例:
SELECT
comm.[LONG_COMM_LINE_1]
FROM
comm
GROUP BY
comm.[LONG_COMM_LINE_1]
HAVING
(EXISTS(SELECT 1 FROM @PEOPLE P WHERE LEFT(comm.LONG_COMM_LINE_1, CHARINDEX('-',
comm.LONG_COMM_LINE_1) - 2) LIKE P.Name))