Hi,
I have three tables in the following structure (simplified):
Table 1: Containing the customers
-------------------------------------------------
create table Customers
(
[cusID] int identity(1, 1) not null,
[cusName] varchar(25) not null
)
Table 2: Containing the customer data fields
---------------------------------------------------------------
create table Data
(
[datID] int identity(1, 1) not null,
[datName] varchar(25) not null,
[datFormula] varchar(1500)
)
Table 3: Containing the customer data values
-----------------------------------------------------------------
create table Values
(
[cusID] int not null,
[datID] int not null,
[valValue] sql_variant
)
In this structure the user can add as many data fields to a customer as
he wants (e.g. Country, City, Email, Phone, ...). I have added triggers
which create a view similar to a pivot (I am working in SQL 2000) and
add triggers to the view so it is insertable, deletable and updateable.
What I would like to do, is allow the user to create new fields where
the values are based upon a calculation. This calculation would be done
through a formula similar to what he would do e.g. in excel (this
formula is stored in the dimFormula field then).
An example might help. Let's assume the user created a field 'Sales'
(containing last year's sales) and 'Invoices' (containing the number of
invoices that were created for him last year). Now, he wants to create
a field 'AvgSales' with the formula '[Sales]/[Invoices]'.
(Note that through adding these data fields, the above view was created
(let's assume it is called vw_Customers and contains the columns [ID],
[Name], [Sales], [Invoices], [AvgSales]).
What I am looking for is a function which can parse this formula into a
t_sql query which runs the calculation. So, the formula
'[Sales]/[Invoices]' would be translated into (let's assume there are
no records with NULL or zero invoices):
update vw_Customers
set [AvgSales] = [Sales]/[Invoices]
from vw_Customers
I am able to do the above with simple calculations (where you can even
use sql functions e.g. year, len, ...). Now I would like to take this
one step forward into the possibility of using functions with more
variables.
For example. Let's assume, the user wants to add a rating (field called
'Rating') to his customers based upon the result of 'AvgSales. He
enters the formula 'if([AvgSales] > 2500, 'A', 'B')'.
If anyone could help me on this, I would be very grateful. Thanks.
M 3 5087
Mike wrote: Hi,
I have three tables in the following structure (simplified):
Table 1: Containing the customers ------------------------------------------------- create table Customers ( [cusID] int identity(1, 1) not null, [cusName] varchar(25) not null )
Table 2: Containing the customer data fields --------------------------------------------------------------- create table Data ( [datID] int identity(1, 1) not null, [datName] varchar(25) not null, [datFormula] varchar(1500) )
Table 3: Containing the customer data values ----------------------------------------------------------------- create table Values ( [cusID] int not null, [datID] int not null, [valValue] sql_variant )
In this structure the user can add as many data fields to a customer as he wants (e.g. Country, City, Email, Phone, ...). I have added triggers which create a view similar to a pivot (I am working in SQL 2000) and add triggers to the view so it is insertable, deletable and updateable.
What I would like to do, is allow the user to create new fields where the values are based upon a calculation. This calculation would be done through a formula similar to what he would do e.g. in excel (this formula is stored in the dimFormula field then).
An example might help. Let's assume the user created a field 'Sales' (containing last year's sales) and 'Invoices' (containing the number of invoices that were created for him last year). Now, he wants to create a field 'AvgSales' with the formula '[Sales]/[Invoices]'.
(Note that through adding these data fields, the above view was created (let's assume it is called vw_Customers and contains the columns [ID], [Name], [Sales], [Invoices], [AvgSales]).
What I am looking for is a function which can parse this formula into a t_sql query which runs the calculation. So, the formula '[Sales]/[Invoices]' would be translated into (let's assume there are no records with NULL or zero invoices):
update vw_Customers set [AvgSales] = [Sales]/[Invoices] from vw_Customers
I am able to do the above with simple calculations (where you can even use sql functions e.g. year, len, ...). Now I would like to take this one step forward into the possibility of using functions with more variables.
For example. Let's assume, the user wants to add a rating (field called 'Rating') to his customers based upon the result of 'AvgSales. He enters the formula 'if([AvgSales] > 2500, 'A', 'B')'.
If anyone could help me on this, I would be very grateful. Thanks.
M
The best advice I can give you is to not try doing this with pure SQL.
You'll save yourself a lot of headache if you take some data that's a
little more "raw" and manipulate it in some other programming language
to get the desired result.
Mike (mi*************@hotmail.com) writes: In this structure the user can add as many data fields to a customer as he wants (e.g. Country, City, Email, Phone, ...). I have added triggers which create a view similar to a pivot (I am working in SQL 2000) and add triggers to the view so it is insertable, deletable and updateable.
What I would like to do, is allow the user to create new fields where the values are based upon a calculation. This calculation would be done through a formula similar to what he would do e.g. in excel (this formula is stored in the dimFormula field then). ... For example. Let's assume, the user wants to add a rating (field called 'Rating') to his customers based upon the result of 'AvgSales. He enters the formula 'if([AvgSales] > 2500, 'A', 'B')'.
I can only echo "ZeldorBlat" don't do this in SQL. If you had been on
SQL 2005, you could possibly have used CLR modules for the task.
But I wonder if you are not barking up the wrong tree entirely. Have
you looked at Analysis Services? I'm completely ignorant about Analysis
Services myself, but I would not be surprised if it has some support
for what you are trying to do.
If you are dead set on doing this in SQL 2000, you have to choices:
1) require that the user uses T-SQL syntax, for instance
CASE WHEN [AvgSales] THEN 'A' ELSE 'B' END
2) Define you own forumla language, and parse it in client code and
define the columns in the views as the users defines his formulas.
Beside AS, you could also investigate what 3rd party products out
there that may address your needs.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se
Books Online for SQL Server 2005 at http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at http://www.microsoft.com/sql/prodinf...ons/books.mspx
Look up the EAV design flaw you have re-discovered and stop writing SQL
like this. SQL is not a computational language; it is a database
language. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Al Christians |
last post by:
I've got an idea for an application, and I wonder how much of what it
takes to create it is available in open source python components.
My plan is this -- I want to do a simple spreadsheet-like...
|
by: VM |
last post by:
I'm trying to find out what the best way would be to parse a formula so I
can then calculate it with its real values.
For example, a user may enter
((sys_empHours*db_empSal)/db_totalHours)
and I...
|
by: RJN |
last post by:
Hi
I have a main report and a sub report. I have a formula field on the
main report and one on the sub report. I want the formula in the
subreport to be evaluated after the formula in the main...
|
by: RJN |
last post by:
Hi
Sorry for posting this message again.
I have a main report and a sub report. I have a formula field on the
main report and one on the sub report. I want the formula in the
subreport to be...
|
by: rjn |
last post by:
Hi
I have a main report in which I have inserted a sub report. I have a formula field on the main report and one on the sub report. I want the formula in the
subreport to be evaluated after the...
| |
by: Mark |
last post by:
I must create a routine that finds tokens in small, arbitrary VB code
snippets. For example, it might have to find all occurrences of
{Formula}
I was thinking that using regular expressions...
|
by: barnzee |
last post by:
Hi all, newbie here, but having a go
I am trying to build a stock watchlist in excel 2007 with a dynamic link to a DDE server (paid for from a broker).There is no add-in or plug-in, I just CTL ALT...
|
by: Tom C |
last post by:
Assume a user supplied excel cell formula of "=Model!E75" which could
change so I don't want to hard code it. Inside of a loop I need to
make it increment the row to refrence E76, E77, etc. Any...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |