Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
imanta
Starting Member
6 Posts |
Posted - 2006-02-08 : 12:05:04
|
| I have three tables: one that holds users, on that holds products, and one that holds votes on products (fields are userid, productid, and the vote they made (1-5)). I am trying to come up with a query that orders the product result set by number of votes both descending and ascending for a particular user. If a user has not voted, then there will not be anything in the vote table for that user. I am not even sure where to begin on this as it is way out of my SQL experience.This is a normal query I use to just pull them out ordered by name.SELECT PRODUCTS.PRODUCT_ID, PRODUCTS.P_NAME, USERS.U_USERNAME FROM [PRODUCTS] INNER JOIN [USERS] ON USERS.USER_ID = PRODUCTS.USER_ID WHERE STATUS_ID = 1 ORDER BY PRODUCTS.P_NAME DESCCan anyone help or point me in the right direction?Thanks. |
|
|
RickD
Slow But Sure Yak Herding Master
3608 Posts |
Posted - 2006-02-08 : 12:13:51
|
| Can you give an example of the expected result set as I can't work out if you want this by vote (i.e 1 for every vote regardless of if it was 1,2,3,4 or 5) or whether the 1-5 matters.. |
 |
|
|
sveroa
Starting Member
14 Posts |
Posted - 2006-02-08 : 12:19:50
|
| I would created a Vote table instead of the status_id in the product table, and done something similar as follows:Table: productproduct_idp_nameTable: useruser_idu_nameTable: voteproduct_iduser_idvoteSELECT a.product_id, a.p_name, count(c.vote)FROM [product] a, [users] b, [vote] cWHERE a.user_id = b.user_id AND c.product_id = a.product_id AND c.user_id = a.user_id AND b.u_name = 'XXX'GROUP BY a.product_id, a.p_nameORDER BY c.vote DESCSwap DESC with ASC for the ascending sorting |
 |
|
|
imanta
Starting Member
6 Posts |
Posted - 2006-02-08 : 12:24:14
|
| The status and vote thing are independent. Status is just whether a product should be visible to the user at all.Basically in the end it should look like the following in terms of results (note that 0 denotes has not been voted on so it will not be in the Votes table).User: RickDProducts:Shirt - 5Shoes - 3Tie - 2Belt - 1Watch - 0Pants - 0ORUser: RickDProducts:Watch - 0Pants - 0Belt - 1Tie - 2Shoes - 3Shirt - 5Thanks for the help. |
 |
|
|
RickD
Slow But Sure Yak Herding Master
3608 Posts |
Posted - 2006-02-08 : 12:57:04
|
| You will need to add the vote table into your query, i'm guessing that the votes are by productid, so that will be your link (you'll need to use a left join as you want all Products, then just ORDER BY Vote ASC or ORDER BY Vote DESC.. |
 |
|
|
imanta
Starting Member
6 Posts |
Posted - 2006-02-08 : 13:13:26
|
| RickD, wow thanks for the info. Can you look at the following and see if it makes sense? As I said, very new to this. Here is a pass at it and I am getting results that look good...SELECT PRODUCTS.PRODUCT_ID, PRODUCTS.P_NAME, VOTES.V_SCOREFROM [PRODUCTS]LEFT JOIN [VOTES] ON VOTES.PRODUCT_ID = PRODUCTS.PRODUCT_IDWHERE PRODUCTS.STATUS_ID = 1 AND VOTES.USER_ID = 3ORDER BY VOTES.V_SCORE ASCOne thing, when I do this, I am not getting the products that have not been voted for. Isn't that the purpose of a Left Join? |
 |
|
|
|
|
|
|
|