2

这是我获取总数的查询。

SELECT       
    MemberShipMaster.Id,   
    MemberShipMaster.Name,  
    TotalCount = 
    (
        SELECT
            ISNULL(COUNT(MemberPortfolio.Id), 0) AS total
        FROM
            JFPMembers
            INNER JOIN MemberPortfolio 
                ON JFPMembers.MemberID = MemberPortfolio.MemberId
        WHERE
            JFPMembers.MembershipType = MemberShipMaster.Id 
            AND JFPMembers.IsActive = 1
            AND MemberPortfolio.IsActive = 1
    )  
FROM
    MemberShipMaster
    Left JOIN JFPMembers
        ON MemberShipMaster.Id = JFPMembers.MembershipType   
WHERE
    MemberShipMaster.Id <> 6
GROUP BY
    MemberShipMaster.Id,
    MemberShipMaster.Name
Order by
    MemberShipMaster.Name   

下面是我的表架构

SELECT [MemberID]        -- PK 
    ,[MembershipType]  --FK , table [MemberShipMaster]
FROM JFPMembers

SELECT [Id]       -- PK
    ,[MemberId] -- FK OF Table JFPMembers         
FROM MemberPortfolio

SELECT [Id]  --PK
    ,[Name]
FROM MemberShipMaster

我需要的是成员 Portfolios 的总计数,按 MembershipType,即我的关系是这样的,JFPMembers 是保存成员信息的主表。会员分为 3 类,高级会员、企业会员和普通会员。这三种类型存储在 MemberShipMaster 中。ID 分别为 3、4 和 5。还有一张用于持有 Portfolio 的表,该表作为 MemberId 来关联其各自的成员。

但是我收到了 MemberShipMaster.Id 的一些多值或参考错误。请帮我...

4

1 回答 1

0

新代码:

SELECT abb1.Id, abb1.Name,   
TotalCount =  
(SELECT 
        ISNULL(COUNT(abb3.Id), 0) AS total 
    FROM 
        JFPMembers as abb4 
        INNER JOIN MemberPortfolio as abb3 
            ON abb4.MemberID = abb3.MemberId 
    WHERE 
        abb4.MembershipType = abb1.Id  
        AND abb4.IsActive = 1 
        AND abb3.IsActive = 1)   

FROM 
    MembershipMaster as abb1 
    Left JOIN JFPMembers as abb2
        ON abb1.Id = abb2.MembershipType    
WHERE 
    abb1.Id <> 6 
GROUP BY 
    abb1.Id, 
    abb1.Name 
Order by 
    abb1.Name  
于 2012-08-21T14:42:19.947 回答