How to get Lowest value of each group in mysql - php

I want to get lowest value of each group from two table
Table Are Below
Table 1 Table 2
| GPN | Amt | | GPN | Date |
| A | 10 | | A | 2016-09-10 |
| A | 15 | | A | 2016-09-18 |
| A | 20 | | B | 2016-09-10 |
| A | 25 | | B | 2016-09-11 |
| A | 30 | | B | 2016-09-12 |
| B | 20 | | C | 2016-10-12 |
| B | 40 | | C | 2016-10-13 |
| B | 60 | | C | 2016-10-14 |
| B | 80 | | D | 2016-09-10 |
| B | 100 | | D | 2016-10-13 |
| C | 3 |
| C | 6 |
| C | 9 |
| C | 12 |
| C | 15 |
| D | 7 |
| D | 10 |
| D | 13 |
| D | 16 |
| D | 19 |
| D | 22 |
How i want that value
For Example
Date = 2016-09-10,
On That how many GPN are there so i have to get every GPN's Lowest Amt
So Result Will be like this
Result
| GPN | Amt |
| A | 10 |
| B | 20 |
| D | 7 |
I have tried by using ASC LIMIT 1 so obviously it will show only one raw but but i have no idea how to do that.
and i did it with php loop but i am looking if it is possible in mysql query so that will awesome.

Inner join and group by
select table1.GPN, min(table2.Amt)
from table1
inner join table2 on table1.GPN= table2.GPN
where date(table2.date ) = str_to_date('2016-09-10', '%Y-%m-%d')
group by table1.GPN

This is a classic aggregate function query.
SELECT T1.GPN, MIN(Amt) FROM Table1 T1 INNER JOIN Table2 T2 ON T1.GPN = T2.GPN GROUP BY T1.GPN WHERE T2.Date = ?

Related

Multi table Mysql joint query in Laravel

Am trying to connect two tables Customer and Customer_Usage for getting the result.Tables are showing below.
Table: Customer
+----+-------+-------------+--------+
| id | Ip_Id | Customer_Id | Class |
+----+-------+-------------+--------+
| 1 | 100 | A | First |
| 2 | 100 | B | First |
| 3 | 100 | C | First |
| 4 | 101 | D | First |
| 5 | 101 | E | First |
| 6 | 100 | F | Second |
+----+-------+-------------+--------+
Table: Customer_Usage
+----+-------------+----------------+
| id | Customer_Id | Usage |
+----+-------------+----------------+
| 1 | A | 1245 |
| 2 | B | 4414 |
| 3 | C | 8521 |
| 4 | D | 2314 |
| 5 | E | 521 |
| 6 | F | 5412 |
+----+-------------+----------------+
The condition is while enter a value for search Ip_Id, example for 100 it will give a result like below. How to make a joint for these two tables in Laravel using Mysql
Output result
+-------+-------------+----------------+
| Ip_Id | Customer_Id | Usage |
+-------+-------------+----------------+
| 100 | A | 1245 |
| 100 | B | 4414 |
| 100 | C | 8521 |
| 100 | F | 5412 |
+-------+-------------+----------------+
This is the query am trying.
$result = DB::table('Customer')
->where('Ip_Id','=','100')
->get();
Left join is recommended to achieve what you need.
DB::table('Customer')
->select('Customer.Ip_Id','Customer.Customer_Id','Customer_Usage.Usage')
->leftJoin('Customer_Usage', 'Customer_Usage.Customer_Id', '=', 'Customer.Customer_Id')
->where('Customer.Ip_Id',100)
->get();
Use Eloquent Inner Join like:
$results = DB::table('Customer')
->select('Customer.Ip_Id','Customer.Customer_Id', 'Customer_Usage.Usage')
->join('Customer_Usage','Customer.Customer_Id', '=','Customer_Usage.Customer_Id')
->where('Ip_Id', 100)
->get();
You will get the desired output like above.

How to manage SELECT data from 3 different table's using with JOIN?

I have 3 different table's.
driver
| id | name |
|-----|------|
| 10 | abc |
| 21 | xyz |
booking
| id | driver_id | booking_amount |
|----|-----------|----------------|
| 1 | 10 | 250 |
| 2 | 10 | 150 |
| 3 | 21 | 200 |
| 4 | 21 | 300 |
income
| id | driver_id | credit | debit | date |
|----|-----------|--------|-------|----------|
| 1 | 10 | 100 | | 1-1-2019 |
| 2 | 10 | 250 | | 2-1-2019 |
| 3 | 10 | | 200 | 3-1-2019 |
| 4 | 21 | 250 | | 1-1-2019 |
| 5 | 21 | 400 | | 2-1-2019 |
| 6 | 21 | | 50 | 3-1-2019 |
driver.id = booking.driver_id
driver.id = income.driver_id
I have use this query >>
SELECT driver.*, sum(booking.total_booking_income) as total_income
FROM driver
JOIN booking ON driver.id=booking.driver_id
GROUP BY driver.id
ORDER BY driver.id DESC
Note : but i am not able to add balance field in my this query.
i want to all driver records of income after group of booking and group of income by driver id like
| id | driver_id | driver_name | total_income | balance |
|----|-----------|-------------|--------------|---------|
| 1 | 10 | abc | 400 | -250 |
| 2 | 21 | xyz | 500 | 100 |
Assuming balance is the difference between the credit and the debit then:
SELECT d.*, sum(b.total_booking_income) as total_income,
i.balance
FROM driver d JOIN
booking b
ON d.id = b.driver_id LEFT JOIN
(SELECT i.driver_id,
SUM(i.credit) - SUM(i.debit) as balance
FROM income i
GROUP BY i.driver_id
) i
ON i.driver_id = d.id
GROUP BY d.id, i.balance
ORDER BY d.id DESC;

PHP - Counting some values in column hieararchy

i need help to calculate a value inside a hierarchy array. I have a table like this:
+--------+------------+-------+-----------+
| kia_id | kia_name | value | parent_id |
+--------+------------+-------+-----------+
| 1 | ac service | | 0 |
| 2 | hil | | 0 |
| 3 | dispatch | | 1 |
| 4 | tat main | 13.3 | 3 |
| 5 | tat air | 10.1 | 3 |
| 6 | sla comp | 11.7 | 2 |
| 7 | sla serv | | 2 |
| 8 | slb | 9.9 | 7 |
+--------+------------+-------+-----------+
i want to display to html that table like this :
+--------+----------------+-------+-----------+------------------------+
| kia_id | kia_name | value | parent_id | total_value_from_child |
+--------+----------------+-------+-----------+------------------------+
| 1 | ac service | | 0 | 23.4 |
| 3 | dispatch | | 1 | 23.4 |
| 4 | tat main | 13.3 | 3 | |
| 5 | tat air | 10.1 | 3 | |
| 2 | hil | | 0 | 21.6 |
| 6 | sla comp | 11.7 | 2 | |
| 7 | sla serv | | 2 | 9.9 |
| 8 | slb | 9.9 | 7 | |
+--------+----------------+-------+-----------+------------------------+
how to count all child value to set the total of this parent, please help me..
I think you need to combine a recursive CTE with aggregation. The idea is to start at the "bottom" (leafs) and work your way up. Then aggregate to bring in the original data.
with cte as (
select kia_id, value, kia_id as parent_id, 0 as lev
from t
where not exists (select 1 from t t2 where t2.parent_id = t.kia_id)
union all
select cte.kia_id, cte.kia_value, t.parent_id, cte.lev + 1
from cte join
t
on cte.parent_id = t.kia_id
)
select t.*, c.parent_value
from (select cte.parent_id, sum(kia_value) as parent_value
from cte
group by cte.parent_id
) c join
t
on t.kia_id = c.parent_id;

MYSQL: subquery return as table name in join

Is it possible to use subquery return as a table name in join?
Dynamic Table(dynamictable)
+--------+--------------+----------+-----------+--------+
| tableid| tablename | settings | pack_type | status |
+--------+--------------+----------+-----------+--------+
| 24 | xxxxxx | NULL | F | A |
| 25 | YYYYYY | NULL | M | A |
| 30 | ZZZZZZ | NULL | M | A |
| 26 | AAAAAA | NULL | M | A |
+--------+--------------+----------+-----------+--------+
Product Table(products)
+--------------+------------+
| tableid | product_id |
+--------------+------------+
| 30 | 1 |
| 30 | 2 |
| 25 | 3 |
| 30 | 4 |
+--------------+------------+
xxxxxx
+------------+--------------+
| product_id | product_cost |
+------------+--------------+
| 1 | 350 |
| 2 | 200 |
| 4 | 200 |
+------------+--------------+
i.e(for example):
select *
from products as p
left join (select tablename
from dynamictable as d
where d.tableid = p.tableid) as dt
on dt.product_id = p.product_id;
You need to use LEFT JOIN into subquery.
select *
from products as p
left join (select tablename,products.tableid
from dynamictable as d
left join products on d.tableid = products.tableid
) as dt
on dt.product_id = p.product_id;

mysql inner join 2 tables and order by count

I am having the following tables in my DB
PROJECTS
+----+-------------------------------------------+
| id | name |
+----+-------------------------------------------+
| 1 | YANNONALI COURT |
| 2 | UNIVERSITY OF COLORARDO DENVER RESEARCH 2 |
| 3 | G.R.E.A.T PROGRAM DESALTER BUILDING |
| 4 | MONARCH CLUB |
| 5 | LAFAYETTE MERCANTILE |
| 6 | CAMELBACK VILLAGE RAQUET AND HEALTH CLUB |
| 7 | BACK COUNTRY |
| 8 | URBAN CRASHPAD |
| 9 | PRIVATE RESIDENCE |
| 10 | EATON RESIDENCE |
+----+-------------------------------------------+
PROJECT_ASSIGNMENTS(WHERE projects.id=project_assignment.target_id)
+-------+-----------+-------------+
| id | target_id | property_id |
+-------+-----------+-------------+
| 19178 | 1 | 48 |
| 19192 | 1 | 39 |
| 19391 | 1 | 3 |
| 19412 | 2 | 3 |
| 19591 | 2 | 34 |
| 19610 | 2 | 34 |
| 21013 | 3 | 2 |
| 21032 | 3 | 2 |
| 30876 | 4 | 2433 |
| 38424 | 5 | 2580 |
+-------+-----------+-------------+
PROPERTIES(WHERE properties.id= project_assignment.property_id)
+----+------------------+
| id | name |
+----+------------------+
| 2 | Residential |
| 3 | Multi Family |
| 34 | New Construction |
| 39 | Contemporary |
| 48 | Southwest |
+----+------------------+
I want O/P ordered by no.of projects in the list...
Residential(177) //12 - total no.of projects which is having this property
Multi Family(15)
New Construction(13)
Contemporary(11)
please give me some MySQL queries
Thank You
This should do the trick:
select
c.name,
count(c.id) as CountOfProperties
from
projects a,
project_assignments b,
properties c
where
a.ID=b.target_id
and b.property_id=c.ID
group by
c.name
order by
count(c.id) desc;
Try this::
select
prop.name,
count(prop.id) as CountOfProperties
from
projects p
inner join project_assignments pa on (p.ID=pa.target_id)
inner join properties prop on (pa.property_id=prop.ID)
group by
prop.name
order by
count(prop.id) desc;

Categories