Solved

Count from two tables

Posted on 2008-06-22
9
259 Views
Last Modified: 2012-06-27
Following situation:
Table 1:
Emp_ID
Emp_Name


Table 2:
Trans_ID
Emp_ID



Goal:
I need a report that displays ALL records from Table 1 and displays then number of related records in Table 2.
It should also show records from Table 1 if there are NO records in Table 2. (i.e. result would be Employee Nr. 712 = 0 transactions)

How do I do this in Crystal Reports?
DB is DB2.
Do I need to make queries for this, or can I count in the report itself?
How would the queries look like?

THANKS A LOT
0
Comment
Question by:olio
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 28

Accepted Solution

by:
Bill Bach earned 200 total points
ID: 21840900
Two ways, depending on the capabilities of the SQL engine:

SELECT Emp_ID, Emp_Name, (SELECT COUNT(*) FROM Table2 WHERE Table2.Emp_ID = Table1.Emp_ID) FROM Table1

SELECT Emp_ID, Emp_Name, COUNT(*) FROM Table1
LEFT OUTER JOIN Table2 WHERE Table2.Emp_ID = Table1.Emp_ID
GROUP BY Emp_ID, Emp_Name
0
 
LVL 37

Assisted Solution

by:momi_sabag
momi_sabag earned 100 total points
ID: 21841009
i would use BillBach second suggestion
the first one is very bad in terms of perfromance if you have many rows
it's query needs a slight modification:

SELECT Emp_ID, Emp_Name,  sum(case t2.Emp_ID when null then 0 else 1 end)
FROM Table1 t1
LEFT OUTER JOIN Table2  t2 WHERE t2.Emp_ID = t1.Emp_ID
GROUP BY Emp_ID, Emp_Name

this is the correct
his query will return 1 if no rows exist for a given id, and not 0 as you requested
0
 
LVL 37

Assisted Solution

by:momi_sabag
momi_sabag earned 100 total points
ID: 21841012
if the above returns sytax error then try

SELECT Emp_ID, Emp_Name,  sum(case when t2.Emp_ID is null then 0 else 1 end)
FROM Table1 t1
LEFT OUTER JOIN Table2  t2 WHERE t2.Emp_ID = t1.Emp_ID
GROUP BY Emp_ID, Emp_Name

0
 
LVL 1

Author Comment

by:olio
ID: 21841078
can i use this right in Crystal Report?
I have no direct access to DB2.
I can just create reports.
This is the difficulty in the organziational setup.
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 37

Expert Comment

by:momi_sabag
ID: 21841202
you can create a view in db2 that runs the abobe query and then you can just query that view as if it was a regular table
0
 
LVL 100

Expert Comment

by:mlmcc
ID: 21847723
Yes, you can add the SQL as a Crystal COMMAND when you set the database source.

If you prefer there are ways to get the counts in Crystal.

mlmcc
0
 
LVL 1

Author Comment

by:olio
ID: 21850450
I prefer to count in Crystal an not with an SQL command.
How would that be done mlmcc?
0
 
LVL 100

Assisted Solution

by:mlmcc
mlmcc earned 100 total points
ID: 21851342
The SQL command will be faster than in Crystal.

There are a couple of ways of counting in Crystal.
Easy way if you are counting all records
Right click a field in the report
Click INSERT --> SUMMARY
Choose type as COUNT
CHoose the appropriate group or report footer.

If you are trying to count distinct values use DISTINCT COUNT

You can also use a variable or running total to do the counting.
If summaries don't work I'll explain those.

mlmcc
0
 
LVL 34

Assisted Solution

by:James0628
James0628 earned 100 total points
ID: 21851610
As mlmcc said, you can use a Count summary.

 Of course you also need to get your tables in the report, and in case you're not sure how to do that ...

 Add the tables to the report, link Table1 to Table2 on Emp_ID (or whatever fields are appropriate) and make the link a LEFT OUTER JOIN, so that records from Table1 will be included even if there are no corresponding records in Table2.

 James
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Need to learn crystal reports 7 63
Software (free or not) to catalog a collection of software and freeware 3 76
Fixing Non-Standard characters in text 8 92
Slow SQL query 12 55
Hello everyone, Hope you find this as helpful as we did. We have on the company I work for an application built in Delphi V with Crystal Reports 8. We all know that Crystal & Delphi can be temperamental sometimes and the worst thing is, nearly…
I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now