润乾报表实现预置分组报表及改进
预置分组报表在报表开发中比较常见而且比较棘手。所谓预置分组报表是指分组没有规律,可能是分组范围没有规律,也可能分组顺序没有规律,需要报表开发人员事先预置(固定)分组的报表。常见的预置分组报表一般有如下三类:按段分组报表、固定次序分组报表、
预置分组报表在报表开发中比较常见而且比较棘手。所谓预置分组报表是指分组没有规律,可能是分组范围没有规律,也可能分组顺序没有规律,需要报表开发人员事先预置(固定)分组的报表。常见的预置分组报表一般有如下三类:按段分组报表、固定次序分组报表、多层固定次序分组报表。下面分别看一下这几类报表的特点,以及润乾报表的实现方法。
按段分组报表
这类报表的特点是分段的区间并未在数据库中存储需要根据相应字段(如年龄、日期)计算,并且每个分段可能会发生变化(如每年节假日所在日期可能不同),经常由用户随意指定。如:统计20-30岁,30-40岁,40-50岁各年龄段的用户数量;每年节假日(春节、端午节、国庆节)期间公路铁路承载的旅客统计。
下面通过实例说明,报表样式如下:
该报表是根据订单表统计各(预置)时间段内,各地区的订单数量、订单金额汇总。其中各时间段范围为:
【1996年圣诞前:date
1996年圣诞--1997年国庆:1996-12-25
1997年国庆--1998年五一:1997-10-1
1998年五一以后:date>1998-5-1】
实现
润乾报表实现该类按段分组报表主要使用ds.plot()函数,计算一个值在一个完全划分中的位置。报表模板及其表达式如下:
其中,参数arg1为日期组,默认值为:1996-12-25,1997-10-1,1998-5-1。
设置B1显示值表达式:map(to(0,count(arg1)+1),arg2),其中参数arg2为字符串组,默认值为:1996年圣诞前,1996年圣诞--1997年国庆,1997年国庆--1998年五一,1998年五一以后。
使用ds.plot()函数可以非常方便地实现上述报表,而且可以通过该函数的参数控制是否包含边界,对于分组较少并且边界包含规律的分组报表尤其适用。不过,当段界包含不规律时则无法使用ds.plot()实现,比如上例中如果时间范围变成:
【1996年圣诞前:date
1996年圣诞--1997年国庆:1996-12-25
1997年国庆--1998年五一:1997-10-1
1998年五一以后:date>1998-5-1】
起始和结束两段不包含段界日期,而中间的各个分段则包含,ds.plot()函数则存在限制,难以实现这类问题。
固定次序分组报表
这类报表的特点是用户要求分组次序固定,而数据库中并未存储相应的分组字段。如:要统计长三角地区、珠三角地区以及环首都经济圈内客户的订单情况。数据库客户表中只有城市(如:北京、上海、石家庄)和地区(如:华北、东北、华南)字段,如果单纯为该报表在数据库里增加另外一个地区字段显然是不值得的,所以任务自然落到报表端了。
下面通过一个实例来说明使用润乾报表实现这类报表的过程,报表样式如下:
要求:
1、 根据源数据中的城市分区分组汇总,其中:
环首都经济圈包括:[北京,天津,张家口,承德,保定,廊坊,唐山,秦皇岛,衡水,沧州,石家庄]
长三角地区包括:[上海,南京,苏州,无锡,常州,镇江,南通,扬州,泰州,杭州,宁波,湖州,嘉兴,绍兴,舟山,台州]
珠三角地区包括:[广州,深圳,珠海,佛山,江门,东莞,中山,惠州,肇庆]
其他城市列为其他地区;
2、报表显示的地区顺序固定,即上图所示顺序显示,这里显然无法通过按字母顺序升降来排序。
实现
该类分组报表主要使用润乾报表的ds.overlap()函数实现,报表模板及其表达式如下:
其中,A2的表达式为:
=ds1.overlap(true,城市 in list("北京","天津","张家口","承德","保定","廊坊","唐山","秦皇岛","衡水","沧州","石家庄"),"环首都经济圈",城市 in list("上海","南京","苏州","无锡","常州","镇江","南通","扬州","泰州","杭州","宁波","湖州","嘉兴","绍兴","舟山","台州"),"金三角",城市 in list("广州","深圳","珠海","佛山","江门","东莞","中山","惠州","肇庆"),"珠三角","其他")
可以看到ds.overlap()函数的强大,对于固定的分组以及分组下成员较少的情况尤其适用。但当分组或分组成员过多时,继续使用该函数的可读性就太差了,难于维护;此外,本例中看到的是一层分组,如果要实现多层固定次序分组报表,ds.overlap()函数则无法直接实现。下面看一个例子。
多层固定次序分组报表
再修改一下上例的报表需求,要求按地区和城市分组,且地区和城市的展现顺序固定,如下顺序(没有则不显示):
环首都经济圈包括:[北京,天津,张家口,承德,保定,廊坊,唐山,秦皇岛,衡水,沧州,石家庄]
长三角地区包括:[上海,南京,苏州,无锡,常州,镇江,南通,扬州,泰州,杭州,宁波,湖州,嘉兴,绍兴,舟山,台州]
珠三角地区包括:[广州,深圳,珠海,佛山,江门,东莞,中山,惠州,肇庆]
报表样式如下:
该报表由于存在两层固定次序分组(地区、城市),所以无法使用ds.overlap()直接实现,需要先将地区在报表中列出,继而通过ds.overlap()实现,报表模板及其表达式如下:
其中,B2、B3、B4表达式分别为:
B2:=ds1.overlap(true,城市=="北京","北京",城市=="天津","天津",城市=="张家口","张家口",城市=="承德","承德",城市=="保定","保定",城市=="廊坊","廊坊",城市=="唐山","唐山",城市=="秦皇岛","秦皇岛",城市=="衡水","衡水",城市=="沧州","沧州",城市=="石家庄","石家庄")
B3:=ds1.overlap(true,城市=="上海","上海",城市=="南京","南京",城市=="苏州","苏州",城市=="无锡","无锡",城市=="常州","常州",城市=="镇江","镇江",城市=="南通","南通",城市=="扬州","扬州",城市=="泰州","泰州",城市=="杭州","杭州",城市=="宁波","宁波",城市=="湖州","湖州",城市=="嘉兴","嘉兴",城市=="绍兴","绍兴",城市=="舟山","舟山",城市=="台州","台州")
B4:=ds1.overlap(true,城市=="广州","广州",城市=="深圳","深圳",城市=="珠海","珠海",城市=="佛山","佛山",城市=="江门","江门",城市=="东莞","东莞",城市=="中山","中山",城市=="惠州","惠州",城市=="肇庆","肇庆")
可以看到,对于多层固定次序分组报表润乾报表也是可以实现的,只是表达式相对复杂,当(地区)分组较多的时候,这种实现的代价会相对较高。
对于上述三类预置分组报表,润乾报表均可以实现,有些做起来也很简单,这也是润乾报表的强大之处(其它报表工具将会更为困难)。但对于一些更复杂的情况,无论是边界包含无规律、表达式过于复杂,还是多层预置分组难于实现,使用润乾报表解决起来就都不那么轻松了。究其原因,是因为报表工具把数据源准备和呈现混在一起导致(需要在报表模板中编写大量复杂计算逻辑),如果能将这两部分分开,则会使报表制作变得清晰、简单。
为解决这些问题,润乾公司对润乾报表进行了完善,推出润乾集算报表5.0版,在保留了润乾报表已有优势后,还在其中内置了独立的计算引擎:集算器,用于解决数据源准备问题。在集算报表中可以将复杂的数据计算(数据源准备)工作交由集算器完成,可将其视为报表的计算层;而报表本身只负责接收计算结果后进行展现(有时包括一些简单计算),从而将数据源准备和报表呈现分开,使得报表制作更为清晰。
下面用集算报表再实现上述三个例子:
按段分组报表
与润乾报表实现时不同之处在于,这里考虑边界无规律的情况,即:
【1996年圣诞前:date
1996年圣诞--1997年国庆:1996-12-25
1997年国庆--1998年五一:1997-10-1
1998年五一以后:date>1998-5-1】
起始和结束两段不包含段界日期,而中间的各个分段则包含。
编写计算脚本
首先使用集算器编写计算脚本,完成分组并返回分组汇总结果:
A2:按照arg1指定的分段进行分组A1:取出订单明细表数据
A3:再按照地区分组,并进行分组汇总计算订单数量和订单金额
A5:为结果集增加时间段字段,即每个分段的显示名称,如:“1996年圣诞前”、“1998年五一以后”
A6:为报表返回的ResultSet
报表调用
使用集算报表设计器,新建报表,使用“集算器”数据集类型,选择上面编辑好的集算脚本(按段分组.dfx):
其中,arg1、arg2为集算脚本参数,param1和param2为报表参数。
设置报表模板及其表达式如下:
在报表中只进行简单的分组汇总,即可完成报表的制作。
使用集算报表实现的最大好处是灵活,无论段界包含与否,均可以使用P.enum()函数实现。所以这里看到,在集算报表中将数据准备(集算脚本)和报表呈现(报表模板)分开带来的好处,在计算层通过集算器完备的计算体系完成数据源准备的工作,在展现层通过报表模板专司展现,层次清晰、实现简单。
下面给出剩余两类报表的实现方法,其他不再赘述。
固定次序分组报表
编写集算脚本

A9:计算各组汇总值
A10:为报表返回结果集
报表调用
设置报表模板及表达式
相对润乾报表的实现,在集算报表中可以逐步(分步编写代码)实现,从而避免过于冗长的表达式带来的编写和维护问题;而且集算器对集合运算的有效支持使得实现更加简单,如这里通过P.align()函数对订单数据按照固定顺序的地区集合进行对位分组,继而完成汇总的代码就体现了对集合运算支持的有效性和简单性。无论对于开发实现,还是后期报表维护都非常容易。
多层固定次序分组报表
编写计算脚本

报表调用
设置报表模板及表达式
本例则进一步体现了集算报表中内置的集算器的强大计算能力,对于无法直接在报表工具中完成的多层固定顺序的分组计算,在集算器中通过前面提到的灵活、分步以及本例中完备的计算能力等特点,使实现变得非常简单。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



This AI-assisted programming tool has unearthed a large number of useful AI-assisted programming tools in this stage of rapid AI development. AI-assisted programming tools can improve development efficiency, improve code quality, and reduce bug rates. They are important assistants in the modern software development process. Today Dayao will share with you 4 AI-assisted programming tools (and all support C# language). I hope it will be helpful to everyone. https://github.com/YSGStudyHards/DotNetGuide1.GitHubCopilotGitHubCopilot is an AI coding assistant that helps you write code faster and with less effort, so you can focus more on problem solving and collaboration. Git

How to implement dual WeChat login on Huawei mobile phones? With the rise of social media, WeChat has become one of the indispensable communication tools in people's daily lives. However, many people may encounter a problem: logging into multiple WeChat accounts at the same time on the same mobile phone. For Huawei mobile phone users, it is not difficult to achieve dual WeChat login. This article will introduce how to achieve dual WeChat login on Huawei mobile phones. First of all, the EMUI system that comes with Huawei mobile phones provides a very convenient function - dual application opening. Through the application dual opening function, users can simultaneously

The programming language PHP is a powerful tool for web development, capable of supporting a variety of different programming logics and algorithms. Among them, implementing the Fibonacci sequence is a common and classic programming problem. In this article, we will introduce how to use the PHP programming language to implement the Fibonacci sequence, and attach specific code examples. The Fibonacci sequence is a mathematical sequence defined as follows: the first and second elements of the sequence are 1, and starting from the third element, the value of each element is equal to the sum of the previous two elements. The first few elements of the sequence

On March 3, 2022, less than a month after the birth of the world's first AI programmer Devin, the NLP team of Princeton University developed an open source AI programmer SWE-agent. It leverages the GPT-4 model to automatically resolve issues in GitHub repositories. SWE-agent's performance on the SWE-bench test set is similar to Devin, taking an average of 93 seconds and solving 12.29% of the problems. By interacting with a dedicated terminal, SWE-agent can open and search file contents, use automatic syntax checking, edit specific lines, and write and execute tests. (Note: The above content is a slight adjustment of the original content, but the key information in the original text is retained and does not exceed the specified word limit.) SWE-A

Go language development mobile application tutorial As the mobile application market continues to boom, more and more developers are beginning to explore how to use Go language to develop mobile applications. As a simple and efficient programming language, Go language has also shown strong potential in mobile application development. This article will introduce in detail how to use Go language to develop mobile applications, and attach specific code examples to help readers get started quickly and start developing their own mobile applications. 1. Preparation Before starting, we need to prepare the development environment and tools. head

How to implement the WeChat clone function on Huawei mobile phones With the popularity of social software and people's increasing emphasis on privacy and security, the WeChat clone function has gradually become the focus of people's attention. The WeChat clone function can help users log in to multiple WeChat accounts on the same mobile phone at the same time, making it easier to manage and use. It is not difficult to implement the WeChat clone function on Huawei mobile phones. You only need to follow the following steps. Step 1: Make sure that the mobile phone system version and WeChat version meet the requirements. First, make sure that your Huawei mobile phone system version has been updated to the latest version, as well as the WeChat App.

In today's software development field, Golang (Go language), as an efficient, concise and highly concurrency programming language, is increasingly favored by developers. Its rich standard library and efficient concurrency features make it a high-profile choice in the field of game development. This article will explore how to use Golang for game development and demonstrate its powerful possibilities through specific code examples. 1. Golang’s advantages in game development. As a statically typed language, Golang is used in building large-scale game systems.

PHP Game Requirements Implementation Guide With the popularity and development of the Internet, the web game market is becoming more and more popular. Many developers hope to use the PHP language to develop their own web games, and implementing game requirements is a key step. This article will introduce how to use PHP language to implement common game requirements and provide specific code examples. 1. Create game characters In web games, game characters are a very important element. We need to define the attributes of the game character, such as name, level, experience value, etc., and provide methods to operate these
