简体   繁体   English

需要在嵌套子查询中使用查询列值

[英]Need to use query column value in nested subquery

EDIT 编辑
Sorry for the half post :( 对不起,半篇文章:(

It seems I cannot use a column from the parent query in a sub query. 看来我不能在子查询中使用父查询中的列。 How can I refactor this query to get what I need? 如何重构该查询以获取所需信息?

dbo.func_getRelatedAcnts returns a table of related accounts (all children from a given account). dbo.func_getRelatedAcnts返回相关帐户的表(来自给定帐户的所有子帐户)。 It goes up to the top level parent account of a given account and then selects all child accounts from there. 它会转到给定帐户的顶级父帐户,然后从那里选择所有子帐户。 This gives me a table of all the parent, sibling, and child account ids. 这为我提供了所有父级,同级和子级帐户ID的表格。

Events has a foreign key to an Account and Profiles has a foreign key to accounts. 事件具有帐户的外键,个人档案具有帐户的外键。

Registrations have a foreign profile key and event key. 注册具有外部配置文件密钥和事件密钥。 There can be multiple regs per profile. 每个配置文件可以有多个注册表。 I need to find all the regs that are associated with profiles that are in accounts that are not in any way related hierarchically to the event the registration is in. 我需要查找与帐户中的配置文件相关联的所有reg,这些帐户与注册所处的事件没有任何层次关系。

The problem is that I want to use the profile account key of profile related to the reg in the parent query to dynamically grab the table in the subquery. 问题是我想使用与父查询中reg相关的配置文件的配置文件帐户密钥来动态获取子查询中的表。

SELECT COUNT(r.reg_id)  
FROM registrations r 
JOIN profiles p ON (r.reg_frn_pro_id = p.pro_id)  
JOIN events e ON (r.reg_frn_evt_id = e.evt_id)  
WHERE evt_frn_acnt_id NOT IN 
      (SELECT * FROM dbo.func_getRelatedAcnts(p.pro_frn_acnt_id))  

My error: 我的错误:
pro_frn_acnt_id is not a recognized table hints option. pro_frn_acnt_id不是公认的表提示选项。 If it is intended as a parameter to a table-valued function, ensure that your database compatibility mode is set to 90. 如果要将其用作表值函数的参数,请确保将数据库兼容模式设置为90。

Not entirely sure why you have two sets of parentheses, and unsure of what you're trying to match up, but one of the following two should work, and if not, it will give you the general idea of what needs to be done. 不能完全确定为什么要使用两组括号,也不能确定要匹配的括号,但是以下两种方法之一应该可以工作,如果不能,那么它将为您提供需要完成操作的一般思路。

1) 1)

SELECT COUNT(r.reg_id)
FROM registrations r JOIN profiles p ON (r.reg_frn_pro_id = p.pro_id)  
JOIN events e ON (r.reg_frn_evt_id = e.evt_id)  
WHERE evt_frn_acnt_id NOT IN 
    ( SELECT pro_frn_acnt_id FROM dbo.func_getRelatedAcnts )

2) 2)

SELECT COUNT(r.reg_id)
FROM registrations r JOIN profiles p ON (r.reg_frn_pro_id = p.pro_id)  
JOIN events e ON (r.reg_frn_evt_id = e.evt_id)  
WHERE evt_frn_acnt_id NOT IN 
    ( SELECT evt_frn_acnt_id FROM dbo.func_getRelatedAcnts )

This works for me: 这对我有用:

CREATE FUNCTION fn_tvf(@input INT)
RETURNS TABLE
AS
        RETURN
        (
        SELECT  @input AS id
        UNION ALL
        SELECT  @input + 1
        )

GO

;WITH    q AS
        (
        SELECT  1 AS id
        ),
        t AS
        (
        SELECT  3 AS id
        )
SELECT  *
FROM    q
CROSS JOIN
        t
WHERE   t.id NOT IN (SELECT  * FROM fn_tvf(q.id))

What is exact error message you get? 您得到的确切错误消息是什么?

Ok. 好。 I created an additional funciton that takes two args, one to create the table of related acnt_ids and another to look for in the resulting table. 我创建了一个附加函数,它需要两个参数,一个用于创建相关acnt_ids的表,另一个用于在结果表中查找。 With this new function I could reformulate the query like so 有了这个新功能,我可以像这样重新编写查询

SELECT COUNT(r.reg_id)
FROM registrations r 
JOIN profiles p ON (r.reg_frn_pro_id = p.pro_id)  
JOIN events e ON (r.reg_frn_evt_id = e.evt_id)  
WHERE dbo.func_checkAcntRelation(p.pro_frn_acnt_id, e.evt_frn_acnt_id) = 0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM