I'm trying to build a query in such a way that my coworkers can modify how different cost components are calculated. A simplified version of the query is below:
SELECT
ProductWeight * ShippingCost As Shipping,
ExpectedRevenue * GRTRate * As GRT
FROM PriceTable
I want to allow the user to control how Shipping and GRT calculate, so I created a second table of formulas:
State | Component | Formula
______________________________________________
NJ | Shipping | 'ProductWeight * ShippingCost'
NY | GRT | 'ExpectedRevenue * GRTRate'
The users are able to modify these formulas through a form in an Access front end. Ideally, I'd like to join PriceTable and Formulas on Product and State and evaluate the formula, but I haven't found any way to make that work, and AFAIK SQL doesn't really work like that.
The current workaround I have:
Based on some answers I've seen here about building dynamic queries, I'm using the XML Path method to create a list of Formulas, like so:
Declare @formulas As nvarchar(max)
Set @formulas = (
SELECT DISTINCT Formula + ' As ' + Component + ',' As [text()]
FROM Formulas
For XML Path('')
)
Set @formulas = LEFT(@formulas, len(@Formulas)-1)
Then, I created a variable that builds the query using the formulas:
Declare @query As nvarchar(max)
@query = 'SELECT ' + @formulas + ' FROM PriceTable;'
While this runs, I can't apply different formulas for products sold in different states. This will just return one formula per product regardless of the state. And even with a bunch of conditional logic, it seems pretty clunky. Is there a better way to approach this situation?