mellowg
mellowg

Reputation: 1796

Android SQLite: Can I consolidate these four SQLite queries into one?

They're all querying the same table, one SELECTs number of results and the other set of queries SELECTs the minimum.

Can this be conslidated into a single query

      likeNewTotal = db.getSingleValue("SELECT COUNT(*) FROM Offers WHERE subCondition = 'likenew'");
   veryGoodTotal = db.getSingleValue("SELECT COUNT(*) FROM Offers WHERE subCondition  = 'verygood'");
 goodTotal = db.getSingleValue("SELECT COUNT(*) FROM Offers WHERE subCondition  = 'good'");
    acceptableTotal = db.getSingleValue("SELECT COUNT(*) FROM Offers WHERE subCondition  = 'acceptable'");

    likeNewLow = db.getSingleValue("SELECT MIN(price) FROM Offers WHERE subCondition = 'likenew'");
        veryGoodLow = db.getSingleValue("SELECT MIN(price) FROM Offers WHERE subCondition  = 'verygood'");
    goodLow = db.getSingleValue("SELECT MIN(price) FROM Offers WHERE subCondition  = 'good'");
    acceptableLow = db.getSingleValue("SELECT MIN(price) FROM Offers WHERE subCondition  = 'acceptable'");

Upvotes: 0

Views: 203

Answers (1)

mvds
mvds

Reputation: 47054

You are probably looking for the GROUP BY function:

SELECT subCondition,COUNT(*),MIN(price) FROM Offers GROUP BY subCondition;

This will give you the COUNT(*) and MIN(price) for all distinct values found in subCondition.

Upvotes: 1

Related Questions