database - SQL issue: Calculating percentages and using multiple joins to the same table -


Here's something I'm trying to do at work. It is said that I have been given this month's data for customers in this store - how much they have been divided by the type of food:

  Customer FOOD_TYPE FOOD_TYPE_VALUE 1 SWEET 52.6 1 Courage 31.0 1 Dairy 45.8 1 drink 12.1 2 sweet 15.1 Toilet 2 44.1 2 drinks 23.4 3 sweet 95.7 3 interview 20.0 3 induces lactation 10.8 3 drinks that 57.1 it has been decided that the client 3 are our ideal client because she demographic profile  

I each For every customer Can receive a percentage allocation to do:

  c1.customer, c1.food_type, 100 * c1.food_type_value / sum (c2.food_type_value) mytable the Mytable C1 Inner C2 Join c1.customer = c2 Customer is c1.customer, c1.food_type, c1.food_type_value  

by client but I am having trouble creating a query that gives me another column with matching percentage values ​​for my ideal customer Will give I.e.:

  Customer FOOD_TYPE FOOD_TYPE_PERC IDEAL_PERC 1 Sweets 37 52 1 Savoring 22 11 1 Dairy 32 6 1 Drink 9 31  

How can I get on Any suggestion without much mess?

"post-text" itemprop = "text">

Include the subset of customer table in which your ideal customer is included:

  select c1 .customer, c1.food_type, 100 * c1.food_type_value / Yoga (c2.food_type_value), c3.FOOD_TYPE_VALUE / Yoga (c2.food_type_value) IDEAL_PERC on join mytable within mytable c1 c2 c1.customer = c2.customer insider When (FOOD_TYPE, select where customers MyTable from FOOD_TYPE_VALUE = 3) group c2.FOOD_TYPE = c3.FOOD_TYPE c1.customer, c1.food_type, c1.food_type_value, c3.FOOD_TYPE_VALUE   

Your reply on my answer shows If you need to divide by it the ideal customer's FOOD_TYPE_VALUE, then do this: Select from within mytable C1

  select c1.customer, c1.food_type, 100 * c1.food_type_value / Yoga (c2.food_type_value), c3.IDEAL_PERC Join Join the mytable c2 c1.customer = c2.customer interiors (s1.FOOD_TYPE, 100 * s1.FOOD_TYPE_VALUE / Yoga (s2.FOOD_TYPE_VALUE) IDEAL_PERC mytable s1 selected within S1. Customer = S2kcustomer where s1.customer on Mytable S2 Join = 3 Group s1.FOOD_TYPE, s1.FOOD_TYPE_VALUE) c1.customer, c1.food_type, c1.food_type_value, c3.IDEAL_PERC  
C2. FOOD_TYPE = c3.FOOD_TYPE by group C3

Comments

Popular posts from this blog

c# - ListView onScroll event -

PHP - get image from byte array -

Linux Terminal Problem with Non-Canonical Terminal I/O app -