Crystal Reports: Need advice on how to avoid use of subreports
I'm trying to write a report to hit a very large database, which makes the use of subreports... unpleasant to say the least. How can I avoid them in this situation:
I'm starting from a table of patient visits. Each visit can have multiple vital readings (Say in a 'Vitals' table that is many-to-one with the visit table) of which I want the min and max of each type. So to accomplish this, I group by the patient visits and then use formulas to calculate the min/maxes and display them in the group footer. So far so good. However, there is another table, Orders, that contains all the orders for that visit as well, of which I also want to select some min/max values to display for the encounter.
To skirt using a subreport, I added some more formulas to the suppressed details that also find the min/max of these order values and display in the group footer. The problem is that Crystal only returns patient visits that have orders (despite being outer-joined), and what I need is a list of ALL visits that may or may not have any orders whatsoever. To complicate matters further, there is yet another Orders table I need to do this with as well (one for medication and one for procedures). Any ideas?
(visitID, patName, visitDate)
------------------------------------------------------------------
1, Patient A, July-24-2009
2, Patient B, July-25-2009
(visitID, line, medName, dosage)
------------------------------------------------------------------
1, 1, Aspirin, 200开发者_开发百科mg
1, 2, Aspirin, 500mg
2, 1, Codeine, 100mg
2, 2, Codeine, 200mg
2, 3, ValerianCap, 700mg
(visitID, line, procName, procType, cost)
------------------------------------------------------------------
1, 1, Xray-wrist, xray, $500
1, 2, Xray-elbow, xray, $300
+-----------------------------------------------
| Patient A
| Med Orders:
| Max Aspirin Dosage: 500mg
|
| Proc. Orders:
| Max cost Xray: $500
|
+-----------------------------------------------
| Patient B
| Med Orders:
| ...
+-----------------------------------------------
You need to flatten your data set I would suggest starting with the query
Something like this. Note basically you are using union to query each of the Joins to the Detail tables separately. When Done you can group by record type and make multiple sections for the Record Type group (4 sections) Each section will have a suppress condition (Example Vitals section will suppress when RecordType <> Vitals.) Now basically you have created groups instead of subreports.
SELECT
'Vitals' As RecordType,
Vitals.*,
--Fields For Orders,
--Fields For Medications,
--Fields For Procedures
FROM Visits
LEFT JOIN Vitals ON Vitals.VisitId = Visits.VisitId
UNION ALL
SELECT
'Orders' As RecordType,
--Fields for Vitals,
Orders.*,
--Fields For Medications,
--Fields For Procedures
FROM Visits
LEFT JOIN Orders ON Orders.VisitId = Visits.VisitId
UNION ALL
SELECT
'Medications' As RecordType,
--Fields for Vitals,
--Fields For Orders,
Medications.*,
--Fields For Procedures
FROM Visits
LEFT JOIN Medications ON Medications.VisitId = Visits.VisitId
UNION ALL
SELECT
'Procedures' As RecordType,
--Fields For Vitals,
--Fields For Orders,
--Fields For Medications,
Procedures.*
FROM Visits
LEFT JOIN Procedures ON Procedures.VisitId = Visits.VisitId
精彩评论