0

我有一个 116 种不同的代理到顶点/地理组合的列表,我必须确保它们配对在一起。例如,这是我的 116 列表中的一小部分。

AGNT    Vertex/Geo
5040    440292570
2010    440390000
1010    440390060
1180    440390246
1800    440390570
1130    440391110
1830    440391680
1410    440395020
1060    440397460
2000    440399685
1460    440399687
2020    440710000
1030    440710210

我一直在使用这个查询来逐一检查每一对。换句话说,我必须不断更改 agnt_hse 和 VRTX_GEOCODE_HSE 以检查每一对是否正确。

USE ExternalUser

SELECT  distinct HSE_KEY_HSE AS HOUSE_KEY, AGNT_HSE AS AGENT, VRTX_GEOCODE_HSE as GEOCODE

FROM        ExternalUser.Vantage.HSE_BASE 
      INNER JOIN externaluser.Vantage.HSL_LOB ON HSE_KEY_HSE = HSE_KEY_HSL 
      LEFT JOIN externaluser.Vantage.HSM_MEMO ON HSE_KEY_HSE = HSE_KEY_HSM

WHERE MEMO_NO_HSM = 1 
And sys_hsl = '8777' 
And sys_hsm = '8777'
And delivery_segment_id_hsl = 'C' 
And agnt_hse in ('5040') and VRTX_GEOCODE_HSE != '440292570'

有没有办法一次运行多个查询?

我对 UNION 命令很熟悉,但我觉得有一种方法可以进一步压缩编码,因为如果我被迫使用 UNION 命令,我将粘贴超过 100 倍的大型查询。

我在想一些事情

WHERE MEMO_NO_HSM = 1 
And sys_hsl = '8777' 
And sys_hsm = '8777'
And delivery_segment_id_hsl = 'C' 
And agnt_hse in ('5040') and VRTX_GEOCODE_HSE != '440292570' 
Then agnt_hse in ('2010') and VRTX_GEOCODE_HSE != '440390000' 
Then agnt_hse in ('1010') and VRTX_GEOCODE_HSE != '440390060' 
...etc

也许我要解决这一切都是错误的,并且有一种不同的方式来运行查询?任何信息都非常感谢。

4

2 回答 2

0

您没有指定您的 rbdms,所以我将给出一个通用示例。该原理应该适用于大多数数据库引擎,但您可能必须更改语法。我还假设 agnt_hse 和 vertx_geocode_hse 是整数。如果该假设是错误的,则所需的更改应该是显而易见的。

select yourfields
from yourtables
where concat(to_char(agnt_hse), to_char(vertx_geocode_hse)) in
(select concat(to_char(agnt_hse), to_char(vertx_geocode_hse))
from TheTablesYouAreLookingAt
minus 
select concat(to_char(agnt_hse), to_char(vertx_geocode_hse))
from TheTablesWithTheCorrectData)
于 2013-08-06T23:01:06.410 回答
0

看起来您正在尝试获取您提供的列表的例外情况。也就是说,与代理匹配但具有不同顶点的所有行。

我将使用列表的显式辅助表(如果可能)来表达此查询,或者使用 CTE 构造一个。这是 CTE 版本:

with pairs as (
      select '5040' as agent, '440292570' as vertex union all
      select '2010', '440390000'
      . . .
     )
SELECT distinct HSE_KEY_HSE AS HOUSE_KEY, AGNT_HSE AS AGENT, VRTX_GEOCODE_HSE as GEOCODE
FROM  ExternalUser.Vantage.HSE_BASE INNER JOIN
      externaluser.Vantage.HSL_LOB
      ON HSE_KEY_HSE = HSE_KEY_HSL LEFT JOIN 
      externaluser.Vantage.HSM_MEMO
      ON HSE_KEY_HSE = HSE_KEY_HSM join
      pairs p
      on p.agent = agnt_hse
WHERE MEMO_NO_HSM = 1 And
      sys_hsl = '8777' And
      sys_hsm = '8777' And
      delivery_segment_id_hsl = 'C' and
      (p.Vertex <> VRTX_GEOCODE_HSE)
于 2013-08-06T20:52:42.033 回答