1

I have a query which returns some rows. Its column names are like: Age, Gender, DOB etc. What I have to do is, to check how many rows are coming from DB of which Age.

E.g. see the image:

enter image description here

See the age subtotal, it means my query is returning 54 rows of age 0, 1 row of age 1 and so on.

This table of subtotal must display only those ages that are returned by the query. For example there is no Age 2 as it was not returned by query.

How can I sort this issue?

Edit

Here is my Stored Procedure:

ALTER PROCEDURE [dbo].[spGetClients]
(
    @orderBy varchar(50),
    @startAge int,
    @endAge int,
    @sex varchar(5),
    @developmentalDisability varchar(200),
    @staffId nvarchar(1024), 
    @statusId nvarchar(1024), 
    @ethnicityId nvarchar(1024), 
    @treatmentProviderId nvarchar(1024)
)

AS
BEGIN
    SET NOCOUNT ON;
    
        SELECT c.Id, dbo.GetClientFullName(c.FirstName, c.MiddleInit, c.LastName) AS ClientName, 
        c.DateOfBirth, dbo.GetAge(c.DateOfBirth, GETDATE()) AS Age, c.Sex, 
        dbo.GetClientStatus(c.Id, @statusId) AS Status, ca.Address, co.Phone, 
        dbo.GetEthnicity(c.Id, @ethnicityId) AS Ethnicity, dbo.GetDevelopmentalDisabilities(c.Id, @developmentalDisability) AS Disabilities, 
        dbo.GetClientStaffContacts(c.Id, @staffId) AS Staff, dbo.GetClientContacts(c.Id) AS Contact, 
        dbo.GetClientInsuranceProviders(c.Id) AS HealthProvider, 
        dbo.GetClientTreatmentProviders(c.Id, @treatmentProviderId) AS TreatmentProvider
        FROM Client c  
        LEFT OUTER JOIN(        
            SELECT ca.ParentEntityId, ca.Address
            FROM ContactAddress ca
            INNER JOIN EntityName en ON en.Id = ca.EntityNameId AND en.Name = 'Client' 
            INNER JOIN GeneralLookup gl ON ca.glAddressTypeId = gl.Id AND gl.LookupItem = 'Primary'    
        ) ca ON c.Id = ca.ParentEntityId 
        LEFT OUTER JOIN(        
            SELECT co.ParentEntityId, co.ContactData Phone
            FROM ContactOther co
            INNER JOIN EntityName en ON en.Id = co.EntityNameId AND en.Name = 'Client' 
            INNER JOIN GeneralLookup gl ON co.glContactTypeId = gl.Id AND gl.LookupItem = 'Home'    
        ) co ON c.Id = co.ParentEntityId 
        LEFT OUTER JOIN GeneralStatus gs on gs.Id = c.StatusId
        where gs.Name <> 'Deleted' 
        and (dbo.GetAge(c.DateOfBirth, GETDATE()) BETWEEN @startAge and @endAge)
        and ((@sex = 'M' and c.sex = 'M') or (@sex = 'F' and c.Sex = 'F') or (@sex = 'Both' and (c.Sex in ('M', 'F', ''))))
        and ((@staffId = '') OR (dbo.GetClientStaffContacts(c.Id, @staffId) is not null))
        and ((@statusId = '') OR (dbo.GetClientStatus(c.Id, @statusId) is not null))
        and ((@ethnicityId = '') OR (dbo.GetEthnicity(c.Id, @ethnicityId) is not null))
        and ((@treatmentProviderId = '') OR (dbo.GetClientTreatmentProviders(c.Id, @treatmentProviderId) is not null))
        ORDER BY 
            CASE
                WHEN @orderBy = 'Consumer Name' THEN dbo.GetClientFullName(c.FirstName, c.MiddleInit, c.LastName) 
                WHEN @orderBy = 'Consumer Address' THEN ca.Address
            END
END

3 Answers 3

3
SELECT Age, count(Age) SubTotals
  FROM (sub query)
 GROUP BY Age

grouping by Age, the query will show Age and count(Age) = number of rows with that age

Update:

You can do this:

SELECT T.*, TA.Age_SubTotals
FROM (YOUR QUERY) T
JOIN (SELECT dbo.GetAge(c.DateOfBirth, GETDATE()) AS Age, count(*) Age_SubTotals
        FROM Client c
       GROUP BY Age) TA
     ON T.Age = TA.Age

This solution is not efficient at all (and I see that you will need to calculate many other subtotals). It's better to leave the proc as it is and do the counting of subtotals in your client application.

Sign up to request clarification or add additional context in comments.

5 Comments

it is not working in my case. Can you please see my updated question having query.
I'm using some UDFs in select statement
when I use this count(dbo.GetAge(c.DateOfBirth, GETDATE())) in my select statement, error occurs: Column 'c.Id' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
It wasn't clear from you initial question that you wanted the subtotals with the original rows. Is that what you want? i.e.: with each row you want an extra column containing the subtotal of it's age column?
manji to be honest, I don't know how I should get this subTotal? I'm confused how can I get it? That's the main issue.
0

Your query is pretty elaborate and the output seems to be for a report, couldn't you just elaborate your sub-total in the report? (SSRS Tutorial for groups and totals)

If it is not possible, i think you could modify your stored procedure to use a Table Variable: load the query in the table and then run the various subtotal query on it.

3 Comments

I need the data for a report, you are right. How can I calculate subtotals in SSRS? (Sequel Server Reporting Service i.e. rdlc file)
You have to create a group in your table or matrix and from that you can add subtotal. you should look at some tutorial about group if you have never done it. I'll add a link to the post
0

Without knowing your struture is going to be something like

SELECT count(age),age FROM `table` WHERE count(age) > 0 GROUP BY age ORDER BY age ASC

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.