Nested Queries Help

Sharkie21

Active Member
Joined
Nov 2, 2005
Messages
319
I'm trying to calculate the normalized average cost by area.

SELECT a.[market]
,sum(a.[cost]) as total_cost
,sum(a.[services]) as total_service
,(sum(a.[cost])/sum(a.[services]))/subquery.averagecost_national as Normalized_Average_Cost
FROM [Table A] a,
[Table B] b,
(SELECT
sum(a.[cost])/sum(a.[services]) as averagecost_national
FROM [Table A] a,
[Table B] b
where a.[ID] = b.[ID]) as subquery
where (a.[ID]) = b.[ID]
group by a.[market]
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
I'm not sure what you mean by normalized average, right now you are doing this (cost/service)/(cost/service) which will obviously = 1.
Here is a way for you to normalize the data from 0-1.

Code:
SELECT a.[market] 
,sum(a.[cost]) as total_cost
,sum(a.[services]) as total_service
,((a.[cost] - min(a.[cost])/(max(a.[cost] - min(a.cost])) as normalized_cost
,((a.[cost] - min(a.[service])/(max(a.[service] - min(a.service])) as normalized_service

FROM [Table A] a
INNER JOIN [Table B] b
on b.[ID] = a.[ID]

group by a.[market]
 
Upvote 0
Looks like you would need an inline sql statement still. Something like below. Once again, I know this isn't normalized average, but maybe it will be something to get you started.

Code:
SELECT a.[market] 
,(a.[cost] - min(Inline.[inline_cost]))/(max(Inline.[inline_cost] - min(Inline.[inline_cost]) as normalized_cost
,(a.[service] - min(Inline.[inline_service]))/(max(Inline.[inline_service] - min(Inline.[inline_service])) as normalized_service

FROM [Table A] a
INNER JOIN [Table B] b
on b.[ID] = a.[ID],
(SELECT a.[cost] as inline_cost
,a.[services] as inline_service
FROM [Table A] a
INNER JOIN [Table B] b
on b.[ID] = a.[ID]) Inline

group by a.[market], a.[cost]
 
Upvote 0

Forum statistics

Threads
1,221,834
Messages
6,162,268
Members
451,758
Latest member
lmcquade91

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top