SQL 水晶报表 - 分组依据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6091302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Crystal Reports - Group By
提问by user765215
I'm using Visual Studio 2008 and would like to present the following SQL query in a report:
我正在使用 Visual Studio 2008 并希望在报告中显示以下 SQL 查询:
select name, count(*) from mytable group by name;
I can achieve this by creating a dataset that is essentially the above query (consisting of columns 'name' and 'count'), however it seems overkill to create an additional dataset just for this query. I'd rather do what I've done with other reports in my project and that is have a dataset that is the entire table:
我可以通过创建一个基本上是上述查询的数据集(由列“名称”和“计数”组成)来实现这一点,但是仅为此查询创建一个额外的数据集似乎有点过分。我宁愿做我对项目中的其他报告所做的事情,并且有一个包含整个表格的数据集:
select * from mytable;
And then use Crystal Report features to perform the grouping. Howver I can't find a way to do this grouping. I assume this is possible? This would allow me to reuse the dataset for other areas in the project rather than having to create a unique dataset for each report.
然后使用水晶报表功能进行分组。但是,我找不到进行此分组的方法。我认为这是可能的?这将允许我将数据集重用于项目中的其他区域,而不必为每个报告创建唯一的数据集。
回答by Hariharan Anbazhagan
Grouping Fields
分组字段
Without Writing SQL query, you can group the Data from the table.
无需编写 SQL 查询,您就可以对表中的数据进行分组。
In Field Explorer, you can find 'Group Name Fields' Right Click and Select 'Group Expert'.
在字段资源管理器中,您可以找到“组名字段”,右键单击并选择“组专家”。
After selecting, you will get a Group Expert Dialog Box, which shows the Fields found in your datatable. Select a Field Name which you want to group. (Here I selected 'Project Name')
选择后,您将获得一个组专家对话框,其中显示在您的数据表中找到的字段。选择要分组的字段名称。(这里我选择了“项目名称”)
You can also create Sub-Groups under that field, the Groups ll be classified as Group A, Group B.
您还可以在该字段下创建子组,这些组将分为 A 组、B 组。
Now you can find Group Fields added in your crystal report automatically ! Grouping is Done !
现在您可以在水晶报告中自动找到添加的组字段!分组完成!
Counting Fields
计数字段
To Count the no of records or fields in a report, you need to add a new 'Running Total Fields'
要计算报告中的记录或字段数,您需要添加一个新的“运行总字段”
Add a Field which you want to count.
添加要计算的字段。
After adding the Field Change the 'Type of Summary' to Count, and press OK.
添加字段后,将“汇总类型”更改为“计数”,然后按“确定”。
Add this Total Field to your report in the area where you need (Group Footer will be advisable).
将此总计字段添加到您需要的区域中的报告中(建议使用组页脚)。
Now find your Field Name is Grouped and Counted without writing any SQL Query.
现在无需编写任何 SQL 查询即可找到您的字段名称已分组和计数。
Hope this ll help you, Leave comments if any !
希望对你有帮助,有的话请留言!
回答by Arvo
In crystal reports, add group by 'name' field and insert there summary of any field from detail, changing summary function to 'count'.
在水晶报表中,按“名称”字段添加分组,并从详细信息中插入任何字段的摘要,将摘要功能更改为“计数”。
回答by vice
Add a group field in crystal; in this example on the name field.
在水晶中添加组字段;在这个例子中的名称字段。
Insert details into the footer of the group.
将详细信息插入组的页脚。
Insert a count function into the footer of the group, counting on the record unique identifiers. Can also use a distinct count if you have multiple occurences of the same name for example.
在组的页脚中插入一个计数函数,对记录的唯一标识符进行计数。例如,如果您多次出现同名,也可以使用不同的计数。
回答by vice
For optimal performance, you should really use GROUP BY on the server side.
Create a view that feeds data to your Crystal Report.
为了获得最佳性能,您应该真正在服务器端使用 GROUP BY。
创建一个向 Crystal Report 提供数据的视图。
You can do grouping in Crystal, however you will encounter performance issues
as the data queried by your report scales up.
您可以在 Crystal 中进行分组,但是
随着报表查询的数据按比例增加,您会遇到性能问题。
It is a design decision, but it saves lots of time and client processing in the long run.
这是一个设计决策,但从长远来看,它可以节省大量时间和客户端处理。
I tend to design the view first (WHERE clause, GROUP BY clause, DB Functions in SELECT)
and then the Crystal Report after I have the prepared the data on the DB side.
我倾向于先设计视图(WHERE 子句、GROUP BY 子句、SELECT 中的 DB 函数)
,然后在 DB 端准备好数据后设计 Crystal Report。
My advice is to start with the end in mind.
我的建议是从结束开始。
回答by contactmatt
If possible, create a database view that groups the dataset and then use that view in the crystal report. This is the easiest and most readable implementation of what you're trying to accomplish in the report.
如果可能,创建一个对数据集进行分组的数据库视图,然后在 Crystal 报表中使用该视图。这是您试图在报告中完成的最简单和最易读的实现。