users@glassfish.java.net

Migrating from SQL to JPA QL

From: Markus Karg <karg_at_quipsy.de>
Date: Thu, 21 Aug 2008 12:34:05 +0200

I have the following SQL:

SELECT Cause.causingCostCentreId D, COUNT(DISTINCT Complaint.number) C
FROM Cause LEFT JOIN DiscoveredMistake LEFT JOIN Clarification LEFT JOIN Complaint
GROUP BY D
ORDER BY C DESC


Since hours I try to find out how to translate that to JPA QL, but I do not have any success.


I tried the following, but it doesn not return the same result:

SELECT o.causingCostCentre, COUNT(DISTINCT o.discoveredMistake.clarification.complaint)
FROM Cause o
GROUP BY o.causingCostCentre


The problem is that:

(a) "ORDER BY C" is not possible, since C is an aggregate function. In SQL it works.

(b) "DISTINCT" is not working correctly, returning the same row multiple times. In SQL it works.

(c) When "o.causingCostCentre" is NULL, the row is completely missing. In SQL I do get it.


I am totally confused! :-(

Please help me! :-)


Thanks!
Markus


QUIPSY QUALITY GmbH & Co. KG
Ein Unternehmen der MES-Gruppe
Stuttgarter Strasse 23
D-75179 Pforzheim
Tel: 07231-9189-52
Fax: 07231-9189-59
www.quipsy.de
karg_at_quipsy.de
Registergericht Mannheim HRA 701214
Geschäftsführer: Nils Schroeder

Diese E-Mail enthält persönliche, vertrauliche und vor Weitergabe geschützte Informationen und ist ausschließlich für den vorgesehenen o.g. Empfänger (Adressaten) bestimmt. Falls Sie diese E-Mail versehentlich erhalten haben und nicht der vorgesehene Empfänger sind, bitten wir Sie, die E-Mail und deren Anhänge nicht aufzubewahren, nicht zu vervielfältigen, nicht zu nutzen und nicht weiterzugeben. Bitte informieren Sie uns als Absender über diesen Zustellungsfehler und löschen Sie die E-Mail.