sql - 向 CTE 添加子查询
问题描述
declare @nodeid int = '1';
with cte as
(
select
cust_ID, name, null lnode, null rnode
from
user_detail
where
cust_ID = @nodeid
union all
select
t.cust_ID, t.name,
isnull(cte.lnode, case when t.joinside = 0 then 1 else 0 end)
lnode,
isnull(cte.rnode, case when t.joinside = 1 then 1 else 0 end),
rnode
from
user_detail t
inner join
cte on cte.cust_ID = t.parentid
)
select
cust_ID, name
from
cte
where
rnode = '0'
option (maxrecursion 0)
当前情景:
我想要的是:
我将解释我想要什么:上面的查询仅从user_detail
表中获取结果。我想以这样一种方式修改查询,它还将在installments
表中搜索status
相应的列cust_id
。
修改后的查询将从基于的分期付款表中获取状态值cust_id
。
查询将在第三列中显示结果,如第二个屏幕截图所示。
我对 CTE 和嵌套查询不是很熟悉。我希望你们能理解我的问题。
解决方案
你可以试试下面 -
declare @nodeid int = '1';
with cte as (
select cust_ID, name,null lnode, null rnode from user_detail where
cust_ID = @nodeid
union all select t.cust_ID,t.name, ISNULL(cte.lnode, CASE WHEN t.joinside =
0 THEN 1 ELSE 0 END)
lnode, ISNULL(cte.rnode, CASE WHEN t.joinside = 1 THEN 1 ELSE 0 END)
rnode from user_detail t inner join cte on cte.cust_ID = t.parentid )
select cust_ID,name,status from cte c
inner join installment i on c.cust_id=i.cust_id
where rnode='0'
option (maxrecursion 0)
推荐阅读
- unit-testing - 如何在不向每个规则添加 EOF 的情况下测试 ANTLR 翻译
- json - 如何使用 Javascript 做简单的预测应用程序?
- c# - 强制创建中间表“Blog_Post”
- linux - 为什么accept() 阻塞,而listen() 是TCP 中最先涉及的?
- powerbi - 从单个 Excel 工作表创建三个表
- django - Wagtail - 如何保存使用包含 M2M 的 InlinePanel 的记录
- angular - 将图标放在搜索字段中作为 Angular 中的按钮
- java - 如何在spring java中为jira api处理json列表
- node.js - 请求超时问题 - Node/Heroku
- dlookup - 我在使用“dlookup”时遇到问题