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

  • Home
  • SEARCH
  • 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 6018359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:15:50+00:00 2026-05-23T03:15:50+00:00

I have a schema that looks like this: Tables table_id seat_count Orders order_id table_id

  • 0

I have a schema that looks like this:

 Tables
   table_id
   seat_count

 Orders
   order_id
   table_id
   meal_id

 Meals
   meal_id
   price

I am trying to get Tables that have the biggest income per seat_count, i.e.

 |idTable| |income| |seat_count|
     2        50$        5
     3        60$        4
     4        40$        3
     10       80$        2

The closest I got was with this query:

 SELECT tables.table_id, 
       SUM(income), 
       tables.seat_count 
FROM   (SELECT tables.table_id, 
               tables.seat_count, 
               COUNT(orders.meal_id) * meals.price AS income 
        FROM   meals 
               INNER JOIN (tables 
                           INNER JOIN orders 
                             ON tables.table_id = orders.table_id) 
                 ON meals.meal_id = orders.meal_id 
        GROUP  BY tables.table_id, 
                  tables.seat_count, 
                  meals.price 
        ORDER  BY COUNT(orders.meal_id) * meals.price DESC) 
GROUP  BY tables.table_id, 
          tables.seat_count 
ORDER  BY SUM(income) DESC  

But I am stuck, it returns records such as:

 table_id, income, seat_count
   1         40$     5
   2         30$     5
   4         20$     4

(I.e with duplicate seat_counts) and I have no idea how to get rid of it.

  • 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-23T03:15:51+00:00Added an answer on May 23, 2026 at 3:15 am

    I think this will give you what you want, though if two tables have the same seat count and income level, it will show both. I tried it on a mockup of your table structure above in Access 2003 (which is why it has the strange join syntax with the brackets).

    select sub1.table_id, sub1.seat_count, sub1.income
    from
    (
        SELECT Tables.table_id, Tables.seat_count, Sum(Meals.price) AS Income
        FROM (Tables INNER JOIN Orders ON Tables.table_id = Orders.table_id) INNER JOIN Meals ON Orders.meal_id = Meals.meal_id
        GROUP BY Tables.table_id, Tables.seat_count
    ) sub1
    inner join 
    (
        select seat_count, max(Income) as Maxincome
        from
        (
            SELECT Tables.table_id, Tables.seat_count, Sum(Meals.price) AS Income
            FROM (Tables INNER JOIN Orders ON Tables.table_id = Orders.table_id) INNER JOIN Meals ON Orders.meal_id = Meals.meal_id
            GROUP BY Tables.table_id, Tables.seat_count
        ) sub
        group by seat_count
    ) sub2 on (sub1.seat_count = sub2.seat_count and sub1.income = sub2.maxincome);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a schema design that looks like this: Table: User Row Column Family
I have a schema that essentially looks like this: CREATE TABLE `data` ( `id`
I have schema that looks like this: create_table users, :force => true do |t|
I have a schema that looks like this: +----------+ | tour | +----------+ |
I have this existing schema where a schedule table looks like this (very simplified).
I have a table that looks like this: Id (PK, int, not null) ReviewedBy
I have a legacy SQL schema which looks something like this: CREATE TABLE `User`
I have an existing table structure that looks something like this: AnimalTable ------------- |Id
I have an XML structure that looks like: <Succeeded p1:type=Edm.Boolean xmlns:p1=http://schemas.microsoft.com/ado/2007/08/dataservices/metadata xmlns=http://schemas.microsoft.com/ado/2007/08/dataservices>false</Succeeded> Since I
I have two tables in my database schema that represent an entity having a

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.