On Mon, Sep 30, 2002 at 03:50:56PM +0200, Bozic, Stefan (FF) wrote:
> Hey there,
> im looking for a funktion of the criteria Object which will return the max
> value of a column?
> 
> something like  SELECT user,MAX(salary) FROM users

As far as I know there is no function on the Criteria object that will
do this directly for you, but you can modify a Criteria object and add
in the functionality that you want, you will have to work with Village
records for this.

> Any Idea
> 
> Stefan

Take a look at the code below:-

private static String getTotalCostFor(Date start, Date end, String columnName, String 
columnValue) {

        try {
            Criteria criteria = isBetween(start, end);
            criteria.addSelectColumn("SUM(" + CallPeer.COST + ") AS TOTALCOST");

            if(columnName != null && columnValue != null) {
                criteria.add(columnName, columnValue);
                criteria.addGroupByColumn(columnName);
            }

            List l = CallPeer.doSelectVillageRecords(criteria);

            if (l.size() == 0)
                return "0";

            Record r = (Record) l.get(0);
            Value v = r.getValue(1);
            NumberFormat f = NumberFormat.getInstance();

            if (f instanceof DecimalFormat) {
                ((DecimalFormat) f).applyPattern("#,###");
            }

            return f.format(v.asLong());
        } catch (Exception e) {
            Log.info(e.toString());
            return null;
        }
    }

    private static Criteria isBetween(Date min, Date max) {
        max = new Date(max.getTime() + 1000 * 60 * 60 * 24); // add one day so that 
range search works intuitively

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Criteria criteria = new Criteria();

        criteria.add(CallPeer.START, (Object) sdf.format(min), Criteria.GREATER_EQUAL);
        Criteria.Criterion criterion = criteria.getCriterion(CallPeer.START);
        criterion.and(
                criteria.getNewCriterion(
                        criterion.getTable(),
                        criterion.getColumn(),
                        sdf.format(max),
                        Criteria.LESS_EQUAL)
        );


        return criteria;
    }

Attachment: msg08984/pgp00000.pgp
Description: PGP signature

Reply via email to