Apportionment Query/Best Practice

Comfy

Well-known Member
Joined
Dec 21, 2009
Messages
3,386
Hi,

I'm slowly but surely learning SQL etc and was hoping someone could advise on the following:

I have these two tables:


Excel 2010
ABC
1IDSiteCost
21A£100.00
32B£200.00
43C£500.00
54D£1,000.00
65E£100.00
Data



Excel 2010
ABCD
1IDSite1Site2Rate
21AE0.25
32AC0.5
43AD0.25
54BE0.8
65BD0.2
SplitMatrix


Using the [Rate] in 'SplitMatrix' I need to return the following From 'Data'


Excel 2010
GHI
4IDSiteCost
53C£550.00
64D£1,065.00
75E£285.00
Query Result


Here is my current Query:

Code:
SELECT C.Site, SUM(C.Cost) As Total
FROM
(SELECT A.Site, A.Cost
FROM Data As A
LEFT JOIN SplitMatrix As B
On A.Site = B.Site1
WHERE B.Site1 IS NULL
UNION ALL
SELECT A.Site2 As Site, (B.Cost * A.Rate) As Cost
FROM SplitMatrix As A
LEFT JOIN Data As B
On A.Site1 = B.Site
WHERE B.Site <> NULL) As C
GROUP BY C.Site

I'm wondering whether this is the correct way to structure the Query or is there an easier way? (easier = fewer lines/more efficient)
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).

Forum statistics

Threads
1,215,045
Messages
6,122,836
Members
449,096
Latest member
Erald

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