我需要在SQL Server中实现以下查询:
select *
from table1
WHERE (CM_PLAN_ID,Individual_ID)
IN
(
Select CM_PLAN_ID, Individual_ID
From CRM_VCM_CURRENT_LEAD_STATUS
Where Lead_Key = :_Lead_Key
)
但是WHERE..IN子句只允许1列。我如何比较2个或更多的列与另一个内部选择?
我需要在SQL Server中实现以下查询:
select *
from table1
WHERE (CM_PLAN_ID,Individual_ID)
IN
(
Select CM_PLAN_ID, Individual_ID
From CRM_VCM_CURRENT_LEAD_STATUS
Where Lead_Key = :_Lead_Key
)
但是WHERE..IN子句只允许1列。我如何比较2个或更多的列与另一个内部选择?
当前回答
简单而错误的方法是使用+或连接两列并生成一列。
Select *
from XX
where col1+col2 in (Select col1+col2 from YY)
这会偏离轨道,很慢。不能在编程中使用,但如果你只是查询验证一些东西可能会被使用。
其他回答
简单而错误的方法是使用+或连接两列并生成一列。
Select *
from XX
where col1+col2 in (Select col1+col2 from YY)
这会偏离轨道,很慢。不能在编程中使用,但如果你只是查询验证一些东西可能会被使用。
select * from tab1 where (col1,col2) in (select col1,col2 from tab2)
注意: Oracle忽略一个或多个所选列为NULL的行。在这些情况下,你可能想要使用NVL-Funktion将NULL映射到一个特殊的值(不应该在值中);
select * from tab1
where (col1, NVL(col2, '---') in (select col1, NVL(col2, '---') from tab2)
以某种形式将列连接在一起是一种“hack”,但是当产品不支持多个列的半连接时,有时您别无选择。
内部/外部连接解决方案不工作的例子:
select * from T1
where <boolean expression>
and (<boolean expression> OR (ColA, ColB) in (select A, B ...))
and <boolean expression>
...
当查询在本质上并不简单时,有时您无法访问基表集来执行常规的内/外连接。
如果你确实使用了这个“黑客”,当你组合字段时,请确保在它们之间添加足够的分隔符以避免误解,例如ColA + ":-:" + ColB
我觉得这样更容易
Select *
from table1
WHERE (convert(VARCHAR,CM_PLAN_ID) + convert(VARCHAR,Individual_ID))
IN
(
Select convert(VARCHAR,CM_PLAN_ID) + convert(VARCHAR,Individual_ID)
From CRM_VCM_CURRENT_LEAD_STATUS
Where Lead_Key = :_Lead_Key
)
希望这对你有所帮助:)
*** t-sql ***
我使用string_agg作为廉价的hack来获得cheep上的一些伪规范化。(好吧,它是多路复用的,我知道它很糟糕,我用一些精心制作的80年代风格的盒子图来补偿你的痛苦,享受吧!~)
这里有一个例子(想出名字替代品很有趣:D)
select
vendorId,
affiliate_type_code,
parent_vendor_id,
state_abbr,
county_abbr,
litigation_activity_indicator,
string_agg(employee_id,',') as employee_ids,
string_agg(employee_in_deep_doodoo,',') as 'employee-inventory connections'
from (
select distinct top 10000 -- so I could pre-order my employee id's - didn't want mixed sorting in those concats
mi.missing_invintory_identifier as rqid,
vendorId,
affiliate_type_code,
parent_vendor_id,
state_abbr,
county_abbr,
litigation_activity_indicator,
employee_identifier as employee_id,
concat(employee_identifier,'-',mi.missing_invintory_identifier) as employee_in_deep_doodoo
from
missing_invintory as mi
inner join vendor_employee_view as ev
on mi.missing_invintory_identifier = ev.missing_invintory_identifier
where ev.litigation_activity_indicator = 'N'
order by employee_identifier desc
) as x
group by
vendorId,
affiliate_type_code,
parent_vendor_id,
state_abbr,
county_abbr,
litigation_activity_indicator
having count(employee_id) > 1
┏━━━━━━━━━━┳━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ vendorId ┃ affiliate_type ┃ parent_vendor_id ┃ state_abbr ┃ county_abbr ┃ litigation_indicator ┃ employee_ids ┃ employee-inventory connections ┃
┣━━━━━━━━━━╋━━━━━━━━━━━━━━━━╋━━━━━━━━━━━━━━━━━━╋━━━━━━━━━━━━╋━━━━━━━━━━━━━━━━━━━╋━━━━━━━━━━━━━━━━━━━━━━╋━━━━━━━━━━━━━━━━━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
┃ 123 ┃ EXP ┃ 17 ┃ CA ┃ SDG ┃ N ┃ 112358,445678 ┃ 112358-1212,1534490-1212 ┃
┣━━━━━━━━━━╋━━━━━━━━━━━━━━━━╋━━━━━━━━━━━━━━━━━━╋━━━━━━━━━━━━╋━━━━━━━━━━━━━━━━━━━╋━━━━━━━━━━━━━━━━━━━━━━╋━━━━━━━━━━━━━━━━━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
┃ 4567 ┃ PRI ┃ 202 ┃ TX ┃ STB ┃ Y ┃ 998754,332165 ┃ 998754-4545,332165-4545 ┃
┗━━━━━━━━━━┻━━━━━━━━━━━━━━━━┻━━━━━━━━━━━━━━━━━━┻━━━━━━━━━━━━┻━━━━━━━━━━━━━━━━━━━┻━━━━━━━━━━━━━━━━━━━━━━┻━━━━━━━━━━━━━━━━━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛