简体   繁体   English

如何在Birt中添加总行?

[英]How to add total row in Birt?

I have a data set that display the Name and the value and sorted by Name. 我有一个显示名称和值并按名称排序的数据集。

How can I add row for sum on the birt report? 如何在Birt报告中添加总和行? Something like this. 这样的事情。

Name  |  Val
    ABC   |   3
    ABC   |   2
    ABC   |   1
    Total |   6
    DEF   |   3
    DEF   |   2
    Total |   5

I tried to do this by grouping the Name column but the output shows like this. 我试图通过对“名称”列进行分组来做到这一点,但是输出显示如下。

Name  |  Val
    ABC   |   3
    ABC   |   2
    ABC   |   1
    Total |   6

    Name  |  Val
    DEF   |   3
    DEF   |   2
    Total |   5

You may try doing GROUP BY with ROLLUP : 您可以尝试使用ROLLUP进行GROUP BY

WITH cte AS (
    SELECT Name AS OrigName, SUM(Val) AS Val,
        ROW_NUMBER() OVER (PARTITION BY NAME ORDER BY SUM(Val) DESC) rn
    FROM yourTable
    GROUP BY ROLLUP(Name, Val)
)

SELECT
    CASE WHEN rn=1 AND OrigName IS NOT NULL THEN 'Total'
         WHEN OrigName IS NULL THEN 'Grand Total'
         ELSE OrigName END AS NAME,
    Val
FROM cte
ORDER BY
    OrigName, Val;

在此处输入图片说明

Demo 演示版

While Tim's answer is technically correct, you do not have to create such data in SQL, especially, it's not going to be pretty during formatting (you will have a lot of if (name == "Total" || name == "Grand Total") etc. 尽管Tim的回答在技术上是正确的,但您不必在SQL中创建此类数据,尤其是在格式化期间它不会很漂亮(您会遇到很多if (name == "Total" || name == "Grand Total")

BIRT tables have things called groups . BIRT表具有称为groups东西。 You can say that you are grouping by name, and every group can have it's header and/or footer. 您可以说您是按名称分组,每个组都可以有其页眉和/或页脚。 You choose footer and set total in there. 您选择页脚并在那里设置总计。

You can read more here: https://help.eclipse.org/mars/index.jsp?topic=%2Forg.eclipse.birt.doc%2Fbirt%2Fsg-DisplayTheTotalNumberOfCustomersInTheReport.html (See Fig. 7-12) 您可以在此处了解更多信息: https : //help.eclipse.org/mars/index.jsp?topic=% 2Forg.eclipse.birt.doc%2Fbirt%2Fsg-DisplayTheTotalNumberOfCustomersInTheReport.html(请参见图7-12)

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

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