0

我在构建查询时遇到了一些麻烦,我需要从 FamilyAccountDetails 表中返回 AccountID,其中家庭帐户至少有一个孩子(一个孩子是 AccountDetails.AccountID 等于 FamilyAccountDetails.AccountID),其 HoldFlag = 0。

AccountDetails
EntID  |HoldFlag|AccountID|StatusFlag

FamilyAccountDetails
EntID|FamilyAccountID|AccountID|StatusFlag

SQL:

      Select FD.AccountID, FD.FamilyAccountId 
from Test2TMAdvisor5.dbo.FamilyAccountDetails FD
      where FD.EnterpriseId = 'TestDan'
      and AccountId in (Select AccountId from Test2TMAdvisor5.dbo.AccountDetails AD where Ad.AccountID = FD.AccountId and ad.HoldFlag =0)
4

2 回答 2

1
SELECT
    FD.[AccountID]
    ,FD.[FamilyAccountId] 
FROM
    FamilyAccountDetails FD
WHERE
    FD.[EnterpriseId] = 'TestDan'
    AND EXISTS 
        (
        SELECT 
            * 
        FROM 
            AccountDetails AD
        WHERE 
            AD.[AccountID] = FD.[AccountId] 
            AND AD.[HoldFlag] = 0
        )
于 2012-06-28T23:11:03.637 回答
0

此 SQL 查询应提取您想要的结果:

SELECT
    FD.[AccountID]
    ,FD.[FamilyAccountId] 
FROM
    FamilyAccountDetails FD
INNER JOIN AccountDetails AD on AD.[AccountID] = FD.[AccountId] 
WHERE
    FD.EntID = 'TestDan'
    AND AD.[HoldFlag] = 0
于 2012-06-29T02:30:30.327 回答