简体   繁体   中英

SQL Aggregation in Reporting Services

I am migrating a report to MS Reporting Services from powerbuilder with ingres database. I have the following dataset:

Code| Code Description | Carer_Id| Child_id | Child_age
=======================================================
738  Workflow           1234       2345        10
738  Workflow           1234       2346        15
739  Estimate           1235       2367        10

The powerbuider report uses built-in functions for running total to output data like:

code | Code Description | carer | child | age 0 < 10 | age 10 > 15
738   Workflow             1        2        1          1
==================================================================
739   Estimate             1        1        1          0

Basically the requirement is to show total number of carers, children for the code (group by code) but also to show the total number of children under the age of 10, and total number of children between the age of 10 and above grouped by code.

My question is how can I get the same thing in T-sql 2005 or in SSRS ? I can get the distinct COUNT of the carer_id and Child_id and then group them to get me the total numbers of carers and children but I can't get the age ? If possible I do not want to use cursor should I use CTE? Please any advise would be highly appreciated. Thanks in advance.

SELECT  Code,
        [Code Description],
        COUNT(DISTINCT carer_ID) Carer,
        COUNT(DISTINCT Child_ID) child,
        SUM(CASE WHEN Child_Age BETWEEN 0 AND 10 THEN 1 ELSE 0 END) [age 0 < 10],
        SUM(CASE WHEN Child_Age BETWEEN 11 AND 15 THEN 1 ELSE 0 END) [age 10 > 15]
FROM    TableName
GROUP   BY Code, [Code Description]

OUTPUT

╔══════╦══════════════════╦═══════╦═══════╦════════════╦═════════════╗
║ CODE ║ CODE DESCRIPTION ║ CARER ║ CHILD ║ AGE 0 < 10 ║ AGE 10 > 15 ║
╠══════╬══════════════════╬═══════╬═══════╬════════════╬═════════════╣
║  738 ║ Workflow         ║     1 ║     2 ║          1 ║           1 ║
║  739 ║ Estimate         ║     1 ║     1 ║          1 ║           0 ║
╚══════╩══════════════════╩═══════╩═══════╩════════════╩═════════════╝

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM