简体   繁体   中英

SQL server report builder 3.0 dynamic grouping and sum

So I'm trying to make a report and I want to group my row using certain condition. This is what I have

    Code    July,1    July,2  SC   BF
    20       5          2      1    6
    21       4          1      4    1
    31       4          7      9    2
    35       5          1      6    0

What I want to do is sum for the twenties, thirties and etc. like

    Code    July,1    July,2  SC   BF
    20       5          2      1    6
    21       4          1      4    1
    2*       9          3      5    7
    31       4          7      9    2
    35       5          1      6    0
    3*       9          8      15   2

Or possibly

    Code    July,1    July,2  SC   BF
    20       5          2      1    6
    21       4          1      4    1
    2*                         5    7
    31       4          7      9    2
    35       5          1      6    0
    3*                         15   2

Thank you in advance

In ssrs report You can add a calculated field in your dataset with field name as Group and expression as =Floor(Fields!code.Value/10) and then in tablix do your sum bassed on that grouping. 添加表达

创建Tablix

结果

One way to handle this is in your SQL setup a column you can group by. In this case if Code is an integer, you can divide by 10 and it will group all the twenties with 2 and thirties with 3:

create table #test
(
    intValue int,
    sumValue int
)

insert into #test
select 20, 9
union all select 21, 10
union all select 30, 1
union all select 31, 2

select * , round(intValue/10,0) as GroupByThis
from #test
select Code    July,1    July,2  SC   BF from tablename
union 



select DISTINCT Code/10 ,  (select SUM (July,1) over (Code/10) from tablename where code=t.code)July,1   ,    (select SUM (July,2) over (Code/10) from tablename where code=t.code) July,2 ,  (select SUM (SC) over (Code/10) from tablename where code=t.code) SC,  (select SUM (BF) over (Code/10) from tablename where code=t.code) BF from tablename t

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