question

clickhere avatar image
clickhere asked

How can i calculate a formular was stored in sql ?

Hi, I have a table like this, i want to calculate a formular was stored in column TURNOVER_FORMULAR, The formular denpend on STK_CODE. How can i do it in SQL Server 2005 ?![alt text][1] [1]: /storage/temp/3018-stk.png
t-sqltsqlcomputed-column
stk.png (13.2 KiB)
10 |1200 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

· Write an Answer
Tom Staab avatar image
Tom Staab answered
You can use a computed column, but your formula is different on each line, and I don't know the exact rules. The formula to combine all of those variations will be a bit complicated, but it would probably be something like this. ALTER TABLE MyTable ADD turnover_formular AS CASE WHEN incoming_quantity > 1000 THEN total_freight * .5 + incoming_quantity * 1000 WHEN incoming_quantity BETWEEN 1 AND 1000 THEN total_freight * .5 + incoming_quantity * 5000 WHEN incoming_quantity = 0 AND outgoing_quantity > 0 THEN outgoing_weight * 1000 + outgoing_quantity * 500 ELSE total_freight * .45 END ;
1 comment
10 |1200 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Thanks Tom, In this case, the TURNOVER_FORMULAR depend on STK_CODE value. Can i use CURSOR when my data over 10.000 records for every process
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.