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 9268515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:50:07+00:00 2026-06-18T14:50:07+00:00

Below, is the schema of my brand_of_items table. For simplicity, shown here with two

  • 0

Below, is the schema of my brand_of_items table. For simplicity, shown here with two columns: id (primary and AI), symbol (varchar 50, unique)

Table - brand_of_items
id   symbol
0    a
1    b
2    c
..   ..
10   j

Below, is the schema of my items_of_brand.

Table - mainIndexQuantity
id  brand_of_items_id   vol  item_type  salefinalizeddate
0         1              5      0       2005-5-11
1         1              6      0       2004-5-11
2         1              7      0       2011-5-11
3         1              8      0       2011-5-12
4         1              9      0       2011-5-12
5         1              10     0       2011-5-11
6         1              5      1       2012-5-11
7         1              6      1       2012-5-11
8         1              7      1       2011-5-11
9         1              8      1       2010-5-12
10        1              9      1       2012-5-12
11        1              10     1       2005-5-12

The mainIndexQuantity table brand_of_items_id columns is a foreign key which points to brand_of_items (id).
The mainIndexQuantity table item_type column is not a foreign key, which it should be.

The two item types are: 0 = retail and 1 = wholesale

I want to calculate the ratio of the types of items (retail vs wholesale) per each_brand_of_items table entry. The goal is to see if the a brands item is selling more in retail or wholesale.


**
Adding Complexity:
I want to add a date column to mainIndexQuantity table and want to find out the difference in sum of RetailVolume and WholesaleVolume and group the results by salefinalizeddate field.

This is to help determine what items in what seasons sold more and the (delta) difference in sum of RetailVolume & WholeSaleVolume will help to select items to pay most attention to.

  • 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-06-18T14:50:08+00:00Added an answer on June 18, 2026 at 2:50 pm

    Try this:

    SELECT 
      b.id,
      b.symbol,
      IFNULL(SUM(m.item_type = 1), 0) / (COUNT(*) * 1.0) AS wholesaleRatio,
      IFNULL(SUM(m.item_type = 0), 0) / (COUNT(*) * 1.0) AS RetailRatio
    FROM brand_of_items b
    LEFT JOIN mainIndexQuantity m ON b.id = m.brand_of_items_id
    GROUP BY b.id, 
             b.symbol;
    

    SQL Fiddle Demo.

    This will give you:

    | ID | SYMBOL | WHOLESALERATIO | RETAILRATIO |
    ----------------------------------------------
    |  0 |      a |              0 |           0 |
    |  1 |      b |            0.5 |         0.5 |
    |  2 |      c |              0 |           0 |
    | 10 |      j |              0 |           0 |
    

    Assuming that:

    • wholesaleRatio is the count of the items of type Whole sale to the count of all items.
    • RetailRatio is the count of the items of type retail to the count of all items.

    If this ration is for the total sum of the vol column to the total vol you can do this instead:

    SELECT 
      b.id,
      b.symbol,
      SUM(CASE WHEN m.item_type = 1 THEN m.vol ELSE 0 END) / SUM(m.vol) AS wholesaleRatio,
      SUM(CASE WHEN m.item_type = 0 THEN m.vol ELSE 0 END) / SUM(m.vol) AS RetailRatio
    FROM brand_of_items b
    LEFT JOIN mainIndexQuantity m ON b.id = m.brand_of_items_id
    GROUP BY b.id, 
             b.symbol;
    

    Note that:

    • I used LEFT JOIN, so that you got the unmatched rows in the result set, i.e, those brand items that has no entries the MainIndexQuantity table. If you don’t want to include them, use INNER JOIN instead.
    • The multiply with 1.0 to get the count with decimal places, as noted by @JW.

    Update 1

    To include the Total Volume, Retail Volume Sum and Wholesale Volume sum try this:

    SELECT 
      b.id,
      b.symbol,
      IFNULL(SUM(m.item_type = 1), 0) * 1.0 / COUNT(*)     AS wholesaleRatio,
      IFNULL(SUM(m.item_type = 0), 0) * 1.0 / COUNT(*)     AS RetailRatio,
      IFNULL(SUM(m.vol), 0)                                AS 'Total Volume',
      SUM(CASE WHEN m.item_type = 1 THEN m.vol ELSE 0 END) AS 'Retail Volume sum',
      SUM(CASE WHEN m.item_type = 1 THEN m.vol ELSE 0 END) AS 'Wholesale Volume sum'
    FROM brand_of_items b
    LEFT JOIN mainIndexQuantity m ON b.id = m.brand_of_items_id
    GROUP BY b.id, 
             b.symbol;
    

    Updated SQL Fiddle Demo.

    This will give you:

    | ID | SYMBOL | WHOLESALERATIO | RETAILRATIO | TOTAL VOLUME | RETAIL VOLUME SUM | WHOLESALE VOLUME SUM |
    --------------------------------------------------------------------------------------------------------
    |  0 |      a |              0 |           0 |            0 |                 0 |                    0 |
    |  1 |      b |            0.5 |         0.5 |           90 |                45 |                   45 |
    |  2 |      c |              0 |           0 |            0 |                 0 |                    0 |
    | 10 |      j |              0 |           0 |            0 |                 0 |                    0 |
    

    If you want to sort the result set by these total and sums, put this query in a subquery, then you can do this:

    SELECT *
    FROM
    (
       SELECT 
          b.id,
          b.symbol,
          IFNULL(SUM(m.item_type = 1), 0) * 1.0 / COUNT(*)     AS wholesaleRatio,
          IFNULL(SUM(m.item_type = 0), 0) * 1.0 / COUNT(*)     AS RetailRatio,
          IFNULL(SUM(m.vol), 0)                                AS TotalVolume,
          SUM(CASE WHEN m.item_type = 1 THEN m.vol ELSE 0 END) AS RetailVolumeSum,
          SUM(CASE WHEN m.item_type = 1 THEN m.vol ELSE 0 END) AS WholesaleVolumeSum
        FROM brand_of_items b
        LEFT JOIN mainIndexQuantity m ON b.id = m.brand_of_items_id
        GROUP BY b.id, 
                 b.symbol
    ) AS sub
    ORDER BY RetailVolumeSum    DESC, 
             WholesaleVolumeSum DESC;
    

    But your last requirement is not clear, are you looking for those brand of items that has the highest of retio/wholesale ratis and volumns or select the highest values of them?

    For the later one:

    SELECT *
    FROM
    (
       SELECT 
          b.id,
          b.symbol,
          IFNULL(SUM(m.item_type = 1), 0) * 1.0 / COUNT(*)     AS wholesaleRatio,
          IFNULL(SUM(m.item_type = 0), 0) * 1.0 / COUNT(*)     AS RetailRatio,
          IFNULL(SUM(m.vol), 0)                                AS TotalVolume,
          SUM(CASE WHEN m.item_type = 1 THEN m.vol ELSE 0 END) AS RetailVolumeSum,
          SUM(CASE WHEN m.item_type = 1 THEN m.vol ELSE 0 END) AS WholesaleVolumeSum
        FROM brand_of_items b
        LEFT JOIN mainIndexQuantity m ON b.id = m.brand_of_items_id
        GROUP BY b.id, 
                 b.symbol
    ) AS sub
    ORDER BY RetailVolumeSum    DESC, 
             WholesaleVolumeSum DESC,
             TotalVolume        DESC
    LIMIT 1;
    

    Update 2

    To get those brands that has the highest total volume, you can do this:

    SELECT 
      b.id,
      b.symbol,
      IFNULL(SUM(m.item_type = 1), 0) * 1.0 / COUNT(*)     AS wholesaleRatio,
      IFNULL(SUM(m.item_type = 0), 0) * 1.0 / COUNT(*)     AS RetailRatio,
      IFNULL(SUM(m.vol), 0)                                AS TotalVolume,
      SUM(CASE WHEN m.item_type = 1 THEN m.vol ELSE 0 END) AS RetailVolumeSum,
      SUM(CASE WHEN m.item_type = 1 THEN m.vol ELSE 0 END) AS WholesaleVolumeSum
    FROM brand_of_items b
    LEFT JOIN mainIndexQuantity m ON b.id = m.brand_of_items_id
    GROUP BY b.id, 
             b.symbol
    HAVING SUM(m.vol) = (SELECT MAX(TotalVolume)
                         FROM
                         (
                           SELECT brand_of_items_id, SUM(vol) AS TotalVolume
                           FROM mainIndexQuantity
                           GROUP BY brand_of_items_id
                         ) t);
    

    Like this.

    Note that:

    • This will give you the brands that has the highest total volume, if you are looking for those that has the highest ratio, you have to replace the having clause to get the max of the ratio rather than the max of total volume.

    • This will give you the items that have the highest total volume, so you might expect to have more than item, in case there was multiple items having the highest total volume, like in this updated fiddle demo. In this case, to get only one, you have to use LIMIT to return only one.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Table Schema For the two tables, the CREATE queries are given below: Table1: (file_path_key,
edit: my table schema below is poorly normalized as suggested and I have revamped
I have two tables names book_list and book_category . Schema is like below: book_list
Please have a look at the schema below: CREATE TABLE Person (id int not
I have a table with the schema below. I need to fetch the last
I have the schema below CREATE TABLE `vocabulary` ( `vid` int(10) unsigned NOT NULL
I have a many-to-many relation with a linked table. See (simplified) schema below. Created
I have a table as below Schema: ID | Category | Keyword | Bid
Here the SQL FIDDLE and below tabel schema with values. I want to select
I have a products table with below schema (ProductID INT, ProductStartDate date, ProductExpDate date,

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.