vendredi 18 septembre 2015

SQLite Group BY

i want to Calculate the percentages of some Values between Dates in SQLite Query.

This is how the Rows look like

customerId, sex, timeStamp, item

My Code

 String query = "SELECT *, ROUND((count(sex)*100.0)/(SELECT count(sex) FROM salesTable  where timeStamp BETWEEN '"+startdate+"' AND '"+enddate+"'),2) AS '"+PERCENTAGE+"'   FROM salesTable  where timeStamp BETWEEN '"+startdate+"' AND '"+enddate+"'   GROUP BY sex ORDER BY percentage DESC" ;

My Problem is that for example a Male Customer buys 3* Phones it will be stored like this

  • Male Customer, Male, timeStamp, Phone
  • Male Customer, Male, timeStamp, Phone
  • Male Customer, Male, timeStamp, Phone

The timeStamp´s in this example are same because the phones where sold at the same time. I have to store everything separate even if they belong together because of some other reasons.

And now lets say a Female Customer Buys 2* Phones. Then we have

  • Male Customer, Male, timeStamp, Phone
  • Male Customer, Male, timeStamp, Phone
  • Male Customer, Male, timeStamp, Phone
  • Female Customer, Female, timeStamp, Phone
  • Female Customer, Female, timeStamp, Phone

What i want to get is 50% Male and 50% Female. Im sure it is easy to do it but im stuck with it. I also tried this with GROUP BY sex, timeStamp

 String query = "SELECT *, ROUND((count(sex)*100.0)/(SELECT count(sex) FROM salesTable  where timeStamp BETWEEN '"+startdate+"' AND '"+enddate+"'),2) AS '"+PERCENTAGE+"'   FROM salesTable  where timeStamp BETWEEN '"+startdate+"' AND '"+enddate+"'   GROUP BY sex, timeStamp ORDER BY percentage DESC" ;

at the end but didn't work

Aucun commentaire:

Enregistrer un commentaire