简体   繁体   English

Reporting Services中的SQL聚合

[英]SQL Aggregation in Reporting Services

I am migrating a report to MS Reporting Services from powerbuilder with ingres database. 我正在使用ingres数据库将报告从powerbuilder迁移到MS Reporting Services。 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: powerbuider报告使用内置函数来运行total来输出数据,例如:

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. 基本上,要求是显示护理人员的总数,代号的孩子(按代号分组),还应显示10岁以下的儿童总数,以及按代号分组的10岁以上的儿童总数。 。

My question is how can I get the same thing in T-sql 2005 or in SSRS ? 我的问题是如何在T-sql 2005或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 ? 我可以得到carer_id和Child_id的不同的COUNT,然后将它们分组以获取护老者和孩子的总数,但是我无法得知年龄? If possible I do not want to use cursor should I use CTE? 如果可能,我不想使用游标,应该使用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 OUTPUT

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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