473,669 Members | 2,377 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 1235
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
3369
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 use the update statement will only accept explicit fieldnames and value. The fieldnames I can live with, I only have 5 columns in the database, but why can't I pass a $variable to this statement?
7
248459
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 particular surname in a particular town. I can select the records fine with this syntax (testing in Oracle SQL* Plus) SELECT NAMEINFO.LASTNAME, NAMEINFO.FIRSTNAME, NAMEINFO.MIDDLENAME, NAMEINFO.GENDER, ADDRESSINFO.REGION FROM NAMEINFO, ADDRESSINFO...
16
17004
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 must be UPDATED, if not, they must be INSERTED. Logically then, I would like to SELECT * FROM <TABLE> WHERE ....<Values entered here>, and then IF FOUND UPDATE <TABLE> SET .... <Values entered here> ELSE INSERT INTO <TABLE> VALUES <Values...
16
3867
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 table, or perhaps bytes, being updated where the engine just decides, screw it, i'll just make a new one. surfed this group and google, but couldn't find anything. the context: we have some java folk who like to parametize/
2
3101
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 havnt tested the index yet ) so you can use an .UPDATE( dataTable ) on the data adapter. Otherwise you will get an exception error. Is this statement true? ---- Now me fumbling thru
3
4798
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 narrowed the problem down to the department_id parameter which is set from a hidden field. I can confirm this by hard coding the UpdateCommand to: UPDATE configuration SET special_notes=@special_notes WHERE (student_id=@student_id) AND...
1
5409
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 update method that accepts a strongly typed data table as parameters. We have to have an update method with all data table's columns as its parameter. Like what we have here: http://asp.net/learn/data-access/tutorial-16-cs.aspx
3
3952
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
3044
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 where fxfrccy='EUR' for b in c_f
0
8383
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8894
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8803
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8658
tracyyun
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6210
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4206
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4384
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2029
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1787
bsmnconsultancy
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.