Search code examples
sql-servert-sqlviewcountcalculated-columns

Performing COUNT() on a computed Column from a VIEW


So all I want to do is have a view that shows how many kid between and including the age of 5 - 18 are in each family. I AM USING SQL SERVER.

The view I Have written to get the Family Members Ages is

CREATE VIEW VActiveMembers
AS
    SELECT 
        TM.intMemberID AS intMemberID,
        TM.strFirstName AS strFirstName,
        TM.strLastName AS strLastName,
        TM.strEmailAddress AS strEmailAddress,
        TM.dtmDateOfBirth AS dtmDateOfBirth,
        FLOOR(DATEDIFF(DAY, dtmDateOfBirth, GETDATE()) / 365.25) AS intMemberAge
    FROM 
        TMembers AS TM
    WHERE 
        TM.intStatusFlagID = 1

intStatusFlag = 1 is just a flag that means the member is active.

Now I have tried for about 3ish hours to figure this out but I cannot figure it out. Here is the one where instead of trying to get the solution in one fowl swoop I tried to step wise it, but then I still didn't get the result I wanted.

As you can see I didn't use the view where I calculated the AGE from because the "Multi-part Identifier could not be bound" I have seen that error but I couldn't get it to go away in this case. Ideally I would like the count to be performed on the VIEW instead of recalculating the ages all over again

CREATE VIEW VActiveFamilyMembersK12Count
AS
    SELECT
        TF.intParishFamilyID,
        COUNT(DATEDIFF(DAY, dtmDateOfBirth, GETDATE()) / 365) AS intMemberAgeCount
    FROM 
        TFamilies AS TF
    INNER JOIN 
        TFamilyMembers AS TFM
    INNER JOIN 
        VActiveMembers AS vAM ON (TFM.intMemberID = vAM.intMemberID)
        ON (TFM.intParishFamilyID = TF.intParishFamilyID) 
    WHERE 
        TF.intStatusFlagID = 1
    GROUP BY 
        TF.intParishFamilyID

I wanted to just get a count using the age calculation just to see If I could get a correct count of members in a family, then I could start building upon that to get a count of members of a certain age. The result I get back is 2 but there are guaranteed 3 members to each family.

The result I am looking For is this

Family_ID    |     K12Count
-----------------------------
1001         |        2
1002         |        0
1003         |        1
1004         |        0 

Here is a list of resources I looked up trying to figure this out, maybe one of them is in fact the answer and I just don't see it, but I am at a loss at the moment.

SQL Select Count from below a certain age

How to get count of people based on age groups using SQL query in Oracle database?

Count number of user in a certain age's range base on date of birth

Conditional Count on a field

http://timmurphy.org/2010/10/10/conditional-count-in-sql/

*** EDIT ***

CREATE VIEW VActiveFamilyMembersK12Count
AS
SELECT 
TF.intParishFamilyID, 
SUM(CASE WHEN intMemberAge >= 5 AND intMemberAge <= 18 THEN 1 ELSE 0 END) AS intK12Count
FROM 
TFamilies AS TF
    INNER JOIN TFamilyMembers AS TFM
        INNER JOIN VActiveMembers AS vAM
        ON (TFM.intMemberID = vAM.intMemberID)
    ON (TFM.intParishFamilyID = TF.intParishFamilyID) 

WHERE 
TF. intStatusFlagID = 1

GROUP BY 
TF.intParishFamilyID

GO

THIS IS THE SOLUTION ABOVE.


Solution

  • Conditional count is the way to go. Something like:

    SELECT intParishFamilyID, 
    COUNT(CASE WHEN intMemberAge >=5 and intMemberAge <=18 THEN 1 ELSE 0 END)
    FROM 
        TFamilies AS TF
            INNER JOIN TFamilyMembers AS TFM
                INNER JOIN VActiveMembers AS vAM
                ON (TFM.intMemberID = vAM.intMemberID)
            ON (TFM.intParishFamilyID = TF.intParishFamilyID) 
    
    WHERE 
        TF. intStatusFlagID = 1
    
    GROUP BY 
        TF.intParishFamilyID