使用 CTE 模拟 Excel 的 vlookup
Simulating Excel's vlookup with CTEs
可以使用以下方法从 Excel 模拟 vlookup 函数:
select b.col, (case when a.val is NULL then 'FALSE' else 'TRUE' end)
from b left outer join
(select distinct a.val
from a
) a
on b.col = a.val;
那么多个CTE可以这样嵌套:
WITH cte1 AS
(
SELECT 1 AS id
),
cte2 AS
(
SELECT 2 AS id
)
SELECT *
FROM cte1
UNION ALL
SELECT *
FROM cte2
UNION ALL
SELECT *
FROM cte1
问题是:如何使用 ct1.col
和 ct2.col
而不是 a.col and b.col
执行第一个示例中的 vlookup?我正在尝试在两个查询输出之间执行 vlookup,而不将它们存储到临时表或表中。
我在想这样的事情:
; with cte1 as ( select pd.value as value from pentd pd
inner join aster a on a.aid = pd.aid
where pid = 10
and a.name = 'ls'),
with cte2 as( select pd.value as value from pentd pd
inner join aster a on a.aid = pd.aid
where pid = 15
and a.name = 'ls' )
select (select value from cte2),
(case
when cte1.value is null then 'FALSE' else 'TRUE' end)
from cte2
left outer join
(select distinct value from cte1) cte1
on cte1.value = cte2.value
但是这个returns对我来说(当然上面的代码只是一个例子,不能link真正的数据或输出)
Msg 512, Level 16, State 1, Line 1 Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
知道我想要实现的目标是否可行吗?如果可以,那么如何在避免错误的同时将其用于多个值?
我正在使用 SQL Server 2012,但我也对 SQL Server 2008 解决方案感兴趣。
cte1 - 此处用于连接的列,因为我只有 select 值
aid name value
10 ls 123.123
10 ls 422.433
10 ls 56.32
cte2 - 此处用于连接的列,因为我只有 select 值
aid name value
15 ls 123.123
15 ls 34.21
15 ls 21.256
预期输出
value
123.123
select (select value from cte2),
这一行是你的问题。嵌套查询(括号中的位)returns 多个值。您不能 select 表(意味着多行)作为单列。试试这个:
select cte2.value,
(case
when cte1.value is null then 'FALSE' else 'TRUE' end)
from cte2
left outer join
(select distinct value from cte1) cte1
on cte1.value = cte2.value
如果您需要过滤掉重复项,您可以将其更改为 select distinct cte2.value
,或者将 distinct
选项迁移到您的 CTE。
可以使用以下方法从 Excel 模拟 vlookup 函数:
select b.col, (case when a.val is NULL then 'FALSE' else 'TRUE' end)
from b left outer join
(select distinct a.val
from a
) a
on b.col = a.val;
那么多个CTE可以这样嵌套:
WITH cte1 AS
(
SELECT 1 AS id
),
cte2 AS
(
SELECT 2 AS id
)
SELECT *
FROM cte1
UNION ALL
SELECT *
FROM cte2
UNION ALL
SELECT *
FROM cte1
问题是:如何使用 ct1.col
和 ct2.col
而不是 a.col and b.col
执行第一个示例中的 vlookup?我正在尝试在两个查询输出之间执行 vlookup,而不将它们存储到临时表或表中。
我在想这样的事情:
; with cte1 as ( select pd.value as value from pentd pd
inner join aster a on a.aid = pd.aid
where pid = 10
and a.name = 'ls'),
with cte2 as( select pd.value as value from pentd pd
inner join aster a on a.aid = pd.aid
where pid = 15
and a.name = 'ls' )
select (select value from cte2),
(case
when cte1.value is null then 'FALSE' else 'TRUE' end)
from cte2
left outer join
(select distinct value from cte1) cte1
on cte1.value = cte2.value
但是这个returns对我来说(当然上面的代码只是一个例子,不能link真正的数据或输出)
Msg 512, Level 16, State 1, Line 1 Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
知道我想要实现的目标是否可行吗?如果可以,那么如何在避免错误的同时将其用于多个值?
我正在使用 SQL Server 2012,但我也对 SQL Server 2008 解决方案感兴趣。
cte1 - 此处用于连接的列,因为我只有 select 值
aid name value
10 ls 123.123
10 ls 422.433
10 ls 56.32
cte2 - 此处用于连接的列,因为我只有 select 值
aid name value
15 ls 123.123
15 ls 34.21
15 ls 21.256
预期输出
value
123.123
select (select value from cte2),
这一行是你的问题。嵌套查询(括号中的位)returns 多个值。您不能 select 表(意味着多行)作为单列。试试这个:
select cte2.value,
(case
when cte1.value is null then 'FALSE' else 'TRUE' end)
from cte2
left outer join
(select distinct value from cte1) cte1
on cte1.value = cte2.value
如果您需要过滤掉重复项,您可以将其更改为 select distinct cte2.value
,或者将 distinct
选项迁移到您的 CTE。