Mysql Group By and Sum Total of the column values - php

enter image description here I am having one table like id, sale_id, item_total, tax fields. need to sum the item_total by grouping the tax values.
Table 1
id | sale_id | item_cost_price | tax |
1 | 10 | 150 | 5 |
2 | 10 | 50 | 7 |
3 | 10 | 30 | 5 |
this is required output:
id | sale_id | item_cost_price | tax |
1 | 10 | 180 | 5 |
2 | 10 | 50 | 7 |
When i tried this query,
SELECT sale_id,tax FROM bgs_ib_sales_items GROUP BY tax
$query=$this->db->query("SELECT sale_id,tax FROM bgs_ib_sales_items GROUP BY tax ");
echo $num = $query->num_rows();
$result=array();
foreach($query->result() as $row){
$result_row[]=$row->sale_id;
$result_row[]=$row->tax;
$result_row[]=$row->item_cost_price;
}
My output is:
i am getting output like this,
am getting distinct tax only. but i need to sum item total values.
Note:
Image 1 : refer my datatable
Image 2: refer my expected outputenter image description here

Add SUM(item_cost_price) in SELECT statement.
In your select statement your select only sale_id, tax. But what you echo are sale_id, tax, and item_cost_price which not exist in your SELECT statement. Try This:-
$sql = "SELECT sale_id,tax, SUM(item_cost_price ) AS TotalPrice FROM bgs_ib_sales_items WHERE sale_id = '10' GROUP BY tax";
$query=$this->db->query($sql);
foreach($query->result() as $row){
$result_row[]=$row->sale_id;
$result_row[]=$row->tax;
$result_row[]=$row->TotalPrice;
}

Related

Why insert data is not shown, only update data is show?

I have three table.First table shows input data, second table shows output data and third is total table where we shows our product total balance.
1.input table
Date | product_name | in_qty
5/7/19 | A | 10
5/7/19 | B | 15
6/7/19 | A | 10
6/7/19 | C | 20
2.output table
Date | product_name | out_qty
7/7/19 | A | 10
8/7/19 | B | 10
3.total balance table
product_name | in_qty | out_qty | total_qty
A | 20 | 10 | 10
B | 15 | 10 | 5
There my problem is in input table product C is not show in the total table. Which product add in the output table only those product total shown in the total table.
$res=mysqli_query($con, "SELECT i.product_name, i.in_qty, o.product_name,o.out_qty
FROM input i, output o
WHERE i.product_name= o.product_name");
while($row=mysqli_fetch_array($res))
{
$in_name = $row['product_name'];
$inqty = $row['Input_qty'];;
$out_name = $row['product_name'];
$outqty = $row['Out_qty'];
$sql=mysqli_query($con, "INSERT INTO total (product_name,Input_qty,Out_qty)
VALUES('$in_name','$inqty','$outqty')");
$sql2=mysqli_query($con, "UPDATE total t2
INNER JOIN (
SELECT product_name, SUM(in_qty) as qty_total
FROM input
GROUP BY product_name
) t1 ON t2.product_name= t1.product_name
SET t2.in_qty = t1.qty_total");
}
My expectation is :
3.total balance table
product_name | in_qty | out_qty | total_qty
A | 20 | 10 | 10
B | 15 | 10 | 5
C | 20 | | 20
your mysql query is wrong.
use following query .
SELECT columns
FROM table1
LEFT [OUTER] JOIN table2
ON table1.column = table2.column;
in your case:
SELECT input.product_name , input.in_qty , output.out_qty
FROM input
LEFT [OUTER] JOIN output
ON input.column = output.column;

mySQL rank order by sum of values in table that have it name in in another table

I have two mySQL database table, I'm using one to store to product information and the other to store the sales information of product sold, where it receive my product_id as the product it sold
product Table example
product_id | prod_name |
------------------------
1 | toyota |
2 | lexus |
3 | wagon |
sale Table example
sale_id | prod_id | qty
------------------------
1 | 1 | 5
2 | 1 | 1
3 | 3 | 2
4 | 1 | 4
5 | 2 | 5
6 | 2 | 1
Now i want the mysqli datebase to tabulate the sum of the most sold product name using php and html table
you can use this query
SELECT
product.product_id,
product.prod_name,
sum(qty) as total
FROM sale
INNER JOIN product
ON
sale.prod_id=product.product_id
GROUP BY
product.product_id
ORDER BY
total DESC
this fiddle if you want to see the results
and you just need to echo the column name if you want to show it to your table on php

How to select only one record of each category?

Products :
--------------------------------------------
| ID | Group | Name | Sold |
--------------------------------------------
| 1 | A | Dell | 0 |
--------------------------------------------
| 2 | A | Dell | 0 |
--------------------------------------------
| 3 | B | Dell | 1 |
--------------------------------------------
| 4 | B | Dell | 1 |
--------------------------------------------
| 5 | C | Dell | 0 |
--------------------------------------------
| 6 | C | Dell | 1 |
--------------------------------------------
Hi everyone, i have a table (products) stored in MySql with many records, for now i'm using this query SELECT * FROM products WHERE sold = 0, in results i get :
--------------------------------------------
| ID | Group | Name | Sold |
--------------------------------------------
| 1 | A | Dell | 0 |
--------------------------------------------
| 2 | A | Dell | 0 |
--------------------------------------------
| 5 | C | Dell | 0 |
--------------------------------------------
i want to get only one record from each group, so the results will be like :
--------------------------------------------
| ID | Group | Name | Sold |
--------------------------------------------
| 1 | A | Dell | 0 |
--------------------------------------------
| 5 | C | Dell | 0 |
--------------------------------------------
You could easily do this by using a distinct clause and removing the id column. If you want to keep the id column you need to specify how one would chose which id to keep.
select distinct
`group`
, name
, sold
from
products
where
sold = 0;
To keep the row with the smallest id (as your example shows) something along the lines of the example below would work.
select
id
, `group`
, name
, sold
from
products
where
sold = 0
and id = (
select
min(p.id)
from
products p
where
p.`group` = products.`group`
and p.sold = 0
);
First, change your field named Group to something like Group_Name. GROUP is a reserved keyword, and if it is not causing you problems now it probably will later.
Second, you should ask yourself what you are really after. The following query should generate your desired result. It adds an additional condition where the IDs that are returned are the lowest numbered ID in each group.
SELECT * FROM products
WHERE sold = 0
AND ID IN (SELECT MIN(ID) FROM products WHERE sold = 0 GROUP BY Group_Name)
Why do you want that, though? That is not a normal desired end state. You should ask yourself why you care about the ID. It looks like your goal is to figure out which products have not sold anything. In that case, I would recommend this instead:
SELECT DISTINCT Group_Name, Name
FROM products
WHERE sold = 0
ORDER BY Group_Name, Name
I found the solution by using the statement GROUP BY,
SELECT * FROM products WHERE sold = 0 GROUP BY group
in the results now, i get only one record for each group and the minimal id without adding any other statement, and in my real table i am using product_group instead of group because it's a reserved word.
Try this:
SELECT `ID`, `Group`, `Name`, `Sold` FROM products WHERE sold = 0 GROUP BY `Group`;

MySQL Select second count of pre selected results

Hello I am trying to make a Select where the uses chooses department and would like to have clause WHERE this department is first, let's say we select 10 results from department: Taxes and then make a SUM SELECT of fee WHERE status = 1. Which results be selected based on the first select All the results are coming from the same table.
| id | department | status | fee |
----------------------------------
| 1 | tax | 1 | 20 |
| 2 | tax | 2 | 20 |
| 3 | tax | 1 | 20 |
| 4 | accounting | 1 | 20 |
So I would like to select if department is choose as tax, and status is 1 the sum of FEE columns which should be 40
So far my Select query looks like this:
SELECT P.id, P.fee, (SELECT SUM(P.fee) FROM cases P WHERE status = 1) as fee_USD
FROM cases P WHERE 1";
if (!empty($department)) { $sql .= " AND P.department = '$department'"; }
the last line is checking if department is given as select option. there are other options as well but to make it simple I have pasted only this part of it. Any help is welcome.
In the Current Selection Fee is = 80
You have to add correlation to your query:
SELECT P1.id, P1.fee,
(SELECT SUM(P2.fee)
FROM cases P2
WHERE P2.department = P1.department AND status = 1) as fee_USD
FROM cases P1
WHERE 1 ...
This way the subquery will return the SUM of only those records which are related to the current record of the main query.

Calculate price between different dates

Hi everyone I'm new and I need help about my booking hotel, I've this table:
id, idhotel, room, data_start, data_end, price
Now I need to calculate the total price when samebody search the rooms between many days
I'm trying this query but the result is 0.
$query = "SELECT room, SUM(price) FROM price WHERE idhotel='".$_GET['id']."' BETWEEN 'data_start' AND 'data_end'
ORDER BY room";
Can someone one help me?
If I understand correctly you are probably looking for something like this
$date_start = $_GET['date_start'];
$date_end = $_GET['date_end'];
$hotel_id = $_GET['id'];
$query = "SELECT room,
SUM(price) total
FROM tablename
WHERE data_start >= '$date_start'
AND date_end <= '$date_end'
AND idhotel = $hotel_id
GROUP BY room";
I think you need something like this:
SELECT idhotel, room, price, (DATEDIFF(data_end, data_start) * price) as total FROM rooms
For testing create next table and fill itselect * :
create table rooms (id int not null primary key auto_increment, idhotel int, room int, data_start date, data_end date, price int);
+----+---------+------+------------+------------+-------+
| id | idhotel | room | data_start | data_end | price |
+----+---------+------+------------+------------+-------+
| 1 | 1 | 1 | 2013-05-09 | 2013-05-12 | 100 |
| 2 | 1 | 1 | 2013-05-20 | 2013-05-20 | 100 |
+----+---------+------+------------+------------+-------+
Use next query for get total cost (modifed for prevent losing one day):
SELECT idhotel, room, price, ((DATEDIFF(data_end, data_start) + 1) * price) as total FROM rooms;
Result:
+---------+------+-------+-------+
| idhotel | room | price | total |
+---------+------+-------+-------+
| 1 | 1 | 100 | 400 |
| 1 | 1 | 100 | 100 |
+---------+------+-------+-------+
Extend
If you want get room total sum for period, try use this code:
SELECT
idhotel, room, SUM(total) as total_sum
FROM
(
SELECT
idhotel,
room,
price,
(DATEDIFF(data_end, data_start) * price) as total
FROM
rooms
) as t
GROUP BY
idhotel, room;
Result:
+---------+------+-----------+
| idhotel | room | total_sum |
+---------+------+-----------+
| 1 | 1 | 150 |
+---------+------+-----------+

Categories