arithmetic operation with table column PDO - pdo

I have a simple table with columns of price ,qty. cookeid and part_number.
I have managed somehow to convert my old mysql code to PDO but I need help to complete the 2nd stage.
To get the total price of sales for a customer, I need to choose every row
that has the customers cookie, then multiply qty and price for each row and finally add those values to get the total price.
I got the code that adds the columns together using Sum but don't know how to multiply the qty with each price and then add those together to get the grand total.
$rs = $pd->prepare('SELECT sum(price) FROM mcart WHERE cookieid=:cookie');
$rs->bindParam(':cookie',$_COOKIE[mcartId]);
$rs->execute();
$sum = $rs->fetchColumn();
echo "total price is ".$sum;

Solved it just in case anyone has similar problem.
$sql = "SELECT * FROM mcart";
$users = $pd->query($sql);
$totpr=0;
$totq=0;
foreach ($users as $row) {
// echo "price is: ".$row["price"] . "-". "quantity is: ".$row["qty"];
// ----- calculate total qty ------
$totq=$row["qty"];
$totqty=$totqty+$totq;
// ----- calculate total price ------
$totp=$row["qty"]*$row["price"];
$totpr=$totpr+$totp;
}
echo "Total qty is: ".$totqty;
echo "<br/>";
echo "Total price is: ".$totpr;
echo "<br/>";

Related

How to group on an aggregated data

so I've to gather the data from the table such that
1.Group each order by shipment type.
2.Filter orders for which sum of sales at shipping group level is less than $35 however overall sales is greater than $35.
For eg: If an order has two shipping groups: S2H and SDD.
Then:
S2H = $20
SDD = $25
Order total = $45
In the above example the individual shipment total is less than $35, however order total is greater than $35.
I've tried till here and would like to know how can I move forward from here to get the desirable date:
SELECT
(CASE WHEN Sales > 35 AND Digital = 0 AND SDD = 1 AND BOPS = 0 AND S2H = 0 AND PREORDER = 0 THEN order_no
      ELSE NULL END) AS Above_SDD_Order,
(CASE WHEN Sales <= 35 AND  Digital = 0 AND SDD = 1 AND BOPS = 0 AND S2H = 0 AND PREORDER = 0 THEN order_no
      ELSE NULL END) AS Below_SDD_Order,
(CASE WHEN Sales > 35 AND Digital = 1 AND SDD = 1 AND BOPS = 0 AND S2H = 1 AND PREORDER = 0 THEN order_no
      ELSE NULL END) AS Above_SDD_S2H_Digital_Order,
(CASE WHEN Sales <= 35 AND Digital = 1 AND SDD = 1 AND BOPS = 0 AND S2H = 1 AND PREORDER = 0 THEN order_no
      ELSE NULL END) AS Below_SDD_S2H_Digital_Order,
(CASE WHEN Sales > 35 AND Digital = 0 AND SDD = 1 AND BOPS = 0 AND S2H = 1 AND PREORDER = 0 THEN order_no
      ELSE NULL END) AS Above_S2H_SDD_Order,
(CASE WHEN Sales <= 35 AND Digital = 0 AND SDD = 1 AND BOPS = 0 AND S2H = 1 AND PREORDER = 0 THEN order_no
      ELSE NULL END) AS Below_SDD_S2H_Order,
(CASE WHEN Sales > 35 AND Digital = 0 AND SDD = 1 AND BOPS = 0 AND S2H = 0 AND PREORDER = 1 THEN order_no
      ELSE NULL END) AS Above_Preorder_SDD_Order,
(CASE WHEN Sales <= 35 AND Digital = 0 AND SDD = 1 AND BOPS = 0 AND S2H = 0 AND PREORDER = 1 THEN order_no
      ELSE NULL END) AS Below_Preorder_SDD_Order,
(CASE WHEN Sales <= 35 AND Digital = 1 AND SDD = 1 AND BOPS = 0 AND S2H = 0 AND PREORDER = 0 THEN order_no
      ELSE NULL END) AS Below_Digital_SDD_Order,
(CASE WHEN Sales > 35 AND Digital = 1 AND SDD = 1 AND BOPS = 0 AND S2H = 0 AND PREORDER = 0 THEN order_no
      ELSE NULL END )AS Above_Digital_SDD_Order,
(CASE WHEN Sales <= 35 AND Digital = 0 AND SDD = 1 AND BOPS = 1 AND S2H = 0 AND PREORDER = 0 THEN order_no
      ELSE NULL END) AS Below_BOPS_SDD_Order,
(CASE WHEN Sales > 35 AND Digital = 0 AND SDD = 1 AND BOPS = 1 AND S2H = 0 AND PREORDER = 0 THEN order_no
      ELSE NULL END) AS Above_BOPS_SDD_Order,
(CASE WHEN Sales <= 35 AND Digital = 1 AND SDD = 1 AND BOPS = 1 AND S2H = 0 AND PREORDER = 0 THEN order_no
      ELSE NULL END )AS Below_Digital_BOPS_SDD_Order,
(CASE WHEN Sales > 35 AND Digital = 1 AND SDD = 1 AND BOPS = 1 AND S2H = 0 AND PREORDER = 0 THEN order_no ELSE NULL END )AS Above_Digital_BOPS_SDD_Order
FROM 
(SELECT order_no,
MAX(CASE WHEN (Shipping_Group = 'DIGITAL_GIFT_CARD' OR Shipping_Group = 'DIGITAL' OR Shipping_Group = 'GAME_INFORMER_DIGITAL' OR Shipping_Group = 'LOYALTY') THEN 1 ELSE 0 END ) AS Digital,
MAX(CASE WHEN Shipping_Group = 'GAME_INFORMER_PHYSICAL' OR Shipping_Group = 'PHYSICAL' THEN 1 ELSE 0 END) AS S2H,
MAX(CASE WHEN Shipping_Group = 'PHYSICAL_PREORDER' OR Shipping_Group = 'DIGITAL_PREORDER' OR Shipping_Group = 'PICKUP_PREORDER' THEN 1 ELSE 0 END ) AS Preorder,    
MAX(CASE WHEN Shipping_Group = 'PICKUP' THEN 1 ELSE 0 END ) AS BOPS,
MAX(CASE WHEN Shipping_Group = 'SDD' THEN 1 ELSE 0 END) AS SDD,
SUM(pl_net_price) as Sales
From 
(SELECT order_no, Shipping_Group,pl_net_price
FROM 
eim-prod.EDW_VIEWS.ORDER_SUBMIT_PRODUCT_LINEITEM 
)
group by 1)
group by 1,2,3,4,5,6,7,8,9,10,11,12,13,14;
You can also tell me if my approach is wrong and how I should take it further as I am a newbie with SQL. Thanks

Populate rows based on another row ( Copy Prices from branch 0, to all other branches)

I am trying to populate rows based on another row,
For example
Update the Product Price table where branchID = 0
to all other products where the branchid <> 0 based on each product code
In the table there is
7 rows of the same product, each row is meant to be identical, but the only difference is the branchid
I want all data from branch 0 row to populate the rest for the product
my current update script does run, but it uses up so much space on the transaction log that it fails, and it takes 2 hours to run
UPDATE ProductPrice
SET StandardSell = pp2.StandardSell,
StandardBuy = pp2.StandardBuy,
InternalCost = pp2.InternalCost,
BuyPerID = pp2.BuyPerID,
AverageCostPerID = pp2.AverageCostPerID,
InternalCostPerID = pp2.InternalCostPerID,
SellPerID = pp2.SellPerID
FROM (SELECT BranchID, ProductID, StandardSell, StandardBuy,SellPerID, InternalCost,BuyPerID,AverageCostPerID,InternalCostPerID
FROM ProductPrice
WHERE BranchID = 0
) AS pp2 INNER JOIN
ProductPrice AS pp1
on pp1.ProductID = pp2.ProductID
WHERE pp1.ProductID = pp2.ProductID
I want products to be updated with the prices from branch 0 to all other branches per product.
This is too long for a comment.
The first observation is that you should fix your data model. Repeating the same columns on seven records is evidence that your data is not normalized. The seven columns that you want to update should probably be in the ProductPrice table.
Then you want a ProductBranch table, with additional columns and the branch id.
That said, if you are stuck with the data model, you are stuck with an update that basically updates all rows. Instead, create a new table with all the columns you want:
insert into temp_productprice
select . . .
from . . .;
Then truncate productprice and insert the new data into it. A bulk insert is more efficient than a giant update.
Finally, you could also try using window functions:
with toupdate as (
select pp.*,
max(case when branchid = 0 then StandardSell end) as StandardSell_0,
max(case when branchid = 0 then StandardBuy end) as StandardBuy_0,
from productprice pp
. . .
)
update toupdate
set StandardSell = StandardSell_0,
StandardBuy = StandardBuy_0,
. . .
from branchid <> 0;
Thank you for your suggestions,
In the end I just gave the log file more space. Then let it run- it takes 2 hours though lol.
But its done
Thanks the script i posted works for anyone elses use.

SQL error with nested SELECT

The error is 'Operation must use an updatable query'.
I am trying to update the 'orders' table with the information below, however only the price of 1 item will be provided through a text box and I am trying to calculate the total order value using the quantity ordered, which will already be in this table.
The code below includes the data taken from the variables. With the 2 in 'VAT = 2' and 'price = 2' being the price of one single unit (£2.00). The total order value will be stored within the 'price' field and the VAT should be calculated using the same code, but times by 0.2 to give the 20% VAT.
UPDATE orders
SET Invoice_number = 'IN9999',
delivery_note_number = 'DN6000',
price =2 *
(SELECT quantity
FROM orders
WHERE purchase_order_number = 'PO7512'
),
VAT = (2 *
(SELECT quantity
FROM orders
WHERE purchase_order_number = 'PO7512'
)/100) * 20,
shipping = 3
WHERE purchase_order_number = 'PO7512'
Maybe I can't use nested query's this way. I'm not sure, but any help would be appreciated :) Thanks!
Instead of subquerying, you can access the whole record directly in the update, like so:
UPDATE Orders
SET Invoice_number = 'IN9999',
Delivery_note_number = 'DN6000',
Price = 2 * quantity,
VAT = (40 * quantity)/100,
Shipping = 3
WHERE purchase_order_number = 'PO7512'
Note that with fractions it's always better to multiply first and divide later.

Drupal commerce Sql select max price for current taxonomy

Hello (sorry for broken english). I try to select min and max prices (drupal commerce) for current catalog tag. ATM i put in header php code:
$max = db_query("SELECT MAX(`commerce_price_amount`) FROM {`field_data_commerce_price`}")->fetchField();
so I can get max price for all products. same for min.
but i need for current tag. my sql tables looks like this:
table field_data_field_product_catalog:
http://postimage.org/image/tahrk3y29/
table field_data_commerce_price:
http://postimage.org/image/w1yvkh8kn/
some advice plz?
$catalog=arg(2);
$max = db_query("SELECT MAX(`commerce_price_amount`) FROM {`field_data_field_product_catalog`},{`field_data_commerce_price`}
WHERE {field_data_commerce_price.entity_id}={field_data_field_product_catalog.entity_id}
AND {field_product_catalog_tid}=:cnid", array(':cnid' => $catalog))->fetchField();
$max=Ceil($max/100);

Updating a field within a table based on a field within the same table

HI ALL
I wish to update a row within my table from a row within that same table,
I have a table called INVENTORY. it stores PRICES for products.
I have SALES codes and I have STOCK codes which are related.
I wish to transfer all the PRICING from the SALES codes to the STOCK codes.
I wish to do something like this:
update INVENTORY
set PRICE = (SELECT PRICE WHERE CODE = "SALES CODE EQUIVALENT OF THE STOCK CODE IM IN")
WHERE
CODE = "SOME STOCK CODE"
a SALES CODE would look like this "U_12345", its STOCK code equivalent would look like "S_12345"
thanks
You're very close, you just need to specify which table you're selecting from as part of your sub-query...
update INVENTORY
set PRICE = (SELECT PRICE FROM your_table WHERE CODE = "SALES CODE EQUIVALENT OF THE STOCK CODE IM IN")
WHERE
CODE = "SOME STOCK CODE"
Even if "your_table" is INVENTORY, you still need to specify it in there.
The only time it gets 'tricky' is when your selecting from the same table that you're update, AND when you need a value from the updated record in the SELECT statement. In that case you need to differentiate between the two references, using an alias. For example...
update INVENTORY
set PRICE = (SELECT PRICE FROM INVENTORY AS [new_price] WHERE [new_price].CODE = INVENTORY.NEW_CODE)
WHERE
CODE = "SOME STOCK CODE"
UPDATE INVENTORY
SET PRICE = i_sales.PRICE
FROM INVENTORY, INVENTORY i_sales
WHERE INVENTORY.CODE LIKE 'S\_%'
AND i_sales.CODE = REPLACE(INVENTORY.Code, 'S', 'U')
This updates prices for all 'stock' records in INVENTORY with prices from the corresponding 'sales' records.
If you would prefer to update individual prices, change WHERE to something like this:
WHERE i_stock.CODE = 'S_12345'
or this:
WHERE i_stock.CODE IN ('S_12345', 'S_12346')
Note: The FROM syntax used is based on this doc page.