Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6900271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:31:31+00:00 2026-05-27T07:31:31+00:00

I have a table of data that looks like +———+———–+————+————+ | u_id | a_id

  • 0

I have a table of data that looks like

+---------+-----------+------------+------------+
| u_id    | a_id      | count      | weighted   |
+---------+-----------+------------+------------+
|       1 |         1 |         17 |  0.0521472 |
|       1 |         2 |         80 |   0.245399 |
|       1 |         3 |          2 | 0.00613497 |
|       1 |         4 |          1 | 0.00306748 |
|       1 |         5 |          1 | 0.00306748 |
|       1 |         6 |         20 |  0.0613497 |
|       1 |         7 |          3 | 0.00920245 |
|       1 |         8 |        100 |   0.306748 |
|       1 |         9 |        100 |   0.306748 |
|       1 |        10 |          2 | 0.00613497 |
|       2 |         1 |          1 | 0.00327869 |
|       2 |         2 |          1 | 0.00327869 |
|       2 |         3 |        100 |   0.327869 |
|       2 |         4 |        200 |   0.655738 |
|       2 |         5 |          1 | 0.00327869 |
|       2 |         6 |          1 | 0.00327869 |
|       2 |         7 |          0 |          0 |
|       2 |         8 |          0 |          0 |
|       2 |         9 |          0 |          0 |
|       2 |        10 |          1 | 0.00327869 |
|       3 |         1 |         15 |   0.172414 |
|       3 |         2 |         40 |    0.45977 |
|       3 |         3 |          0 |          0 |
|       3 |         4 |          0 |          0 |
|       3 |         5 |          0 |          0 |
|       3 |         6 |         10 |   0.114943 |
|       3 |         7 |          1 |  0.0114943 |
|       3 |         8 |         20 |   0.229885 |
|       3 |         9 |          0 |          0 |
|       3 |        10 |          1 |  0.0114943 |
+---------+-----------+------------+------------+

which can be recreated with

CREATE TABLE IF NOT EXISTS tablename ( u_id INT NOT NULL,   a_id MEDIUMINT NOT NULL,s_count MEDIUMINT NOT NULL,  weighted FLOAT NOT NULL)ENGINE=INNODB;
INSERT INTO tablename (u_id,a_id,s_count,weighted ) VALUES (1,1,17,0.0521472392638),(1,2,80,0.245398773006),(1,3,2,0.00613496932515),(1,4,1,0.00306748466258),(1,5,1,0.00306748466258),(1,6,20,0.0613496932515),(1,7,3,0.00920245398773),(1,8,100,0.306748466258),(1,9,100,0.306748466258),(1,10,2,0.00613496932515),(2,1,1,0.00327868852459),(2,2,1,0.00327868852459),(2,3,100,0.327868852459),(2,4,200,0.655737704918),(2,5,1,0.00327868852459),(2,6,1,0.00327868852459),(2,7,0,0.0),(2,8,0,0.0),(2,9,0,0.0),(2,10,1,0.00327868852459),(3,1,15,0.172413793103),(3,2,40,0.459770114943),(3,3,0,0.0),(3,4,0,0.0),(3,5,0,0.0),(3,6,10,0.114942528736),(3,7,1,0.0114942528736),(3,8,20,0.229885057471),(3,9,0,0.0),(3,10,1,0.0114942528736);

the simple version of what I want to do is

SELECT u_id, SUM(weighted) as total FROM tablename WHERE a_id IN (1,2,3,4,5,6,7,8,9) GROUP BY u_id ORDER BY total DESC;

which gives the results

+---------+-------------------+
| u_id    | total             |
+---------+-------------------+
|       2 | 0.996721301227808 |
|       1 | 0.993865059688687 |
|       3 | 0.988505747169256 |
+---------+-------------------+

the more complex version I want to do is to weight the results based on the count from a u_id, so taking the results from

query 1
SELECT count FROM tablename WHERE u_id = 1 

would return

+-----------+------------+
| a_id      | count      |
+-----------+------------+
|         1 |         17 |
|         2 |         80 |
|         3 |          2 |
|         4 |          1 |
|         5 |          1 |
|         6 |         20 |
|         7 |          3 |
|         8 |        100 |
|         9 |        100 |
|        10 |          2 |
+-----------+------------+

which would then be used to calculate the sum, should give

+---------+-------------------+
| u_id    | total             |
+---------+-------------------+
|       1 | 83.15337423       |
|       3 | 65.05747126       |
|       2 | 1.704918033       |
+---------+-------------------+

eg calculating with u_id =3 would be done by

sum(count value from query 1 * weighting value for u_id = 3 for each a_id)
17 *    0.172413793 =2.931034483
80 *    0.459770115 =36.7816092
2   *   0           =0
1   *   0           =0
1   *   0           =0
20  * 0.114942529   =2.298850575
3   * 0.011494253   =0.034482759
100 * 0.229885057   =22.98850575
100 *   0           =0
2   * 0.011494253   =0.022988506
sums up to              65.05747126 

How can I do this with a single query?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T07:31:31+00:00Added an answer on May 27, 2026 at 7:31 am

    You can do this using a subquery. The query that gets the counts for a particular id is:

    SELECT a_id, s_count FROM tablename WHERE u_id = <id>
    

    You will want to left join the result of this subquery into the main table, then sub over the appropriate multiplication, like so:

    SELECT u_id, SUM(counts.s_count * tablename.weighted) AS total FROM tablename 
    LEFT JOIN (SELECT a_id, s_count FROM tablename WHERE u_id = 1) counts
      ON tablename.a_id = counts.a_id
    GROUP BY u_id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table of data that looks like the following: ArtistName TrackName TrackID
I have a table with data that looks like this: create table demo_patient_info (
Say I have a table of data that looks like: ItemNo | ItemCount |
I have a table with weekly data that looks like: userID Site date ------
I have an Oracle table with data that looks like this: ID BATCH STATUS
In SQL Server 2005 I have a table with data that looks something like
I have a table that looks like this A B ID1 data 123 ID2
I have a Table that looks like this MAC-ADDRESS | ACCESSPOINT | TIMESTAMP Data
I have some nicely-structured data that looks like this: CREATE TABLE SourceBodyPartColors ( person_ID
I have some C# code that looks something like this System.Data.DataRow row; var table

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.