减少我的SQL代码的长度

时间:2016-10-31 16:19:39

标签: sql sql-server tsql

我正在为此SQL查询使用第三方软件,该查询具有各种限制,一个是1,000个字符的字符限制。我创建的代码将通过注册到特定帐户的联系人号码列表,并具有专门为此任务创建的视图。我当前的脚本(见下文)有效,但会带来重复的响应。我想缩短代码的长度,并仅选择Distinct响应。

作为背景,这个链接到的CRM系统有多个地方,有人可以留下联系号码,所以我需要代码在显示响应时删除任何重复项。由于上述字符限制,当前代码不会尝试删除重复项,它是:

SELECT [Contact_Mobile_Phone] AS V, [Contact_Mobile_Phone] AS D
FROM [DatabaseName]
WHERE MAINLKUPID = '{Import.TenantID}' AND [Contact_Mobile_Phone] IS NOT NULL
UNION ALL
SELECT [Contact_Home_Phone] AS V, [Contact_Home_Phone] AS D
FROM [DatabaseName]
WHERE MAINLKUPID = '{Import.TenantID}' AND [Contact_Home_Phone] IS NOT NULL
UNION ALL
SELECT [Contact_Work_Phone] AS V, [Contact_Work_Phone] AS D
FROM [DatabaseName]
WHERE MAINLKUPID = '{Import.TenantID}' AND [Contact_Work_Phone] IS NOT NULL
UNION ALL
SELECT [Group_Home_Phone] AS V, [Group_Home_Phone] AS D
FROM [DatabaseName]
WHERE MAINLKUPID = '{Import.TenantID}' AND [Group_Home_Phone] IS NOT NULL
UNION ALL
SELECT [Group_Mobile_Phone] AS V, [Group_Mobile_Phone] AS D
FROM [DatabaseName]
WHERE MAINLKUPID = '{Import.TenantID}' AND [Group_Mobile_Phone] IS NOT NULL
UNION ALL
SELECT [Contact_Home_Phone] AS V, [Contact_Home_Phone] AS D
FROM [DatabaseName]
WHERE [Group_ID] = '{Script.V1}' AND [Contact_Home_Phone] IS NOT NULL
UNION ALL
SELECT [Contact_Mobile_Phone] AS V, [Contact_Mobile_Phone] AS D
FROM [DatabaseName]
WHERE [Group_ID] = '{Script.V1}' AND [Contact_Mobile_Phone] IS NOT NULL
UNION ALL
SELECT 'Enter other number' AS V, 'Enter other number' AS D

正如您所看到的,它不是我编码的最优雅的东西。我希望它看起来像是这样的:

SELECT DISTINCT ([Contact_Mobile_Phone], [Contact_Home_Phone], [Contact_Work_Phone], [Group_Home_Phone], [Group_Mobile_Phone]) V,
([Contact_Mobile_Phone], [Contact_Home_Phone], [Contact_Work_Phone], [Group_Home_Phone], [Group_Mobile_Phone]) D
FROM [DatabaseName]
WHERE MAINLKUPID = '{Import.TenantID}' AND ([Contact_Mobile_Phone], [Contact_Home_Phone], [Contact_Work_Phone], [Group_Home_Phone], [Group_Mobile_Phone])  IS NOT NULL
AND MAINLKUPID = '{Import.TenantID}' AND ([Contact_Mobile_Phone], [Contact_Home_Phone], [Contact_Work_Phone], [Group_Home_Phone], [Group_Mobile_Phone])  IS NOT NULL

标准是它需要在第V列和第D列中显示相同的结果。它不需要通过Null数据,并且它只需要在每列中输入一次,即使它们来自不同的列本来。我知道我的理想代码的语法是对的,希望有人可以指出我正确的方向。提前谢谢。

3 个答案:

答案 0 :(得分:0)

为什么不将代码放在存储过程中并只调用“EXEC my_stored_procedure;”?那会短得多!

答案 1 :(得分:0)

你能解释为什么两次选择同一列,但给它命名不同吗?也许你打算第一个作为描述。如果是这样,它应该是单引号而不是方括号 - 但这会影响重复消除。

您可以执行以下操作以删除重复项:

  • UNION ALL替换为UNION

您可以执行以下操作来缩小尺寸:

  • 删除所有方括号
  • 从除第一个查询之外的所有内容中删除as Vas D

如果这还不够短,则删除NULL比较并将查询结构为:

select *
from (<your query here with no null comparisons and `union` instead of `union all`) x
where d is not null;

答案 2 :(得分:0)

select  phone_num as V,phone_num as D
from    [DatabaseName] unpivot (phone_num for phone_type in ([Contact_Mobile_Phone],[Contact_Home_Phone],[Contact_Work_Phone],[Group_Home_Phone],[Group_Mobile_Phone])) u
where   MAINLKUPID = '{Import.TenantID}'
union
select  phone_num as V,phone_num as D
from    [DatabaseName] unpivot (phone_num for phone_type in ([Contact_Home_Phone],[Contact_Mobile_Phone])) u
WHERE   [Group_ID] = '{Script.V1}'
union all
SELECT 'Enter other number','Enter other number'