【问题标题】:How to join this two functions result sets?如何加入这两个函数的结果集?
【发布时间】:2013-04-22 21:57:31
【问题描述】:

我有两个功能:

fn_get_AB_associations(Date from, Date to, Int AentityId)

使用这些字段获取结果集:

datefrom | AentityId | BentityId

fn_get_BC_associations(Date from, Date to, Int BentityId)

使用这些字段获取结果集:

datefrom | BentityId | CentityId

我需要在日期范围内选择与 A 实体关联的所有 C 实体。

我试图做类似的事情:

select DISTINCT T1.BentityId from dbo.fn_get_AB_associations('2013-04-01', '2013-04-15', 'PF')  T1
INNER JOIN fn_get_BC_associations('2013-04-01', '2013-04-15', T1.BentityId)  T2
ON T1.BentityId = T2.BentityId

但我显然得到了这个错误:The multi-part identifier "T1.BentityId" could not be bound.

那么...有没有办法加入这两个结果集,或者我必须循环第一个函数的结果并为每个函数调用第二个函数?

【问题讨论】:

    标签: sql-server tsql function sql-server-2005 join


    【解决方案1】:

    试试这个 -

    DECLARE 
          @DateStart DATETIME
        , @DateEnd DATETIME
    
    SELECT    
          @DateStart = '2013-04-01'
        , @DateEnd = '2013-04-15'
    
    SELECT DISTINCT 
          t1.DateFrom
        , t1.AentityId
        , t1.BentityId
        ,  t2.* 
    FROM dbo.fn_get_AB_associations(@DateStart, @DateEnd, 'PF') t1
    OUTER APPLY (
        SELECT 
              t2.DateFrom
            , t2.CentityId
        FROM dbo.fn_get_BC_associations(@DateStart, @DateEnd, t1.BentityId) t2
        WHERE t1.BentityId = t2.BentityId
    ) t2
    

    【讨论】:

    • 完美!非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2013-11-08
    • 2017-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-14
    • 2021-01-16
    • 2013-05-21
    相关资源
    最近更新 更多