SQL 在 where 子句中与 coalesce(?) 合并
SQL Union with coalesce(?) in where clause
我正在尝试将旧的帮助台记录合并到较新的帮助台应用程序中。我仍然拥有旧帮助台应用程序的所有数据,它使用与新帮助台应用程序相同的字段,但在不同的文件中。此 select 语句适用于较新的应用程序,可以搜索所有过去的调用以查找特定内容,无论是关键字还是分配给谁等等。
SELECT status, identity, description, contact, scan_text, extended_desc, allocated_to
FROM helpdesk.table1
WHERE UPPER(allocated_to) = coalesce(?, allocated_to)
AND identity = coalesce(?, identity)
AND description = coalesce(?, description)
AND contact= coalesce(?, contact)
AND UPPER(scan_text) LIKE coalesce(?,scantext)
and upper(extended_desc) like coalesce(?, extended_desc)
ORDER by allocated, identity desc
我想做的是使用某种联合,这样我就可以只用一个框来搜索新记录和旧记录,而不是两个不同的框并且不得不记住数据可能存储的位置。我在想这样的事情可能会奏效,但我认为我的 where 子句太模糊了,我尝试了几乎所有在字段前面包含库的组合。
Select *
From
(
select status, identity, description, contact, scan_text, extended_desc, allocated_to
from helpdesk.table1
Union
select status, identity, description, contact, scan_text, extended_desc, allocated_to
from helpdesk.table2
)
WHERE UPPER(allocated_to) = coalesce(?, allocated_to)
AND identity = coalesce(?, identity)
AND description = coalesce(?, description)
AND contact= coalesce(?, contact)
AND UPPER(scan_text) LIKE coalesce(?,scantext)
and upper(extended_desc) like coalesce(?, extended_desc)
ORDER by allocated, identity desc
如果我只对两个 select 进行并集,我会从两个表中获取所有记录,但我需要能够将结果缩小到关键字或其他类型的字段,例如第一个代码块。如果有人能指出正确的方向,我将不胜感激。
此外,我可能应该说它是 db2,而 sql 在 Web 应用程序上是 运行。因此,当此 sql 运行时,它会生成下拉框或文本字段来输入您自己的话,以缩小所有帮助台呼叫的结果范围。
如果我没理解错的话,您需要一个 table 记录来源的指示符。如果是:
Select *
From (select 'old' as which, status, identity, description, contact, scan_text, extended_desc, allocated_to
from helpdesk.table1
Union all
select 'new', status, identity, description, contact, scan_text, extended_desc, allocated_to
from helpdesk.table2
) hd
WHERE UPPER(allocated_to) = coalesce(?, allocated_to) AND
identity = coalesce(?, identity) AND
description = coalesce(?, description) AND
contact= coalesce(?, contact) AND
UPPER(scan_text) LIKE coalesce(?,scantext) AND
upper(extended_desc) like coalesce(?, extended_desc)
ORDER by allocated, identity desc;
然后您可以在 which
列上添加适当的 where
逻辑(并将该列重命名为您想要的任何名称)。
您始终可以将 where 放在要合并的两个 select 语句中。可能比合并两个表然后过滤更快。
SELECT * FROM helpdesk.table1
WHERE UPPER(allocated_to) = coalesce(?, allocated_to)
AND identity = coalesce(?, identity)
AND description = coalesce(?, description)
AND contact= coalesce(?, contact)
AND UPPER(scan_text) LIKE coalesce(?,scantext)
and upper(extended_desc) like coalesce(?, extended_desc)
UNION ALL
SELECT * FROM helpdesk.table2
WHERE UPPER(allocated_to) = coalesce(?, allocated_to)
AND identity = coalesce(?, identity)
AND description = coalesce(?, description)
AND contact= coalesce(?, contact)
AND UPPER(scan_text) LIKE coalesce(?,scantext)
and upper(extended_desc) like coalesce(?, extended_desc)
ORDER BY allocated, identity desc;
只需在第二个 select 之后下订单。 UPPER(allocated_to) = coalesce(?, UPPER(allocated_to))
如果您担心大小写,语法可能会更好。
我不知道这是否特定于我正在使用的网络应用程序生成器,或者您是否可以在不同的其他地方使用它 SQL,但您可以引用参数并将它们传递到场景中有一个叫做parameter parameter的场景,即联盟后半部分的??1、??2、??3。这解决了有两组用户输入框的问题,只需传递您在第一个位置输入的内容。这是适合我的代码的最终版本
SELECT status, identity, description, contact, scan_text, extended_desc, allocated_to
FROM helpdesk.table1
WHERE UPPER(allocated_to) = coalesce(?, allocated_to)
AND identity = coalesce(?, identity)
AND description = coalesce(?, description)
AND contact= coalesce(?, contact)
AND UPPER(scan_text) LIKE coalesce(?,scantext)
and upper(extended_desc) like coalesce(?, extended_desc)
Union
SELECT status, identity, description, contact, scan_text, extended_desc, allocated_to
FROM helpdesk.table2
WHERE UPPER(allocated_to) = coalesce(??1, allocated_to)
AND identity = coalesce(??2, identity)
AND description = coalesce(??3, description)
AND contact= coalesce(??4, contact)
AND UPPER(scan_text) LIKE coalesce(??5,scantext)
and upper(extended_desc) like coalesce(??6, extended_desc)
ORDER by allocated, identity desc
感谢那些试图帮助我的人。
我正在尝试将旧的帮助台记录合并到较新的帮助台应用程序中。我仍然拥有旧帮助台应用程序的所有数据,它使用与新帮助台应用程序相同的字段,但在不同的文件中。此 select 语句适用于较新的应用程序,可以搜索所有过去的调用以查找特定内容,无论是关键字还是分配给谁等等。
SELECT status, identity, description, contact, scan_text, extended_desc, allocated_to
FROM helpdesk.table1
WHERE UPPER(allocated_to) = coalesce(?, allocated_to)
AND identity = coalesce(?, identity)
AND description = coalesce(?, description)
AND contact= coalesce(?, contact)
AND UPPER(scan_text) LIKE coalesce(?,scantext)
and upper(extended_desc) like coalesce(?, extended_desc)
ORDER by allocated, identity desc
我想做的是使用某种联合,这样我就可以只用一个框来搜索新记录和旧记录,而不是两个不同的框并且不得不记住数据可能存储的位置。我在想这样的事情可能会奏效,但我认为我的 where 子句太模糊了,我尝试了几乎所有在字段前面包含库的组合。
Select *
From
(
select status, identity, description, contact, scan_text, extended_desc, allocated_to
from helpdesk.table1
Union
select status, identity, description, contact, scan_text, extended_desc, allocated_to
from helpdesk.table2
)
WHERE UPPER(allocated_to) = coalesce(?, allocated_to)
AND identity = coalesce(?, identity)
AND description = coalesce(?, description)
AND contact= coalesce(?, contact)
AND UPPER(scan_text) LIKE coalesce(?,scantext)
and upper(extended_desc) like coalesce(?, extended_desc)
ORDER by allocated, identity desc
如果我只对两个 select 进行并集,我会从两个表中获取所有记录,但我需要能够将结果缩小到关键字或其他类型的字段,例如第一个代码块。如果有人能指出正确的方向,我将不胜感激。
此外,我可能应该说它是 db2,而 sql 在 Web 应用程序上是 运行。因此,当此 sql 运行时,它会生成下拉框或文本字段来输入您自己的话,以缩小所有帮助台呼叫的结果范围。
如果我没理解错的话,您需要一个 table 记录来源的指示符。如果是:
Select *
From (select 'old' as which, status, identity, description, contact, scan_text, extended_desc, allocated_to
from helpdesk.table1
Union all
select 'new', status, identity, description, contact, scan_text, extended_desc, allocated_to
from helpdesk.table2
) hd
WHERE UPPER(allocated_to) = coalesce(?, allocated_to) AND
identity = coalesce(?, identity) AND
description = coalesce(?, description) AND
contact= coalesce(?, contact) AND
UPPER(scan_text) LIKE coalesce(?,scantext) AND
upper(extended_desc) like coalesce(?, extended_desc)
ORDER by allocated, identity desc;
然后您可以在 which
列上添加适当的 where
逻辑(并将该列重命名为您想要的任何名称)。
您始终可以将 where 放在要合并的两个 select 语句中。可能比合并两个表然后过滤更快。
SELECT * FROM helpdesk.table1
WHERE UPPER(allocated_to) = coalesce(?, allocated_to)
AND identity = coalesce(?, identity)
AND description = coalesce(?, description)
AND contact= coalesce(?, contact)
AND UPPER(scan_text) LIKE coalesce(?,scantext)
and upper(extended_desc) like coalesce(?, extended_desc)
UNION ALL
SELECT * FROM helpdesk.table2
WHERE UPPER(allocated_to) = coalesce(?, allocated_to)
AND identity = coalesce(?, identity)
AND description = coalesce(?, description)
AND contact= coalesce(?, contact)
AND UPPER(scan_text) LIKE coalesce(?,scantext)
and upper(extended_desc) like coalesce(?, extended_desc)
ORDER BY allocated, identity desc;
只需在第二个 select 之后下订单。 UPPER(allocated_to) = coalesce(?, UPPER(allocated_to))
如果您担心大小写,语法可能会更好。
我不知道这是否特定于我正在使用的网络应用程序生成器,或者您是否可以在不同的其他地方使用它 SQL,但您可以引用参数并将它们传递到场景中有一个叫做parameter parameter的场景,即联盟后半部分的??1、??2、??3。这解决了有两组用户输入框的问题,只需传递您在第一个位置输入的内容。这是适合我的代码的最终版本
SELECT status, identity, description, contact, scan_text, extended_desc, allocated_to
FROM helpdesk.table1
WHERE UPPER(allocated_to) = coalesce(?, allocated_to)
AND identity = coalesce(?, identity)
AND description = coalesce(?, description)
AND contact= coalesce(?, contact)
AND UPPER(scan_text) LIKE coalesce(?,scantext)
and upper(extended_desc) like coalesce(?, extended_desc)
Union
SELECT status, identity, description, contact, scan_text, extended_desc, allocated_to
FROM helpdesk.table2
WHERE UPPER(allocated_to) = coalesce(??1, allocated_to)
AND identity = coalesce(??2, identity)
AND description = coalesce(??3, description)
AND contact= coalesce(??4, contact)
AND UPPER(scan_text) LIKE coalesce(??5,scantext)
and upper(extended_desc) like coalesce(??6, extended_desc)
ORDER by allocated, identity desc
感谢那些试图帮助我的人。