473,508 Members | 2,282 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Update Statement in Table - Please urgent help required...

2 New Member
Hi to All Experts....

I am new in DB side. I need to update all rows for column (fees) in table (MarketValue). This table has around 1500 rows.

TB : MarketValue has following colums
id - int (pk)
open_date - datetime
value - money
fees - money

select * from MarketValue
id ope_date value fees
1 05/05/06 33456.07
2 01/25/03 24256.40
3 03/03/07 45657.23
4 09/29/01 5672.89
5 05/15/07 21345.89
.....
.....

Here I need to update fees as per following condition

@first_mnt, @second_mnt, @third_mnt are decimal

@first_mnt = select cast(('04/15/07' - open_dt) as decimal) / 365 from marketvalue
@second_mnt = select cast(('05/15/07' - open_dt) as decimal) / 365 from marketvalue
@third_mnt = select cast(('06/15/07' - open_dt) as decimal) / 365 from marketvalue


/* THIRD MONTH */
if @third_mnt >= 2
@rate3 = 0.05

if @third_mnt >= 1 and @third_mnt < 2
@rate3 = 0.10

if @third_mnt >= 0 and @third_mnt < 1
@rate3 = 0.20


/* SECOND MONTH */
if @second_mnt >= 2
@rate2 = 0.05

if @second_mnt>= 1 and @second_mnt < 2
@rate2 = 0.10

if @second_mnt >= 0 and @second_mnt < 1
@rate2 = 0.20

/* FIRST MONTH */
if @first_mnt >= 2
@rate1 = 0.05

if @first_mnt >= 1 and @first_mnt < 2
@rate1 = 0.10

if @first_mnt >= 0 and @first_mnt < 1
@rate1 = 0.20


@final_rate = (@rate3 + @rate2 + @rate1) / 3

Update MarketValue
set fees = (value * @final_rate) - for each row in table.

As I have mentioned I am new in DB side I don't know how to this calculation in Stored Procedure. I am trying from last few days but could not able to pen proper code for above equation. All rows has to go through this equation.

Any help will be appreciated.
Aug 20 '07 #1
2 1230
mayur123creation
2 New Member
-- Prepare sample data
set dateformat mdy

declare @marketvalue table (id int primary key, open_date datetime, value money, fees money)

insert @marketvalue (id, open_date, value)
select 1, '05/05/06', 33456.07 union all
select 2, '01/25/03', 24256.40 union all
select 3, '03/03/07', 45657.23 union all
select 4, '09/29/01', 5672.89 union all
select 5, '05/15/07', 21345.89

-- Stage the dataUPDATE m
SET m.Fees = m.Value * d.FinalRate
FROM @MarketValue AS m
INNER JOIN (
SELECT ID,
(
CASE
WHEN First >= 2.0 THEN 0.05
WHEN First >= 1.0 THEN 0.10
WHEN First >= 0.0 THEN 0.20
ELSE 0
END +
CASE
WHEN Second >= 2.0 THEN 0.05
WHEN Second >= 1.0 THEN 0.10
WHEN Second >= 0.0 THEN 0.20
ELSE 0
END +
CASE
WHEN Third >= 2.0 THEN 0.05
WHEN Third >= 1.0 THEN 0.10
WHEN Third >= 0.0 THEN 0.20
ELSE 0
END
) / 3.0 AS FinalRate
FROM (
SELECT ID,
DATEDIFF(DAY, open_date, '04/15/07') / 365.0 AS First,
DATEDIFF(DAY, open_date, '05/15/07') / 365.0 AS Second,
DATEDIFF(DAY, open_date, '06/15/07') / 365.0 AS Third
FROM @MarketValue
) AS q
) AS d ON d.ID = m.ID

-- Show the expected output
SELECT * FROM @MarketValue
Aug 21 '07 #2
azimmer
200 Recognized Expert New Member
Hi to All Experts....

I am new in DB side. I need to update all rows for column (fees) in table (MarketValue). This table has around 1500 rows.

TB : MarketValue has following colums
id - int (pk)
open_date - datetime
value - money
fees - money

select * from MarketValue
id ope_date value fees
1 05/05/06 33456.07
2 01/25/03 24256.40
3 03/03/07 45657.23
4 09/29/01 5672.89
5 05/15/07 21345.89
.....
.....

Here I need to update fees as per following condition

@first_mnt, @second_mnt, @third_mnt are decimal

@first_mnt = select cast(('04/15/07' - open_dt) as decimal) / 365 from marketvalue
@second_mnt = select cast(('05/15/07' - open_dt) as decimal) / 365 from marketvalue
@third_mnt = select cast(('06/15/07' - open_dt) as decimal) / 365 from marketvalue


/* THIRD MONTH */
if @third_mnt >= 2
@rate3 = 0.05

if @third_mnt >= 1 and @third_mnt < 2
@rate3 = 0.10

if @third_mnt >= 0 and @third_mnt < 1
@rate3 = 0.20


/* SECOND MONTH */
if @second_mnt >= 2
@rate2 = 0.05

if @second_mnt>= 1 and @second_mnt < 2
@rate2 = 0.10

if @second_mnt >= 0 and @second_mnt < 1
@rate2 = 0.20

/* FIRST MONTH */
if @first_mnt >= 2
@rate1 = 0.05

if @first_mnt >= 1 and @first_mnt < 2
@rate1 = 0.10

if @first_mnt >= 0 and @first_mnt < 1
@rate1 = 0.20


@final_rate = (@rate3 + @rate2 + @rate1) / 3

Update MarketValue
set fees = (value * @final_rate) - for each row in table.

As I have mentioned I am new in DB side I don't know how to this calculation in Stored Procedure. I am trying from last few days but could not able to pen proper code for above equation. All rows has to go through this equation.

Any help will be appreciated.
I suggest that you create a cursor (see more details in help at DECLARE CURSOR) for each row of the table, open and iterate the cursor with fetch next and checking @@FETCH_STATUS, and put your calculation in the body of your iteration (WHILE cycle).
Aug 21 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

6
3357
by: Paul Eden | last post by:
Hello all. I'm totally stumped. Please de-stump me! I've read my data in from the database, changed it according to what i want to do and now I want to write it back. But it seems I can only...
7
248435
by: Dave | last post by:
I have 2 tables, one with names, and another with addresses, joined by their CIVICID number (unique to the ADDRESSINFO table) in Oracle. I need to update a field in the NAMEINFO table for a...
16
16975
by: Philip Boonzaaier | last post by:
I want to be able to generate SQL statements that will go through a list of data, effectively row by row, enquire on the database if this exists in the selected table- If it exists, then the colums...
16
3844
by: robert | last post by:
been ruminating on the question (mostly in a 390/v7 context) of whether, and if so when, a row update becomes an insert/delete. i assume that there is a threshold on the number of columns of the...
2
3083
by: Miro | last post by:
I will ask the question first then fumble thru trying to explain myself so i dont waste too much of your time. Question / Statement - Every mdb table needs a PrimaryKey ( or maybe an index - i...
3
4778
by: Brad Baker | last post by:
I have a formview with a datasource that contains a select and update command. The select statement works fine but the update command doesn't seem to be working. After some troubleshooting I have...
1
5405
by: Max2006 | last post by:
Hi, I am truing to find a pattern for my Business Logic Layer to be able to work fine win ObjectDataSource's Update method. The challenge is ObjectDataSource is not able to work with an...
3
3931
by: Michel Esber | last post by:
Hi all, DB2 V8 LUW FP 15 There is a table T (ID varchar (24), ABC timestamp). ID is PK. Our application needs to frequently update T with a new value for ABC. update T set ABC=? where ID...
5
3030
by: shanks0092001 | last post by:
Hi All, I need a urgent help on the pl/sql written below declare rate integer; cursor c_f is select treccy,treamt from tredtl where treccy not in ('YEN,'EUR'); begin select fxrate into rate...
0
7229
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7129
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...
0
7333
Oralloy
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,...
0
7398
jinu1996
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...
1
7061
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5057
isladogs
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...
0
4716
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...
0
3208
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1566
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.