473,513 Members | 2,409 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Count average cost and update cost of all products in column with conditions

14 New Member
Hi guys, please help me with following:

I have table "Products" and have following task: Count evarege cost and update cost of all products in column "UnitPrice" with conditions if current price > everage then -10% if < +20%

By the way need to use variables

Please help me to write script I use SQL server 2000

Thanks everybody before.
Mar 25 '13 #1
13 1867
r035198x
13,262 MVP
What have you tried?
Mar 25 '13 #2
gelezniyden
14 New Member
Somthing like that but don't know how to resume

Expand|Select|Wrap|Line Numbers
  1. DECLARE @Average cost
  2.  
  3. SELECT @Average cost = AVG(UnitPrice)  FROM Products  
  4.  
  5.  
  6. Update Products
  7.  
  8. Set UnitPrice = @Average cost
  9.  
  10. DECLARE @Computing
  11.  
  12. IF
Mar 25 '13 #3
r035198x
13,262 MVP
Here is a tip: You can use a CASE clause in an update statement

e.g
Expand|Select|Wrap|Line Numbers
  1. UPDATE someTableName 
  2. SET someColumnName = CASE WHEN (someColumnName < someValue) THEN value1 ELSE value2 END
Now try it again.
Mar 25 '13 #4
gelezniyden
14 New Member
Didn't get you buddy sorry I'm not very good in Transact SQL I've wrote somthing like below:

Expand|Select|Wrap|Line Numbers
  1. DECLARE @Average cost
  2.  
  3. SELECT @Average cost = AVG(UnitPrice)  FROM Products  
  4.  
  5.  
  6. Update Products
  7.  
  8. Set UnitPrice = @Average cost
  9.  
  10. DECLARE @Computing
  11.  
  12. IF UnitPrice > @Average cost SET@Computing= -10%
  13.  
  14. ELSE SET IF UnitPrice < @Averaeg cost SET@Computing = +20%
  15.  
Could you please correct my statment or modefy your according my conditions?
Mar 25 '13 #5
r035198x
13,262 MVP
Don't make variable names with spaces. Use AVERAGE_COST instead of Average cost for the variable name and indicate a type.


In your update you are setting UnitPrice to @Average which is not what you described above. Your description says:

"update cost of all products in column "UnitPrice" with conditions if current price > everage then -10% if < +20%" So your statement should look like
Expand|Select|Wrap|Line Numbers
  1. UPDATE Products
  2. SET UnitPrice = CASE WHEN UnitPrice > @Average THEN <your logic here> ELSE <your other logic here> END
  3.  
Trust me it's better for you learn how to come up with this yourself than have someone do it for you.

You can find more SQL server examples and tutorials here:http://msdn.microsoft.com/en-us/library/ms188927.aspx
Mar 25 '13 #6
gelezniyden
14 New Member
I wrote following:
Expand|Select|Wrap|Line Numbers
  1. UPDATE Products
  2. SET UnitPrice = CASE WHEN UnitPrice > @Average_cost THEN -10% ELSE UnitPrice < @Average_cost THEN + 20%
Please correct if somthing is wrong
Mar 25 '13 #7
r035198x
13,262 MVP
What is -10% supposed to be? It's 10% of what? Also you can't just write plain English. You need to learn and use the correct SQL syntax. Hint: 10% of a value is the same as multiplying a value by 0.1
Mar 25 '13 #8
gelezniyden
14 New Member
Sorry buddy just can't understand it logically, anyway thank you for your help
Mar 25 '13 #9
r035198x
13,262 MVP
Just go through your question again and understand exactly what it's asking for. Think about how you would do it manually on a piece of paper until you really understand what's required. Then go through this thread again and see if it starts to make sense.
Mar 25 '13 #10
gelezniyden
14 New Member
Tried below:

Expand|Select|Wrap|Line Numbers
  1. DECLARE @Average_cost money(8)
  2.  
  3. SELECT @Average_cost = AVG(UnitPrice)  FROM Products 
  4.  
  5. UPDATE Products
  6. SET UnitPrice = (CASE WHEN UnitPrice > @Average_cost 
  7.                       THEN UnitPrice - UnitPrice/10 
  8.                       ELSE UnitPrice < @Average_cost 
  9.                       THEN UnitPrice - UnitPrice/20                      
  10.                 END)
It says Incorrect syntax near '<' I don't understand where I was wrong
Mar 25 '13 #11
r035198x
13,262 MVP
Look at the structure of the examples I posted and compare it with what you have.
Mar 25 '13 #12
gelezniyden
14 New Member
Expand|Select|Wrap|Line Numbers
  1. DECLARE @Average_cost money
  2.  
  3. SELECT @Average_cost = AVG(UnitPrice)  FROM Products  
  4. UPDATE Products
  5. SET UnitPrice = (CASE WHEN UnitPrice > @Average_cost 
  6.                       THEN UnitPrice - (UnitPrice * .1)
  7.                       WHEN UnitPrice < @Average_cost 
  8.                       THEN UnitPrice + (UnitPrice * .2)  
  9.                       ELSE @Average_cost                   
  10.                 END)
Thank you for help I applied statment above and it was helpful for me. Issue was resolved.
Mar 25 '13 #13
r035198x
13,262 MVP
Well done!
Mar 25 '13 #14

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

Similar topics

5
1941
by: Re_endian_miss | last post by:
Sorry in advance if this is a stupid or newbie question, but I have scoured all info source and cannot find the answer... I want to implement a trigger (in db2 8.1) that will fire on the update...
0
1456
by: Tom | last post by:
I created a c# web form. I added a DataGrid1 to it. I added the Edit, Update, Cancel Button Column. I added two Bound columns. All is well. But when I run the app and press the edit...
2
1653
by: Ron | last post by:
Hello, I am trying to create a page that pulls class rosters from an SQLServer database. The roster table definition is: emp_id(pk, fk), sec_id(pk, fk), reg_date and reg_status. Status can be...
5
4558
by: cwbp17 | last post by:
Hi all, Have a datagrid that displays the price column of a table. Went to the Datagrid's Property builder and set the 'Data Formatting expression' for the PRICE column to {0:c}, so that the...
0
1780
by: Manish | last post by:
Hey Guys I am using a datagrid to extract information out of SQL Server datbase. The fields extracted are category,week,budget,Last Year,Forecast and Projection. Also i add a calculated column...
3
4628
by: Randall Skelton | last post by:
What is the storage cost of a null entry in a column? i.e. does a null entry of type integer, float8 or text consume the same amount of storage as one that is filled? I ask because I have...
2
4547
by: Rich | last post by:
Hello, Is there a way to capture the Records Affected count when performing a table Update on a Sql Server table using a DataAdapter? How is this done? Thanks, Rich
2
2327
by: redeye | last post by:
Hi All, I need to do a count of individual items in one column I call status. The status column contains only three values (‘Not Available’, Partially Available’, ‘Fully Available’). My sql...
0
4624
by: SuzK | last post by:
I am trying to calculate in VBA in Access 2002 a moving average and update a table with the calculations. Fields in my WeeklyData table are Week Ending (date) ItemNbr (double) Sales Dollars...
3
1798
by: syafia | last post by:
hye! Im a newbie in MySQL and Php, i got a problem to update column in db. Here is the issue: i have 2 tables named 'apply' and 'balance'. Table: apply userId | leaveType |daysApply...
0
7267
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
7175
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
7553
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
7120
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...
0
5697
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5100
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
3235
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1609
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 ...
0
466
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...

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.