简体   繁体   English

如何在单个Crystal报表中获得两个没有联合键的两个独立表的总和?

[英]How do I get two sums of two separate tables with no joint keys in a single crystal report?

I have data in two tables (see below for a sample) - how do I create a Crystal report (more of a "score card" really) displaying only sum(table1.column1) and sum(table2.column1) with no other details? 我有两个表中的数据(请参见下面的示例)-如何创建仅显示sum(table1.column1)和sum(table2.column1)的Crystal报表(实际上是“记分卡”的更多内容),而没有其他详细信息? When I try, one of the sums gets way too big, indicating it has been included in some inner loop in the calculations. 当我尝试时,总和之一变得太大,表明它已包含在计算的某些内部循环中。

Table1: 表格1:
Column1: Integer 列1:整数
Column2: Varchar(100) 第2栏:Varchar(100)
... ...

Table2: 表2:
Column1: Integer 列1:整数
Column2: Varchar(50) 专栏2:Varchar(50)
... ...

Note - there are no joint keys, the only relation between the tables is that they relate to the same business area. 注意- 没有联合键,表之间的唯一关系是它们与同一业务领域相关。

select t1.cnt, t2.cnt
from ( select count(*) cnt from table1 where... ) t1
, ( select count(*) cnt from table2 where... ) t2

If you want to avoid the sub-query approach, the only real route that I can think of is to use sub-reports. 如果您想避免使用子查询方法,那么我能想到的唯一真正的方法就是使用子报告。

2 ways I can think of: 我可以想到2种方式:

  1. Put each query in its own sub-report, and link them into your main report. 将每个查询放在自己的子报表中,并将它们链接到您的主报表中。
  2. Put one query in your main report, and the other in a linked sub-report. 将一个查询放在主报表中,将另一个查询放在链接的子报表中。

I answer this with the caveat that it will almost certainly be slower than simply using one query (as in Randy's answer), because Crystal Reports is not as efficient as the DB engine. 我要警告一下,这几乎肯定会比仅使用一个查询(如Randy的回答)要慢,因为Crystal Reports的效率不如DB引擎。 It's also probably going to be harder than maintain. 这也可能比维护困难。 Basically, while you certainly can do it this way, I'm not sure I would . 基本上,虽然您当然可以这样做,但我不确定是否可以

You could use two SQL Expression fields. 您可以使用两个SQL Expression字段。 Each field needs to return a scalar value. 每个字段都需要返回一个标量值。 You can correlate (link) each query with the main-report's query as well. 您还可以将每个查询与主报表的查询关联(链接)。

  1. Add a grouping levels for Table1.uid. 为Table1.uid添加分组级别。 Create a running total Table1Sum, sum on Table1.Column1, on change of group Table1.uid, reset never. 创建运行总计Table1Sum,在Table1.Column1上求和,更改组Table1.uid时,永不重置。 Create a running total Table2Sum, sum on Table2.Column1, on every record, reset on change of group Table1.uid. 在每个记录上,在Table2.Column1上创建一个运行中的Table2Sum总和,并在更改Table1.uid组时重置。 Print both running totals in the report footer. 在报表页脚中打印两个运行总计。
  2. Place your queries in separate subreports. 将查询放在单独的子报表中。 (This is what I'd probably do.) (这就是我可能要做的。)

The first one obviously requires (1) a unique key in Table1 and (2) printing the values in the footer. 第一个显然需要(1)表1中的唯一键,以及(2)在页脚中打印值。 If those constraints won't work, two subreports should still work. 如果这些约束不起作用,则两个子报表仍应起作用。

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

相关问题 如何在一个输出中从两个表中获得两个总和? - How to get two sums from two tables in one output? 如何从两个不同的表中获取数据,如何将其链接到第三个表中的数据,以及如何在单个报表中获取所有数据? - How can I get data from two different tables, link it to data from a third table, and get everything on a single report? 如何在单个数据库中合并两个表? - How do I merge two tables in single database? 如何将这两个数据库表合并为一个结果集? - How do i merge these two database tables into a single result set? 如何通过单个查询获得两个单独的结果 - How to get two separate result with single query 如何将两个单独的表中的两列拆分为一个视图中联接的多行? - How do I split two columns in two separate tables into joined multiple rows in a view? 在SQL中如何获取两组日期范围的求和 - In SQL How can I get Sums for two sets of date ranges 如何使用flask-sqlalchemy使两个不同表中的两个主键唯一? - How do I make two primary keys, in two different tables, unique using flask-sqlalchemy? 如何获取最后插入的其他两个表的唯一组合外键作为单键 - how to get last inserted unique combined foreign keys of two other tables as single key 使用SQL Server数据的Crystal报表,结合了两个表 - Crystal report using SQL Server data, combining two tables
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM