MULTIPLY EACH ROW VALUES WITH DIFFERENT VALUES - mysql

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;

Related

How to sort the result of a query in Sequelize?

I am trying to return the result of an ordered form query (ascending or descending), I know it is done using order: ['columnName', sort order (ASC / DESC)], I have done well when I am only sorting on base to a single column, but suppose I have a table with these columns:
product_id, price, offer_price, offer, description
and I want to sort by price or by offer_price (the offer_price column depends on the offer column, that is, I am interested in the offer_price only if the offer column has a value of 1 which indicates that this product is really on sale).
How could I do so that the results are returned to me ordered, by price or by price_offer, that is, if a product is on sale, I have to take into account the price_offer at the time of ordering, and if it is not on sale then I should take the normal price.
For example:
Product1: product_id = 1, price = 9,000, offer_price = 8,500, offer = 1, description: 'yy'
Product2: product_id = 2, price = 2,000, offer_price = 500, offer = 0, description: 'bbb'
Product3: product_id = 3, price = 1,500, offer_price = 1,000, offer = 1, description: 'ccccc'
the query result in ascending order should be: product3, product2, product1. since product3 is on sale and is the lowest price of all (1,000), product 2 is the following (this has a price_offer = 500 but is not on sale, so the normal price is taken (2,000), and finally the product3 (8,500)).
How could I achieve this using Sequelize? I can't find something to help me with the documentation.
You can use MySQL IF function like this:
order: [sequelize.literal('(IF(offer = 1, offer_price, price))'), sort order (ASC / DESC)]

MySQL: get sum of columns with multiply two columns on behalf of third column

I am trying to get sum of all rows with multiply two column values with where condition but getting some error of mysql. After try some example i achieve my result but i don't know that is right way or not:
Table: store_stocks
i just want to count the stock qty with amount multiply with qty according to with VAT, with non VAT and total stock.
I just created that query:
SELECT sum(qty*sale_price) as total_stock,
(select sum(qty*sale_price) from store_stocks where vat_status = 0 and store_id = 8) as non_vat_stock,
(select sum(qty*sale_price) from store_stocks where vat_status = 1 and store_id = 8) as with_vat_stock
FROM `store_stocks` where store_id = 8 group by store_id
and its showing result:
can any one tell me is there any another way to achieve this, because i think that's query is little bit complicated, each time i am using where in sub query and i also have to implement this query in laravel eloquent.
You do not need subqueries, you can use a condition within the sum() to make it summarise the specific records only:
SELECT sum(qty*sale_price) as total_stock,
sum(if(vat_status = 0, qty*sale_price, 0)) as non_vat_stock,
sum(if(vat_status = 1, qty*sale_price, 0)) as with_vat_stock
FROM `store_stocks` where store_id = 8 group by store_id
You can use a case expression instead of the if() function as well.

Sorting MySQL query by a function instead of a column

If my website display the products price as the output of a function display_price(), how can I sort the query by price ?
e.g.
Product A price is 100 and base currency is USD = 100USD Product B
price is 95 and base currency is EU = 103USD
Order by price ASC; will list Product B then A, but I use this function to output the correct price:
Function display_price($products_price, $currency_value) {
$price = $products['price'] * $products['currency_value'];
return $price;
}
How can I sort my query in this situation ?
Thank you
This implies that you have a simple calculated value in your queried data:
$price = $products['price'] * $products['currency_value'];
So calculate that value in the query itself, not in PHP. Assuming the structure of your query, you'd add a calculated column:
SELECT
some_field,
some_other_field,
and_so_on,
price,
currency_value,
price * currency_value AS product_price -- add the calculated value
FROM
some_table
ORDER BY
product_price -- sort by the calculated value
No need to involve PHP at all. As far as PHP is concerned, the result of the query includes the values it needs and is in the order it needs to be.

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

SQL statement with formula

I am trying to write an SQL statement that will calculate the total value of a purchase order.
Example time!
SELECT PO_Number, LineItem, Quantity, Cost
FROM POs
Say on PO_Number=484 there are 2 units of LineItem-1 at $2 each. There are also 3 units of LineItem-2 at $5 each. Is there a way to output $19? (2 + 2 + 3 + 3 + 3 = 19)
SELECT SUM(Quantity*Cost) WHERE PO_Number = 484;
UPDATE
If you want to show the totals for multiple purchase orders, you need to "group" your results by purchase order:
SELECT SUM(Quantity*Cost)
WHERE PO_Number IN (484,485,486) -- if you want specified ones only, omit for all
GROUP BY PO_Number;
Or...
SELECT PO_Number, SUM(Quantity*Cost)
GROUP BY PO_Number;