SQL查询计算符合所需条件的人数

时间:2018-02-13 13:19:47

标签: sql

我正在开发一个拥有多个代理和成员的项目。现在我必须执行以下操作(通过SQL查询):

  1. 选择已完成付款的母代理商下的代理商和会员(总数为430,即130 + 150 + 150),为期3个月。
  2. 每个代理商都有n个成员,因此对于每个代理商,我需要检查所有成员是否支付了3个月的会费。
  3. 如果支付了会费,请返回主代理的总会员数代理商。
  4. 我尝试了以下查询,但没有运气:

    CREATE TABLE [dbo].[SlipDetails](
        [SlipDetailsID] [int] IDENTITY(1,1) NOT NULL,
        [SlipID] [int] NULL,
        [SlipNumber] [nvarchar](50) NULL,
        [AgentID] [int] NULL,
        [AgentName] [nvarchar](50) NULL,
        [MemberID] [int] NULL,
        [MemberName] [nvarchar](50) NULL,
        [MonthID] [int] NULL,
        [MonthAmount] [int] NULL,
        [LateFine] [int] NULL,
        [SubmittedDateByAgent] [datetime] NULL,
        [ApprovedByAdmin] [nvarchar](1) NULL,
        [ApprovedDate] [datetime] NULL,
        [MonthName] [nvarchar](50) NULL,
        [Blocked] [nvarchar](1) NULL,
     CONSTRAINT [PK_SlipDetails] PRIMARY KEY CLUSTERED 
    (
        [SlipDetailsID] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    ) ON [PRIMARY]
    
    GO
    
    CREATE TABLE [dbo].[MemberMaster](
        [MemberID] [int] IDENTITY(1,1) NOT NULL,
        [MemberName] [nvarchar](50) NULL,
        [MemberMobile] [varchar](50) NULL,
        [MemberEmail] [nvarchar](50) NULL,
        [MemberDOB] [nvarchar](50) NULL,
        [MemberDOJ] [nvarchar](50) NULL,
        [MemberGender] [nvarchar](50) NULL,
        [MemberGenderID] [int] NULL,
        [MemberAddress] [nvarchar](50) NULL,
        [MemberPhoto] [nvarchar](50) NULL,
        [IsFreeGift] [int] NULL,
        [GiftID] [int] NULL,
        [GiftName] [nvarchar](50) NULL,
        [AgentID] [int] NULL,
        [AgentName] [nvarchar](50) NULL,
        [CardID] [int] NULL,
        [CardNumber] [nvarchar](50) NULL,
        [SID] [int] NULL,
        [SName] [nvarchar](50) NULL,
        [Custom1] [nvarchar](50) NULL,
        [Custom2] [nvarchar](50) NULL,
        [IsActive] [nvarchar](1) NULL,
        [IsBlocked] [nvarchar](1) NULL,
     CONSTRAINT [PK_MemberMaster] PRIMARY KEY CLUSTERED 
    (
        [MemberID] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    ) ON [PRIMARY]
    
    GO
    
    CREATE TABLE [dbo].[AgentMaster](
        [AgentID] [int] IDENTITY(1,1) NOT NULL,
        [AgentName] [nvarchar](50) NULL,
        [Mobile] [nvarchar](50) NULL,
        [AgentUserName] [nvarchar](50) NULL,
        [AgentPassword] [nvarchar](50) NULL,
        [IsActive] [nvarchar](1) NULL,
        [ParentAgentID] [int] NULL,
        [ParentAgentName] [nvarchar](50) NULL,
        [BankName] [nvarchar](50) NULL,
        [AccountHolderName] [nvarchar](50) NULL,
        [IFSC] [nvarchar](50) NULL,
        [BranchName] [nvarchar](50) NULL,
        [AccountNo] [nvarchar](50) NULL,
        [AgentPhoto] [nvarchar](50) NULL,
        [DOJ] [datetime] NULL,
        [SelectedInDraw] [nvarchar](1) NULL,
     CONSTRAINT [PK_AgentMaster] PRIMARY KEY CLUSTERED 
    (
        [AgentID] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    ) ON [PRIMARY]
    

    Table Structure

    查询:

    SELECT COUNT(dbo.MemberMaster.MemberID) AS CMemberID
    FROM dbo.SlipDetails
         INNER JOIN dbo.MemberMaster ON dbo.SlipDetails.MemberID = dbo.MemberMaster.MemberID
    WHERE dbo.MemberMaster.AgentID = 203
    GROUP BY dbo.MemberMaster.AgentID,
             dbo.SlipDetails.MonthAmount
    HAVING(SUM(dbo.SlipDetails.MonthAmount) >= 430);  
    

    如何获得每个代理商下的会员的确切计数?任何帮助都会很棒。

3 个答案:

答案 0 :(得分:1)

Select mm.AgentName,Count(mm.MemberID) as MemberCount from MemberMaster mm
INNER JOIN
(Select AgentID,MemberId from SlipDetails
group by AgentID,MemberID
having Sum(MonthAmount)>=430)sd
on sd.AgentID=mm.AgentID and sd.MemberID=mm.MemberID
Group by mm.AgentID, mm.AgentName

甚至:

Select am.AgentName,MemberCount from AgentMaster am
inner join
(Select mm.AgentID,Count(mm.MemberID) as MemberCount from MemberMaster mm
INNER JOIN
(Select AgentID,MemberId from SlipDetails
group by AgentID,MemberID
having Sum(MonthAmount)>=430)sd
on sd.AgentID=mm.AgentID and sd.MemberID=mm.MemberID
Group by mm.AgentID)mm1
on am.AgentID=mm1.AgentID

答案 1 :(得分:1)

试试这个

    WITH SlipDetails_ttl as (
        Select st.MemberID, sum(MonthAmount) as MonthAmount_ttl 
            from SlipDetails as st
        group by st.MemberID
        having sum(MonthAmount)>=430
    )
    Select am.AgentID, count(stt.MemberID) 
    From SlipDetails_ttl as stt 
    join MemberMaster as mm on mm.MemberID = stt.MemberID
    join AgentMaster as am on mm.AgentID = am.AgentID
    group by am.AgentID

答案 2 :(得分:1)

尝试一下:

SELECT agt.AgentID, COUNT(*)
FROM dbo.AgentMaster agt -- Get all agents
INNER JOIN dbo.MemberMaster mbr ON agt.AgentID = mbr.AgentID -- Get each agents' members
INNER JOIN dbo.SlipDetails slp ON mbr.MemberID = slp.MemberID -- Get payment details for members
GROUP BY agt.AgentID
HAVING(SUM(slp.MonthAmount)) = 430 -- Only return members that have paid 430

一些假设/注释:

  • 仅查看每个座席的直接成员(不是递归,仅限一个级别)
  • 不考虑3个月的约束,只检查每个成员已支付430