Jump to content

Divide a value in one table by a value in another table?


RJP1

Recommended Posts

Hi guys,

 

I was wondering the best way to do this...

 

I'd like to output the result of number_x divided by number_y along with the data that is in Table 1 in ASC order of the division result. Is that easily doable?

 

Table 1:

id | userid | name | email | number_x

 

Table 2

id | userid | number_y

 

Thanks for any help guys, would be very useful!

 

RJP

Link to comment
Share on other sites

Hmm, I think I got my question wrong last night...

 

What I need it to do is this:

 

Table 1:

id | userid | name | email | number_x

 

Table 2

id | userid

 

Count the number of records in table2 that have the same userid as table1, then divide number_x by this count and then output all data from table 1 (with the calculation) in ASC order...

 

That's was what I meant because the number_y doesn't exist as such, it is actually the count of rows in the second table.

 

Cheers, RJP

Link to comment
Share on other sites

Hi

 

This should do it:-

 

SELECT id, userid, name, email, number_x, number_x / bCount AS CalcField

FROM (SELECT a.id, a.userid, a.name, a.email, a.number_x, COUNT(b.id) AS bCount

FROM Table1 a

INNER JOIN Table2 b

ON a.userid = b.userid

GROUP BY a.id, a.userid, a.name, a.email, a.number_x) fred

ORDER BY CalcField

 

You might be able to do it in one go without the subselect but not certain.

 

All the best

 

Keith

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.