Skip to content
Advertisement

Find 5 greatest totals after summing (quantity x value) for each qualifying user

I currently have a table which displays the inventory values of users. It works, but it displays all users and their inventory values in a random order. I’m wanting to limit the table to the top 5 descending inventory values only.

This data cuts across three tables: DB Fiddle

  1. Users
  2. Inventories (contains a row for each item a user has in their inventory)
  3. Items (contains the value of the items)

The output is something like:

Inventory Value Username
120000 Account1
112000 Accounts2
70000 Account3
169000 Accounts5

I’m guessing this could be done with a JOIN query, but I’m not very experienced and haven’t done one that cuts across three tables before. I don’t know how I can use these nested queries to get the top 5 inventory totals and arrange them in descending order.

Advertisement

Answer

  1. Join the three table is accordance with the rules in your original nested queries.
  2. Group the data on username values.
  3. This will create AGGREGATE data which will need to be organized within the SUM() function so that the item quantities associate with the correct item value.
  4. Now it is trivial to apply your ORDER BY and LIMIT rules.

SQL: (Demo)

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement