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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:40:34+00:00 2026-05-12T07:40:34+00:00

Here’s an obfuscated version of something I’ve been trying to do at work. Say

  • 0

Here’s an obfuscated version of something I’ve been trying to do at work. Say I have been given this month’s data for customers in my shop – how much they’ve spent split by the food type:

CUSTOMER    FOOD_TYPE      FOOD_TYPE_VALUE
1           SWEET          52.6
1           SAVOURY        31.0
1           DAIRY          45.8
1           DRINKS         12.1
2           SWEET          15.1
2           SAVOURY        44.1
2           DRINKS         23.4
3           SWEET          95.7
3           SAVOURY        20.0
3           DAIRY          10.8
3           DRINKS         57.1

It has been decided that Customer 3 is our ideal customer as he fits the demographic profile, and we want to track month by month, how everybody’s distribution of shopping preferences differs from his.

I can get the percentage allocation for each food type for each customer with:

SELECT
  c1.customer,
  c1.food_type,
  100 * c1.food_type_value / sum(c2.food_type_value)
FROM
  mytable c1 INNER JOIN mytable c2
  ON c1.customer = c2.customer
group by c1.customer, c1.food_type, c1.food_type_value

But I am having trouble constructing a query that will give me a further column with the matching percentage values for my ideal customer. ie:

CUSTOMER    FOOD_TYPE      FOOD_TYPE_PERC  IDEAL_PERC
1           SWEET          37              52
1           SAVOURY        22              11
1           DAIRY          32              6
1           DRINKS         9               31

Any tips on how I can achieve this without too much mess?

  • 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-12T07:40:34+00:00Added an answer on May 12, 2026 at 7:40 am

    Join it on the subset of the customer table that contains your ideal customer:

    SELECT
      c1.customer,
      c1.food_type,
      100 * c1.food_type_value / sum(c2.food_type_value),
      c3.FOOD_TYPE_VALUE / sum(c2.food_type_value) as IDEAL_PERC
    FROM
      mytable c1 INNER JOIN mytable c2
      ON c1.customer = c2.customer
      INNER JOIN (
        SELECT FOOD_TYPE, FOOD_TYPE_VALUE
        FROM mytable
        WHERE customer = 3) c3
      ON c2.FOOD_TYPE = c3.FOOD_TYPE
    group by c1.customer, c1.food_type, c1.food_type_value, c3.FOOD_TYPE_VALUE
    

    Your comment to my answer suggests you need to divide by the FOOD_TYPE_VALUE of the ideal customer, so do this:

    SELECT
      c1.customer,
      c1.food_type,
      100 * c1.food_type_value / sum(c2.food_type_value),
      c3.IDEAL_PERC
    FROM
      mytable c1 INNER JOIN mytable c2
      ON c1.customer = c2.customer
      INNER JOIN (
        SELECT s1.FOOD_TYPE,
               100 * s1.FOOD_TYPE_VALUE / sum(s2.FOOD_TYPE_VALUE) IDEAL_PERC
        FROM mytable s1
        INNER JOIN mytable s2
        on s1.customer = s2.customer
        WHERE s1.customer = 3
        GROUP BY s1.FOOD_TYPE, s1.FOOD_TYPE_VALUE) c3
      ON c2.FOOD_TYPE = c3.FOOD_TYPE
    group by c1.customer, c1.food_type, c1.food_type_value, c3.IDEAL_PERC
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's a basic regex technique that I've never managed to remember. Let's say I'm
Here's a problem I ran into recently. I have attributes strings of the form
Here is the issue I am having: I have a large query that needs
Here's my scenario - I have an SSIS job that depends on another prior
Here is my code, which takes two version identifiers in the form 1, 5,
Here's a coding problem for those that like this kind of thing. Let's see
Here we go again, the old argument still arises... Would we better have a
Here's task: We have an sql server database. which is hosted at our server.
Here is a simplification of my database: Table: Property Fields: ID, Address Table: Quote
Here is the scenario: I'm writing an app that will watch for any changes

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.