You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-user@db.apache.org by David Mitchell <da...@factorylogic.com> on 2003/03/07 21:56:15 UTC

HAVING clause

 Is there some particular reason why there's no "addHaving()" method on
Criteria?

 I needed to do the following query on my database. (I am implementing an
INTERSECTION query, which isnt supported by my database, SQL Server)

SELECT     numOperators
FROM         flFactoryOperatorCycleTime
WHERE     (workCenterNdx = 64) AND (productNdx IN (753, 754, 758))
GROUP BY numOperators
HAVING      (COUNT(*) = 3)

 Since I needed to only query a single field (numOperators) and could not
deal with selecting ALL columns (due to the group by clause), I used a
report query . So far so good. Then, I ran into trouble because report
queries can only be created from Criteria, which don't seem to support
"HAVING" clauses.

 My hack (which works) to get around it is to FAKE it! But I do wish I had a
better solution.

Criteria crit = new Criteria();
crit.addEqualTo(IFactoryOperatorCycleTime.WORKCENTERNDX, new
Integer(((IPersistentObject)workCenter).getNdx()));
crit.addIn(IFactoryOperatorCycleTime.PRODUCTNDX, productNdxs);
crit.addSql("(1=1) GROUP BY NUMOPERATORS HAVING (COUNT(*) =
"+productNdxs.size()+")");

 The reason for the "(1=1)" is because when you do an addSql() on a
criteria, it helpfully puts in the 'AND' part for you, but "AND HAVING..."
doesnt work,
and HAVING has to come after the GROUP BY, so if I do the addGroupBy()
method separately, the HAVING gets put BEFORE the GROUP BY, which also
doesnt work.

 The final query ends up looking like this:

SELECT A0.numOperators FROM flFactoryOperatorCycleTime A0 WHERE ((
A0.workCenterNdx =  ? ) AND  
(A0.productNdx IN ( ? , ? , ? ))) AND (1=1) GROUP BY NUMOPERATORS HAVING
(COUNT(*) = 3)

 Any better suggestions?

 thanks- 
David Mitchell


 

 


+---------------------------------------------------------+ 
This message may contain confidential and/or privileged information.  If you
are not the addressee or authorized to receive this for the addressee, you
must not use, copy, disclose or take any action based on this message or any
information herein.  If you have received this message in error, please
advise the sender immediately by reply e-mail and delete this message.
Thank you for your cooperation.
+---------------------------------------------------------+

Re: HAVING clause

Posted by Jakob Braeuchi <jb...@gmx.ch>.
hi david,

there's no having clause in ojb so far.
imo it's not too difficult to implement, but i'm not sure whether it 
belongs to Criteria or to Query.

Criteria#addHaving (Criteria aHavingCriteria)  should be sufficient.

jakob

David Mitchell wrote:

> Is there some particular reason why there's no "addHaving()" method on
>Criteria?
>
> I needed to do the following query on my database. (I am implementing an
>INTERSECTION query, which isnt supported by my database, SQL Server)
>
>SELECT     numOperators
>FROM         flFactoryOperatorCycleTime
>WHERE     (workCenterNdx = 64) AND (productNdx IN (753, 754, 758))
>GROUP BY numOperators
>HAVING      (COUNT(*) = 3)
>
> Since I needed to only query a single field (numOperators) and could not
>deal with selecting ALL columns (due to the group by clause), I used a
>report query . So far so good. Then, I ran into trouble because report
>queries can only be created from Criteria, which don't seem to support
>"HAVING" clauses.
>
> My hack (which works) to get around it is to FAKE it! But I do wish I had a
>better solution.
>
>Criteria crit = new Criteria();
>crit.addEqualTo(IFactoryOperatorCycleTime.WORKCENTERNDX, new
>Integer(((IPersistentObject)workCenter).getNdx()));
>crit.addIn(IFactoryOperatorCycleTime.PRODUCTNDX, productNdxs);
>crit.addSql("(1=1) GROUP BY NUMOPERATORS HAVING (COUNT(*) =
>"+productNdxs.size()+")");
>
> The reason for the "(1=1)" is because when you do an addSql() on a
>criteria, it helpfully puts in the 'AND' part for you, but "AND HAVING..."
>doesnt work,
>and HAVING has to come after the GROUP BY, so if I do the addGroupBy()
>method separately, the HAVING gets put BEFORE the GROUP BY, which also
>doesnt work.
>
> The final query ends up looking like this:
>
>SELECT A0.numOperators FROM flFactoryOperatorCycleTime A0 WHERE ((
>A0.workCenterNdx =  ? ) AND  
>(A0.productNdx IN ( ? , ? , ? ))) AND (1=1) GROUP BY NUMOPERATORS HAVING
>(COUNT(*) = 3)
>
> Any better suggestions?
>
> thanks- 
>David Mitchell
>
>
> 
>
> 
>
>
>+---------------------------------------------------------+ 
>This message may contain confidential and/or privileged information.  If you
>are not the addressee or authorized to receive this for the addressee, you
>must not use, copy, disclose or take any action based on this message or any
>information herein.  If you have received this message in error, please
>advise the sender immediately by reply e-mail and delete this message.
>Thank you for your cooperation.
>+---------------------------------------------------------+
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
>For additional commands, e-mail: ojb-user-help@db.apache.org
>
>
>  
>


Re: HAVING clause

Posted by Jakob Braeuchi <jb...@gmx.ch>.
hi all,

i just commited the first try to support HAVING clause.
this sample code with a having-criteria:

        ReportQueryByCriteria query;
        Criteria crit, having;

        crit = new Criteria();
        crit.addGroupBy(new String[] { "id", "name", "vorname" });
        having = new Criteria();
        having.addGreaterThan("sum(konti.saldo)", new Integer(200));
       
        query = new ReportQueryByCriteria(Person.class, crit);
        query.setColumns(new String[] { "id", "name", "vorname", 
"sum(konti.saldo)" });
        query.setHavingCriteria(having);    <<<<
       
        broker.getReportQueryIteratorByQuery(query);;

produces this sql:

SELECT A0.id,A0.name,A0.vorname,sum(A1.saldo) FROM tabPerson A0 INNER 
JOIN tabKonto A1 ON A0.id=A1.idPerson GROUP BY A0.id,A0.name,A0.vorname 
HAVING sum(A1.saldo) >  '200'

do not expect this first version to work perfectly !  there will be 
problems when the whereCriteria is empty but the havingCriteria not.


hth
jakob

David Mitchell wrote:

> Is there some particular reason why there's no "addHaving()" method on
>Criteria?
>
> I needed to do the following query on my database. (I am implementing an
>INTERSECTION query, which isnt supported by my database, SQL Server)
>
>SELECT     numOperators
>FROM         flFactoryOperatorCycleTime
>WHERE     (workCenterNdx = 64) AND (productNdx IN (753, 754, 758))
>GROUP BY numOperators
>HAVING      (COUNT(*) = 3)
>
> Since I needed to only query a single field (numOperators) and could not
>deal with selecting ALL columns (due to the group by clause), I used a
>report query . So far so good. Then, I ran into trouble because report
>queries can only be created from Criteria, which don't seem to support
>"HAVING" clauses.
>
> My hack (which works) to get around it is to FAKE it! But I do wish I had a
>better solution.
>
>Criteria crit = new Criteria();
>crit.addEqualTo(IFactoryOperatorCycleTime.WORKCENTERNDX, new
>Integer(((IPersistentObject)workCenter).getNdx()));
>crit.addIn(IFactoryOperatorCycleTime.PRODUCTNDX, productNdxs);
>crit.addSql("(1=1) GROUP BY NUMOPERATORS HAVING (COUNT(*) =
>"+productNdxs.size()+")");
>
> The reason for the "(1=1)" is because when you do an addSql() on a
>criteria, it helpfully puts in the 'AND' part for you, but "AND HAVING..."
>doesnt work,
>and HAVING has to come after the GROUP BY, so if I do the addGroupBy()
>method separately, the HAVING gets put BEFORE the GROUP BY, which also
>doesnt work.
>
> The final query ends up looking like this:
>
>SELECT A0.numOperators FROM flFactoryOperatorCycleTime A0 WHERE ((
>A0.workCenterNdx =  ? ) AND  
>(A0.productNdx IN ( ? , ? , ? ))) AND (1=1) GROUP BY NUMOPERATORS HAVING
>(COUNT(*) = 3)
>
> Any better suggestions?
>
> thanks- 
>David Mitchell
>
>
> 
>
> 
>
>
>+---------------------------------------------------------+ 
>This message may contain confidential and/or privileged information.  If you
>are not the addressee or authorized to receive this for the addressee, you
>must not use, copy, disclose or take any action based on this message or any
>information herein.  If you have received this message in error, please
>advise the sender immediately by reply e-mail and delete this message.
>Thank you for your cooperation.
>+---------------------------------------------------------+
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
>For additional commands, e-mail: ojb-user-help@db.apache.org
>
>
>  
>