How to get price difference from same table in SQL - php

I have following table
slno date productid companyid price
88 2017-05-17 1 1 65.27
87 2017-05-17 1 2 72.94
86 2017-05-17 1 3 73.13
85 2017-05-17 2 1 73.73
84 2017-05-17 2 2 67.71
83 2017-05-16 1 1 65.40
82 2017-05-16 1 2 72.49
81 2017-05-16 2 1 73.31
80 2017-05-16 2 2 67.17
Now I want price of product 1 for 2017-05-17 and difference of price from yesterday for same company id.
e.g:
getPrice( productid = 1, date='2017-05-17')
and this should return :
companyid , productid , date, price, difference from yesterday:
1, 1,'2017-05-17', 65.27, -0.13
2, 1,'2017-05-17', 72.94, 0.45
...
or it should return:
companyid , productid , date, price, yesterday price:
1, 1,'2017-05-17', 65.27, 65.40
2, 1,'2017-05-17', 72.94, 72.49
...
How to get this in PHP SQL?

You should consider doing this with php, because php can tell you yesterdays and today date. So get yesterdays and today date with php and use it to manipulate the sql query
so the query should look something like this
yesterdays Price:
select companyid, productid , date, price from table where productid='1' and date='$yesterday';
Today price
select companyid, productid , date, price from table where productid='1' and date='$today';
now you can get both values and subtract them with php to get the difference

Related

Select where rows exist more than once with variation

This is a hard one to put a title to so I apologise for ambiguity.
I have the following MySQL table (it's a Magento table):
id attr_id store_id entity_id value
----+---------+---------+------------+------------------------
1 45 0 173 Sale Gifts + Apartment
2 45 0 175 Sale Outerwear
3 45 1 175 Sale Outerwear
4 45 0 177 (null)
5 45 1 177 New Arrivals
6 45 0 178 New Tops
7 45 1 178 New Tops
As you can see, some of the rows have the same everything except store_id.
I want to do the following:
If a row with store_id = 0 has a duplicate row, but with store_id = 1 and different values (for example, rows 4 and 5 above), update the row with store_id = 0 to have the same value as the other.
Delete the row with store_id = 1
I know I will probably need a combination of both PHP and MySQL for this. I just don't know what the MySQL query would be.
Any help would be great!
EDIT
The end goal from the above table is the following
id attr_id store_id entity_id value
----+---------+---------+------------+------------------------
1 45 0 173 Sale Gifts + Apartment
2 45 0 175 Sale Outerwear
4 45 0 177 New Arrivals
6 45 0 178 New Tops
In order to retrive redundunt values having the same entity_id, you can do :
SELECT
*
FROM
magento m1, magento m2
WHERE
m1.attr_id=m2.attr_id
AND
m1.entity_id=m2.entity_id
AND
m1.id > m2.id
And for fixing null values, you will need to loop the above results and search for the null and replace it with the previous or next result.

Retrieve stock balance by category from the following table

I have the following table structure
id stock category created type
11 10 4 2015-06-05 13:30:56 a
12 5 4 2015-06-05 13:31:30 s
14 12 6 2015-06-05 15:58:41 a
15 10 6 2015-06-05 15:59:07 s
16 10 4 2015-06-05 16:00:18 a
17 10 4 2015-06-05 16:00:52 a
18 1 6 2015-06-05 16:01:23 a
20 10 6 2015-06-05 16:51:48 a
21 10 6 2015-06-05 17:00:23 s
22 10 4 2015-06-05 17:02:39 s
23 45 7 2015-06-05 20:48:31 a
Type 'a' is for stock added
Type 's' is for sale
I'm performing following query to get the stock balance, but I need to fetch the balance for each category.
Here is the query I tried so far:
SELECT (
select SUM(stock)
from stock
where type='a'
) - (
select SUM(stock)
from stock
where type='s'
) as balance
I think you can try that (not tested)
select category
, sum(if(type='a', stock, 0)) - sum(if(type='s', stock, 0)) as balance
from stock
group by category
Edit:
I just tested it and it works
If you are using PHP you can use this
SELECT SUM(stock) FROM stock WHERE type= 'a' AND category = 4
put the result in the $var1
SELECT SUM(stock) FROM stock WHERE type= 'b' AND category = 4
put the result in the $var2 and then do
$var = $var1 - $var2
you can loop the SQL calls and set the category number that you need.
There are more solution but I since I do not know the code that you use before and were you put this results this is the best that I can do.

Mysql select count per price then per brand per day

Good day,
This is my first time on this forum and would like to acquire assistance on how do you select in MySQL count per price, per brand per day in which is kind of tricky as the same price may have the same brand. Below is my sample query but not the actual output that I'm expecting, by the way I only have 3 BRAND but multiple price.
SELECT orderDate,prepaid_type,vatprice, count(vatprice) as hits
FROM playdough.playdough_transactions
where deliveryStatus='true' AND orderDate LIKE "2015-01%"
AND (refunded='' OR (refunded='Y' AND SUBSTRING(completed_date,1,10) > "2015-11-18"))
GROUP BY vatprice,prepaid_type,orderDate ORDER BY hits,orderDate DESC;
Output should be as below:
Ex.
orderDate prepaid_type vatprice hits
2015-01 IN_HTOCS 100.00 5
IN_NSN 150.00 1
IN_NSN 100.00 1
IN_NEW 20.00 55
IN_HTOCS 55.00 123
2015-02 IN_HTOCS 20.00 21
IN_NSN 10.00 11
IN_NSN 200.00 2
2015-03 IN_HTOCS 100.00 5
IN_NSN 10.00 7
IN_NSN 11.00 9
Grateful for any assistance on this.
Thank you,

mysql distinct manufacturer,count of 2 other columns and sum of value

Afternoon,
I have a couple of tables in mysql
The first holds the ticket information and status of ticket
The second holds the items and costs for each item.
Each ticket can have multiple items which are stored into the items table.
example table 1
Ticket id Manufacturer status
102 man-A 10
103 man-A 20
104 man-A 10
105 man-C 10
106 man-B 20
example table 2
Ticket id Item Price
102 item_a 100.00
103 item_a 100.00
103 item_b 110.00
103 item_c 115.00
104 item_c 115.00
105 item_b 110.00
106 item_a 100.00
106 item_c 115.00
now on a quick stats page i need to show.
Manufacturer Qty(status 10) Qty(status 20) Value
man-A 2 1 530.00
man-B 0 1 225.00
man-C 1 0 110.00
If there are no tickets with status 10 or status 20 i do not need to show that manufacturer.
I would like to do 1 mysql request to get the stats i need to show.
Many Thanks
Try this with join and using SUM()
SELECT
t1.Manufacturer,
SUM(t1.status =10) `Qty(status 10)`,
SUM(t1.status =20) `Qty(status 20)`,
SUM(t2.price) `Value`
FROM table1 t1
JOIN table2 t2 ON (t1.`Ticket id` =t1.`Ticket id`)
GROUP BY t1.Manufacturer
Fiddle Demo

PHP sql multiple attributes

I have the folowing query that shows my products.
SELECT DISTINCT productsmap.id, attributes_product.waarde,
attributes_product.att_id, productsmap.name, productsmap.category
AS categoryId, brand.naam AS brand, productsmap.sku, categorie.name AS
category, productsmap.price, productsmap.shops
FROM productsmap
INNER JOIN categorie ON productsmap.category = categorie.id
INNER JOIN brand ON productsmap.brand = brand.id
INNER JOIN attributes_product ON productsmap.id = attributes_product.pmapid
WHERE productsmap.category
IN ( 2, 3, 4, 5, 6, 7, 8 )
AND productsmap.shops >0
AND (
productsmap.price >= 3.94
AND productsmap.price <= 204.99
)
AND productsmap.online =1
LIMIT 0 , 30
It gives the folowing results I cropped a bit:
(id) (waarde) (att_id) (name) (categoryId) (brand) (sku)
2 109 1 Name 4 Merk 70000
2 2013 2 Name etc etc etc
2 123 3 Name etc etc etc
3 103 1 Name2 etc etc
3 2012 2 Name2
3 123 3 Name2
4 110 1 3name
4 2013 2 3name
4 102 3 3name
Whit multiple times the same id and name only diffrent att_id and waarde. But i need to add to my query (attributes_product.att_id = 1 and waarde = 109) and (attributes_product.att_id = 2 and waarde = 2013)
But this is not possible because it are diffrent results. How can I solve this issue? Or on what can i search to solve this issue?
It attributes.product are att_id and waarde

Categories