MYSQL sum the total up and down votes by all users for the items bought by a single user

Go To StackoverFlow.com

4

I'd like to sum the total up and down votes on only the items bought by a single user. I have a big table so I don't want to sum all votes made by everyone for EVERY item, just the items that a particular user bought.

Here's my query so far:

select SUM(purchaseyesno) AS tots, SUM(rating=1) AS yes, SUM(rating=0) AS no, item_id
from items_purchased
where purchaser_account_id=12373 
group by item_id

as you can expect, these sums are only the summing user 12373's info, so its just one value. I'm not sure how to get ALL the purchases of item_ids that are bought by user 12373.

I'm sure there is some kind of subquery,nesting thing I need to include but I'm clueless.

here's how I'd like my data to look, item_id=3,4,5 are all bought by user=12373. Whereas item_id=1,2,6 were bought by other users.

item_id    tots    yes    no
   3        7       4      2
   4        5       1      3
   5        1       0      1

thoughts?

2012-04-05 02:47
by tim peterson
Please provide more info on the columns in your items purchased table that relevant. Do you have a PurchaserAccount table? If so is the only relevant field the purcharseraccountid? Is the relationship on the purchaseraccount_id column - Dan P 2012-04-05 02:53
hi, none of these columns are keys. The purchase_id is the primary key. The name of the table is items_purchased. I only need to use this one table as far as I can tell because all the columns in the SELECT part of the SQL statement cover all the columns that I need - tim peterson 2012-04-05 02:58


2

select item_id, SUM(purchaseyesno) tots, SUM(rating = 1) yes, SUM(rating = 0) no
from items_purchased
where item_id in (
    select item_id from items_purchased
    where purchaser_account_id = 12373
)
group by item_id
2012-04-05 02:57
by Mosty Mostacho
hi Mosty, sorry if I wasn't clear but I do need the itemid because each item is displayed to the user in a list. They aren't aggregated.I'd just like to tally the yes/no votes made by all users including user 12373 for itemid=3, item_id=4, etc - tim peterson 2012-04-05 03:01
you've got a point, i'm thinking.. - tim peterson 2012-04-05 03:05
@MostyMostacho grouping by itemid does NOT mean that all items are in one row. Only the items with the SAME itemid will be grouped together - binarious 2012-04-05 03:10
hi, Mosty, but if I remove the where clause then I'll get all the voting for every item. It won't be restricted to user=12373 anymore. On a big table this seems like it will be costly - tim peterson 2012-04-05 03:10
@timpeterson well I don't get what's wrong with your code. Explain what should be different - binarious 2012-04-05 03:25
hi Mosty, your final edit query is the same as my query above, or am I confused - tim peterson 2012-04-05 03:25
@timpeterson yes you're right, and this query is working. I've tested it locally - binarious 2012-04-05 03:26
i am never getting tots>1 since it is only grouping by item_ids bought by user=12373. I think the query has to be slightly more complicated than i currently is to make it work - tim peterson 2012-04-05 03:35
i know me too, sorry. Yes, I'm only looking for items bought by that user. However, other people can buy those items too. I'd like to give user=12373 a look at the total number of purchases and how they were rated by all users on an item by item basis - tim peterson 2012-04-05 03:44
btw, i tried your query. It seems close but still isn't giving me tots>1 for any item. ugh - tim peterson 2012-04-05 03:45
@timpeterson Give it another try. I think the most confusing part was only the items bought by a single user so you would never get more than 1 tot (unless a user bought the same product twice). Maybe instead of single it would have been better to say given - Mosty Mostacho 2012-04-05 03:52
oh wow, you are my hero, that works! the where item_id in is really the key. thanks so much for patiently tackling this with me. Triple-Up vote to that - tim peterson 2012-04-05 03:53
@timpeterson Welcome :) I have fun solving this queries anyway, so thank you too - Mosty Mostacho 2012-04-05 03:55
Ads