473,396 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to return highest SUM values using JOINS and GROUP BY clause

I've got two important tables.

One has a list of questions within categories...
One has a rating for questions the user chose to give points to...

Columns in table1 (questions) are:
id
question
category_id

Columns in Table2 (rating) are:
id
q_id
rating
user_ip
time
c_id

The query i am using is :
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM questions as q,(SELECT SUM(rating) as totq FROM rating WHERE c_id='category_id') r,(SELECT SUM(rating) AS sumr FROM rating WHERE 'q_id=q.id') ra WHERE category_id='".$rowcat[0]."' ORDER BY (ra.sumr/r.totq),id
  2.  
What I am trying to get is a list of questions sorted by the highest ranked (most points out of overall CATEGORY points) to lowest ranked questions. also separated by category headings.

So say I have three categories ... they seperate the sections like this

Cat1
que1
que2
que3

Cat2
que1
que2
que3

On the results page, if say the people voted and question3 was ranked highest in category 1 and question 2 in category 2. the output i am looking for is

Cat1
que3
que1
que2

Cat2
que2
que3
que1

Please let me know if my select statement is wayyy off, this is killing me. I have never done such a complicated select before.

Thanks!
Chad
Jul 13 '07 #1
2 1782
Atli
5,058 Expert 4TB
I'm not really sure how all your fields work, but I imagine you could do something like this:
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.   q.question,
  3.   SUM(r.rating) / COUNT(r.rating) AS 'Rating'
  4. FROM questions AS q
  5. INNER JOIN  rating AS r
  6.   ON q.id = r.q_id
  7. WHERE q.category_id = 1
  8. GROUP BY q.id
  9. ORDER BY SUM(r.rating) / COUNT(r.rating) DESC
  10. LIMIT 3;
  11.  
This will give you the three highest ranking questions from a given category_id, returning the questions and the avarage rating for each question in order from highest ranking to lowest ranking.
Jul 15 '07 #2
Atli
5,058 Expert 4TB
I have edited the thread's title to better describe it's contents. (I hope)
Please read the Posting Guidlines before posting

Moderator
Jul 15 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: dan | last post by:
i have 2 tables that i want to count TABLE 1 categories --id --name forum_comments --id --category_id
2
by: raulgz | last post by:
Hi I work ith sql server 2000 and i need know the diferent of joins in format not ansi ( with * ) and joins in format ansi ( with 'outher join on' ). Two format work equal ???
1
by: tekanet | last post by:
Hello folks, first of all I really don't know how you gurus call this way of writing joins: SELECT A.FIELD, B.FIELD FROM TABLE_A A, TABLE_B B
9
by: Ed_No_Spam_Please_Weber | last post by:
Hello All & Thanks in advance for your help! Background: 1) tblT_Documents is the primary parent transaction table that has 10 fields and about 250,000 rows 2) There are 9 child tables with...
4
by: Sparky Arbuckle | last post by:
I am building an app that is looking for the latest model of a particular product. I am storing these results in a datalist. Below is what the location and newest model fields look like: ...
2
by: | last post by:
Hi All, How can I return a row when the condition is not met for example simplified SELECT Name,SUM(Cost) AS T FROM DB WHERE Name="+Name +" GROUP BY Name this works fine but I ned it to return...
3
by: cmartin1986 | last post by:
I have written a sql query and I need it to return 0 when it doesn't find any matches to my criteria. I have tried adding iif statements, tried sum, and just Count, all of these methods work fine to...
10
by: marting | last post by:
Before I throw my new expensive laptop out of the window! I'm stuck on getting my joins right with the correct amount in a column count. After speaking with someone a few weeks back, they...
1
by: enrico via DotNetMonster.com | last post by:
this is my select query behind my form with a condition: SELECT tbltrcclient.ClientID, tbltrcservice.ServiceID, tbltrcservice.Date, tbltrcclient.Lastname, tbltrcclient.Firstname,...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.