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?
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
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
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
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