SQL PHP Retrieve total money for a specific Store - php

Is there a query that retrieve the TotalMoney,TotalPaidMoney,TotalReturnMoney for each StoreName.
Like:
StoreName| TotalPaid | ToatalReturn | TotalMoney
Pizza | sum of PaidMoney | sum of ReturnMoney | sum of TotalMoney
|
From this Table
Date | StoreName|PaidMoney| ReturnMoney| TotalMoney
*********
2014 | Pizaa | 33$ | 12$ | 120$
2014 Main 12$ 23$ 123$
2014 | Pizaa | 33$ | 12$ | 120$
2014 | Main | 33$ | 12$ | 120$
Table I Have:
Store Table

You can easily use a GROUP BY statement, like so :
SELECT StoreName, SUM(PaidMoney) as TotalPaid, SUM(ReturnMoney) as TotalReturn, SUM(TotalMoney) as TotalMoney FROM store_table GROUP BY StoreName

You can use the following command:
SELECT SUM(PaidMoney) AS TotalPaid, SUM(ReturnMoney) AS TotalReturn, SUM(TotalMoney) AS TotalMoney FROM Table WHERE Store='Pizaa';
Or if you want all stores and regroup by store:
SELECT SUM(PaidMoney) AS TotalPaid, SUM(ReturnMoney) AS TotalReturn, SUM(TotalMoney) AS TotalMoney FROM Table GROUP BY Store;

If you really have this currency signs in your table, it might be a bit more complicated. I now assume, that you always have the $ sign as last sign in your column and that there are no decimal values:
SELECT
StoreName,
SUM(PAID_MONEY_VALUE) AS SUM_PAID_MONEY_VALUE,
SUM(RETURN_MONEY_VALUE) AS SUM_RETURN_MONEY_VALUE,
SUM(TOTAL_MONEY_VALUE) AS SUM_TOTAL_MONEY_VALUE
FROM
(
SELECT
StoreName,
CONVERT(int, SUBSTRING(PaidMoney, 1, LEN(PaidMoney - 1))) AS PAID_MONEY_VALUE,
CONVERT(int, SUBSTRING(ReturnMoney, 1, LEN(ReturnMoney - 1))) AS RETURN_MONEY_VALUE,
CONVERT(int, SUBSTRING(TotalMoney, 1, LEN(TotalMoney - 1))) AS TOTAL_MONEY_VALUE
FROM <YOUR_TABLE>
) AS SUBQUERY
GROUP BY StoreName

Related

MySQL - Average of Unique rows

If I have three columns:
id, user, points
My data is:
+-------+------------------+-------------+
| id | user | points |
+-------+------------------+-------------+
| 1 | A | 100 |
+-------+------------------+-------------+
| 1 | A | 200 |
+-------+------------------+-------------+
| 2 | B | 300 |
+-------+------------------+-------------+
| 2 | B | 400 |
+-------+------------------+-------------+
I would like to have the average of ONLY the max points of each user.
For this exmple I want to get as results: 300 points ((200+400)/2).
When I use the following Mysql query, I get: 250:
SELECT avg(points) FROM table
SQL DEMO
Try this :
SELECT avg(points) FROM (
SELECT max(points) as points FROM table1 group by id
) as T
Firstly get the max points of each user and then get the AVG from them.
You should first get the max group by use and the the avg of this subquery
SELECT AVG(points)
FROM (SELECT MAX(points) FROM your_table GROUP BY user) subt

Get the total of multiple column in sql

In database I have such table:
| id hotel_id | room_id | Ac_rooms | Non_ac_rooms | simple_rooms | Furnitured_room | other_rooms | added_by |
| 9 | 2 | 3 | 2 | 6 | 12 | 21 | raj |
I want to get the total numbers of room from SQL query (which is the total of room_id, Ac_rooms, Non_ac_rooms, simple_rooms, Furnitured_room, other_rooms).
What is the best way to get the total from SQL query? I need total number of rows.
Try this:
SELECT
SUM(Ac_rooms) as Ac_rooms,
SUM(Non_ac_rooms) as Non_ac_rooms,
SUM(Simple_rooms) as Simple_rooms,
SUM(Furnitured_rooms) as Furnitured_room,
SUM(Other_rooms) as Other_rooms,
SUM(Ac_rooms+Non_ac_rooms+Simple_rooms+Furnitured_room+Other_rooms) as Total_rooms,
FROM tbl_rooms
Or
SELECT
SUM(SUM(Ac_rooms)+SUM(Non_ac_rooms)+SUM(Simple_rooms)+SUM(Furnitured_room)+SUM(Other_rooms)) as Total_rooms,
FROM tbl_rooms
If I understood you correctly, you just need the sum
select (ac_rooms + Non_ac_rooms + simple_rooms + Furnitured_room + other_rooms) as total_rooms from YOUR_TABLE
Also you specified finding total number of rows, which you can get by using standart count function
select count(*) as number_of_rows from YOUR_TABLE
Or may be you are looking for the sum of types of the rooms through all the rows? In that case you will need
select sum(ac_rooms ), sum(Non_ac_rooms), sum(simple_rooms), sum(Furnitured_room), sum(other_rooms) from YOUR_TABLE
UPD: If I got you right, you need this
select sum(ac_rooms ) as ac_rooms_total,
sum(Non_ac_rooms) as non_ac_rooms_total,
sum(simple_rooms) as simple_rooms_total,
sum(Furnitured_room) as furnitured_room_total,
sum(other_rooms) as other_rooms_total,
sum(ac_rooms + Non_ac_rooms + simple_rooms + Furnitured_room + other_rooms) as TOTAL
from YOUR_TABLE

MySQL SUM With Grouped Date

How to get data Price with grouped date ?
SELECT store.date, store.pic, owner.name,
SUM (CASE WHEN [>>GROUPED store.date<<] THEN store.price) AS total_price
FROM store
JOIN owner
ON store.pic = owner.name GROUP BY store.date
and i want to display like this :
+-------------+------+------------+
| date | name | price |
+-------------+------+------------+
| 2016-09-30 | John | 100000.00 |
| 2016-09-29 | Rey | 125000.00 |
+-------------+------+------------+
Try this will may help you,
SELECT store.date, store.pic, owner.name,SUM(store.price) AS total_price
FROM store
JOIN OWNER ON store.pic = owner.name
GROUP BY store.date
i can't understand why you used case statement in your query

Display only first of duplicate column values

How to query for erase the view below?
+-------------------+------------+
| Order_id | Weight |
| 20 | 4 |
| 21 | 5 |
| 22 | 2 |
| 22 | 2 |
+-------------------+------------+
To be like this:
+-------------------+------------+
| Order_id | Weight |
| 20 | 4 |
| 21 | 5 |
| 22 | 2 |
| 22 | |
+-------------------+------------+
When displaying results but not entered into the database.
A simple way is:
select DISTINCT order_id, weight from xyz
UNION
select order_id, null from xyz
group by order_id, weight
having count(*) > 1
Order by weight desc;
The 1st select statement will display all the unique values and 2nd one will retrieve only the repeated values.
In your required output table, it seems like you want to display all the non-repeated rows and the 1st column value of repeated rows but not 2nd column value. The above query will allow you to do that.
OK, here is how to do it:
SELECT
Order_id,
Weight,
if(#order_id = Order_id, '', Weight) as no_dup_weight,
#order_id := Order_id as dummy
FROM Table1
ORDER BY Order_id asc;
You basically need to check to see if the previous Order_id is the same as the current, and if they are, output an empty field.
Here is an SQLFiddle demonstrating the solution.
Do you actually need 2 rows for the dupes? Can't you just use the DISTINCT clause as per http://www.mysqltutorial.org/mysql-distinct.aspx
Or is it important to know what has duplicates. In which case you should look into the GROUP BY clause

Grab all items from MYSQL Table expired 1 day ago

MYSQL Table trial_list structure as follows...
id | product_id | expiry_date(date) | by_user | curr_datentime(timestamp)
we are able to extend any trial, and if we do that it simply another row with new expiry_date.
Now we would like to get rows got expired yesterday, we are currently using following sql query.....
Sample MYSQL DATASET
+----+-------------+-------------+-------------+----------+---------------------+
| id | product_id | comment | expiry_date | by_user | dnt |
+----+-------------+-------------+-------------+----------+---------------------+
| 2 | 50 | testing | 2011-02-18 | tester | 2011-02-17 23:36:12 |
+----+-------------+-------------+-------------+----------+---------------------+
| 3 | 50 | again | 2011-02-20 | tester | 2011-02-19 20:36:12 |
+----+-------------+-------------+-------------+----------+---------------------+
| 4 | 50 | extend | 2011-02-23 | tester | 2011-02-21 22:36:12 |
+----+-------------+-------------+-------------+----------+---------------------+
$sql = 'SELECT id, product_id, expiry_date, by_user, curr_datentime FROM trial_list WHERE expiry_date < CURDATE() ORDER BY expiry_date DESC';
We believe this is not correct as its getting all rows which date is older than yesterday not updated expiry_date, suppose we have given some user expiry date 1st feb 2011 and then we change again with 12th feb 2011, so it selects 1st feb 2011 entry. I think it makes sense.
What you have to do first is get the latest item per product_id. After that you can further filter it down to those which are expired. Something like:
SELECT a.* FROM
trial_list AS a
LEFT JOIN trial_list AS b ON a.product_id = b.product_id AND a.id < b.id
WHERE b.product_id IS NULL
AND a.expiry_date < curdate()
See http://dev.mysql.com/doc/refman/5.5/en/example-maximum-row.html
Try using NOW() instead of CURDATE(), you are comparing a Date to a Timestamp, NOW() will compare timestamps.

Categories