简体   繁体   中英

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? When I try, one of the sums gets way too big, indicating it has been included in some inner loop in the calculations.

Table1:
Column1: Integer
Column2: Varchar(100)
...

Table2:
Column1: Integer
Column2: 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:

  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. 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. 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. Create a running total Table1Sum, sum on Table1.Column1, on change of group Table1.uid, reset never. Create a running total Table2Sum, sum on Table2.Column1, on every record, reset on change of group 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. If those constraints won't work, two subreports should still work.

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