query to increase price value by 15% in selected rows - mysql

I need to increase price by 15% in some rows of a table named my_products, based on the refresh_time of the product itself.
I don't know how to write this query, I was trying:
UPDATE my_products SET price = (price + 15%) WHERE refresh_time like "%2013%"
But this doesn't work.

UPDATE my_products SET price = (price * 1.15) WHERE refresh_time like "%2013%"
Just multiply the amount times 1.15. The 1 keeps the original value and the .15 adds the additional 15%.

Related

MULTIPLY EACH ROW VALUES WITH DIFFERENT VALUES

Hello everyone,
Here I am trying to do the multiplication of reach row multiplied by different values.
For example..,
I have 3 columns with customer_id(int), product_name(varchar) , price(int)
Now, In the column of Product_name = LV, MK, Price column should be multiplied by 10 (Price * 10) and Product_name = YSL product Price column should be multiplied by 20 (Price * 20). Display the results as a new columns as points achieved by each customer.
Could you please tell me how can I achieve this..,
Your help could be appreciated
You can get it done by incorporating a CASE statement directly in the SQL.
SELECT
customer_id,
product_name,
CASE
WHEN product_name IN ('LV', 'MK') THEN price * 10
WHEN product_name = 'YSL' THEN price * 20
ELSE price
END as adjusted_price
FROM TABLENAME;

MySQL update with conditions based on another tables value

I have two tables in MySQL. I would like to set the value of the 'Total' column to be equal to the Ratio x (Table A column if the item begins with A) or Ratio x (Table B column if the item begins with B) and the Month column in both tables match.
create table zeus(id int primary key,
Month varchar(10),
Items varchar(20),
Ratio decimal(3,2),
Total decimal(10,2));
create table ares(id int primary key,
Month varchar(10),
`Value A` decimal(10,2),
`Value B` decimal(10,2));
id
Month
Items
Ratio
Total
1
May
Bag
0.50
2
May
Apple
0.3
3
May
Bottle
0.25
4
May
Acorn
0.1
5
May
Alarm Clock
0.6
6
May
Bottle
0.25
id
Month
Total A
Total B
1
May
600
800
2
June
780
400
I have tried this
update zeus
set Total = (CASE
WHEN Items like ('A%') and Month = 'May'
THEN Ratio * (select(ares.`Value A`)
WHERE Month = 'May')
WHEN Items like ('B%') and Month = 'May'
THEN Ratio * (select(ares.`Value B`)
WHERE Month = 'May')
END);
But I can't seem to figure it out.
It looks like you just want:
update zeus
inner join ares using (Year,Month)
set zeus.Total = case
when zeus.Items like 'A%' then ares.`Value A` * zeus.Ratio
when zeus.Items like 'B%' then ares.`Value B` * zeus.Ratio
end
That will set the Total to null if Items starts with neither A nor B; add an else clause if you want it set to blank or left as the existing zeus.Total value.
Note that having arrays of column names for related values rather than a separate table with one value per row is usually considered bad database design.

Trying to round a calculation to 2 decimal place in a new column in mySQL

So I am trying to...
Write a SELECT statement that returns these column names and data from the Products table:
product_name
list_price
discount_percent
discount_amount
A column that’s calculated from the previous two columns
discount_price
A column that’s calculated from the previous three columns
Round the discount_amount and discount_price columns to 2 decimal places.
Sort the result set by discount price in descending sequence.
Use the LIMIT clause so the result set contains only the first 5 rows.
So far I have
USE my_guitar_shop;
SELECT product_name, list_price, discount_percent,
CONCAT(discount_percent / 100 * list_price)
AS discount_amount
FROM products
I am able to return discount_amount, but not able to be able to round the column to 2 decimal places.
How would I also go about returning a second column? Such as
ROUND(list_price - discount_amount, 2)
AS discount_price
It says it doesn't recognize discount_amount?
Use ROUND() instead of CONCAT.
ROUND(discount_percent / 100 * list_price, 2)
The first argument is the number to round. The second argument is how many decimal places to round to.
http://dev.mysql.com/doc/refman/5.7/en/mathematical-functions.html#function_round
Blockquote
How would I also go about returning a second column? Such as
ROUND(list_price - discount_amount, 2)
AS discount_price
You can't reuse an alias like that. I would just repeat the expression
ROUND(list_price - (discount_percent / 100 * list_price), 2)
AS discount_price

Calculating Cumulative Commission Amounts

I have a commission banding table containing the following:
Lower Upper Percentage
0 300 45
300.01 800 50
800.01 1500 55
The lower and upper amounts are currency values, and i need to calculate a cumulative amount to pay based on the total sales, using the percentage amount relevant to the total sales.
If i then have a total sales amount of 350, my commission should be calculated as the following:
first 300 of the total would be using the 45%
the remaining 50 would be using the 50%
so my total would be
300*45% = 135
50*50% = 25
Total = 160
I am updating a table with the amounts via a sproc so would need to accommodate this in there.
How is the best way to go about this?
Note : the sproc below has the correct column names, where as the example above i changed the names of the columns for simplicity. The SPROC also joins the table where the bands are stored, and the update table is a kind of working/reporting table
EDIT : sproc update section:
UPDATE CommissionCalculationDetails
SET TotalCommissionAmount =
case
when TotalSales > Upper then Upper
when TotalSale > Lower then #sales - Lower
else 0
end
* Percentage / 100
FROM CommissionCalculationDetails
LEFT JOIN CommissionBand
ON TotalSales > CommissionBand.Lower
AND TotalSales < CommisionBand.Upper
I propose that you store non-inclusive lower bounds instead (ex: 300 instead of 300.01) and use strictly greater than when comparing against it. As it stands, the value 300.005 would not be correctly categorized.
If this was the case, you could compute the total commission using the following query:
select
sum (
case
when #sales > Upper then Upper
when #sales > Lower then #sales - Lower
else 0
end
* Percentage / 100
) as TotalCommission
from CommissionTable
Here's an online test version of this: http://www.sqlfiddle.com/#!3/87f12/8
Slightly offtopic: Your table currently contains redundant information; each lower bound is (more or less) equal to the previous upper bound. Although this is not essential, you could think of a way to store, for example, upper bounds only (and have a null signifying unbounded).
For the update, one possible solution is to extract the commission calculation in a function, like so:
create function ufn_CalculateCommission(#Sales money)
returns money
as
begin
declare #result money
select
#result =
sum (
case
when #sales > Upper then Upper
when #sales > Lower then #sales - Lower
else 0
end
* Percentage / 100
)
from CommissionBand
return #result
end
After declaring the function, the update can be simplified to:
update CommissionCalculationDetails
set TotalCommissionAmount = dbo.ufn_CalculateCommission(TotalSales);
Here's how it works: http://www.sqlfiddle.com/#!3/f4405/4

how to increase or decrease the quantity in a field in my sql when we enter the new quantity

I'm doing a project I'm encountering a problem in my project.........
the problem is i would like to increase or decrease the quantity in my sql quantity field when i pass the issued or delivered quantity i.e....
initially the quantity in the quantity filed is 1 when i intake the product of a quantity 10 then automatically it should update the quantity as 10+1=11
so it must update as 11, if i remove a 1 quantity it should update as 0........
how to write a code in jsp..........
pls do help
if you want to INCREMENT the value of product, use the query below
UPDATE tableName SET columnName = columnName + toadd
and DECREMENT
UPDATE tableName SET columnName = columnName - tosubtract
so to apply in your case,
UPDATE tableName SET quantity = quantity + 10
or
UPDATE tableName SET quantity = quantity - 1