简体   繁体   中英

Comparing 2nd largest item per group to the largest item in the group using SQL

I have a relational database for a Burger Building application that a restaurant uses. Two of the tables contained in the DB are Category and Item . These are used to display the categories and then the customer can select a category (EG Buns) and view all of the children contained in that category and choose which ones to add to their order. The two tables are linked using a field called CategoryID .

The Item database contains amongst many, the following fields: ItemID , ItemName , TimesOrdered , CategoryID .

One of the required functions is to view the item that has been ordered the most (most popular) per category. This can be retrieved from the TimesOrdered field. However, if two items have been ordered the same amount of times, then there is technically not any item in that category that has been ordered the most.

Therefore, the largest TimesOrdered field will have to be compared to the second largest TimesOrdered field to determine if any items have been ordered the most for that category.

Is there any way to achieve this using SQL? For example, showing the ItemID for each category (using Grouping on CategoryID) that has been ordered the most as long as the item that has been ordered the second most has been ordered less times than the item that has been ordered the most.

I know that this can obviously be done by simply viewing the first two items and comparing the second record's TimesOrdered field with the first record's TimesOrdered field, but as a challenge and a way to improve my SQL, is their any way to get the desired results by using a single SQL statement?

Thanks in advance for any responses :)

项目表样本数据

在此处输入图片说明

在此处输入图片说明

Would it be possible to share some sample data? For example, what types of records are in your Item table?

How specifically is your Item table related to your Category table? Do you have multiple items per category?

I'd also want to know how the TimesOrdered field gets updated. Is this something that is updated manually by a user whenever that item is ordered, or handled by code?

Regarding the output: It sounds like you want to display, by category, the item with the most orders. Is this correct? If so, would it be displayed via a query the user runs? It sounds like you want to display something different for categories with multiple items having the "max ItemCount" for that category. If a given category has multiple items with max ItemCount, what should display for that category? Could you provide some sample output of what you're expecting to see?

I'm thinking the best way to handle this would be to use multiple sub-queries, which can get rather hairy in Access. It might be best to break this into separate queries in Access, which you can progressively select from

  1. Create a query Q1 that shows the max TimesOrdered for each category.
  2. Create a query Q2 that uses Q1 to figure out how many items for each category have the max TimesOrdered value.
  3. Depending on how you want to display the final results, you could create a new query, Q3 , that either shows NULL for the item in that category (if there's a tie), or the appropriate item. Basically, you'd display the item from each category where the TimesOrdered matches the max TimesOrdered for that category (having to possibly do special handling for categories with ties).

Another thing you might want to think about: What about having a separate Orders table that stores details of each order, rather than having a TimesOrdered field? Of course, that would complicate your queries further, but give you more data to report on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM