select a column with the highest id which have common fields - mysql

i have a table named iview:
gpreq_id a_id m_id rcv_qty
1 100 4 0
2 100 4 1
3 100 5 4
4 101 4 1
5 101 4 10
6 101 4 1
how can i select this that the m_id in the a_id's has the highest gpreq_id?
like:
gpreq_id a_id m_id rcv_qty
2 100 4 1
3 100 5 4
6 101 4 1

First find max value for each a_id, m_id pair and then join to iview:
select i.*
from iview as i
inner join (
select a_id, m_id, max(gpreq_id) as max_gpreq_id
from iview
group by a_id, m_id
) as mi on (mi.max_gpreq_id = i.pgreq_ie)

Try something like
SELECT i1.*
FROM iview as i1
WHERE i1.gpreq_id IN (SELECT MAX(gpreq_id)
FROM iview as i2
GROUP BY i2.a_id, i2.m_id)
Here is the SQL FIDDLE

Related

SQL JOIN two tables & second table only gets latest entry & with 2 ids

I have 2 tables.
MARKET TABLE
ID main_key sub_key name created_at
------------------------------------------------------
1 1 1 bottle 1606636000
2 2 1 flask 1606642546
3 2 2 flask 1606650045
4 3 1 can 1606650445
5 3 2 can 1606651546
6 4 1 glass 1606652545
MARKET_UPDATES TABLE
ID main_key sub_key price update
------------------------------------------------------
1 1 1 100 1606665555
2 2 1 120 1606665555
3 2 2 150 1606665555
4 3 1 500 1606665555
5 3 2 550 1606665555
6 4 1 25 1606665555
7 1 1 110 1606665666
8 2 1 135 1606665666
9 2 2 145 1606665666
10 3 1 490 1606665666
11 3 2 440 1606665666
12 4 1 29 1606665666
I've tried this.
SELECT *
FROM market m
JOIN (
SELECT MAX(id) max_id, fk_main_key
FROM market_update
GROUP BY fk_main_key, sub_key
) m_max ON (m_max.fk_main_key = m.main_key)
JOIN market_update mu ON (mu.id = m_max.max_id)
But it multiples it a lot of times and I end up with a lot of the same lines. I think it multiples main_key amount and sub_key amount.
I am trying to JOIN MARKET_UPDATES into MARKET so I can get the latest prices from MARKET_UPDATES, but my issue is that I have 2 id's that I have to check on, main_key & sub_key.
So I have to merge the MARKET table with id(7-12) in MARKET_UPDATES.
here is one way based on your query :
SELECT *
FROM market m
JOIN (
SELECT MAX(id) max_id , fk_main_key, sub_key
FROM market_update
GROUP BY fk_main_key, sub_key
) m_max
ON (m_max.fk_main_key = m.main_key
and m_max.sub_key = m.sub_key)
JOIN market_update mu ON (mu.id = m_max.max_id)
and here is another way using window function RANK() :
SELECT *
FROM market m
JOIN (
SELECT id
, fk_main_key
, sub_key
, rank() over (partition by fk_main_key, sub_key order by id desc ) rnk
FROM market_updat
) mu
ON mu.fk_main_key = m.main_key
and mu.sub_key = m.sub_key
and rnk = 1

Sql join 4 tables count rows

I have 4 tables in similar form. Structures of these tables are like:
id team_id position_id country_id
1 1 1 3
2 1 1 3
3 2 2 3
4 3 3 3
I can count rows of one table with:
SELECT count(position_id) as count1, position_id
FROM players1
where country_id = 3
group by position_id;
Getting result as:
position_id count1
1 54
2 41
3 39
4 32
I want join 4 tables and want to get a result like:
position_id count1 count2 count3 count4
1 54 42 51 61
2 41 40 49 59
3 39 29 44 50
4 32 21 37 47
Can you help me write this sql?
As I have understand you question. Execute this Mysql query.
SELECT
d1.position_id AS Positions_Id,
d1.count1 AS count1,
d2.count1 AS count2,
d3.count1 AS count3,
d4.count1 AS count4
FROM (
SELECT position_id, COUNT(position_id) AS count1
FROM players1
WHERE country_id=3
GROUP BY position_id) AS d1
LEFT JOIN (
SELECT position_id, COUNT(position_id) AS count1
FROM players2
WHERE country_id=3
GROUP BY position_id
) AS d2 ON d2.position_id = d1.position_id
LEFT JOIN (
SELECT position_id, COUNT(position_id) AS count1
FROM players3
WHERE country_id=3
GROUP BY position_id
) AS d3 ON d3.position_id = d1.position_id
LEFT JOIN (
SELECT position_id, COUNT(position_id) AS count1
FROM players4
WHERE country_id=3
GROUP BY position_id
) AS d4 ON d4.position_id = d1.position_id

MySql get highest count per parent ID

ID item_ID parent_ID count
================================
1 11 2 5
2 12 2 6
3 13 3 2
4 14 3 3
5 15 2 7
6 16 1 3
SELECT * FROM relations ORDER BY count DESC
The row that should be returns are 2,4 and 6 because they have the highest count for their parent_ID
how do i change the query to accomplish this?
The inner select gets the highest count for each parent_ID. If you join against that, it filters out the relevant records
select t1.*
from your_table t1
join
(
select parent_ID, max(count) as mcount
from your_table
group by parent_ID
) t2 on t1.parent_ID = t2.parent_ID
and t1.count = t2.mcount

parent data display with child count

I have a database tbl_products
id parent_id product_name
1 0 abc
2 1 xyz
3 1 fgh
4 1 pqr
5 2 lmn
6 2 uvw
I want to display data like
id child_count product_name
1 3 abc
2 2 xyz
3 0 fgh
4 0 pqr
5 0 lmn
6 0 uvw
You can use the following query:
SELECT id,
(SELECT COUNT(*)
FROM tbl_products
WHERE parent_id = t.id) AS child_count,
product_name
FROM tbl_products AS t
The query uses a correlated subquery to get the number of children of the current record.
Demo here
Alternatively you can use a JOIN:
SELECT t1.id,
COALESCE(t2.cnt,0) AS child_count,
t1.product_name
FROM tbl_products AS t1
LEFT JOIN (SELECT parent_id, COUNT(*) AS cnt
FROM tbl_products
GROUP BY parent_id
) AS t2 ON t1.id = t2.parent_id
Demo here

Getting Max date from multiple table after INNER JOIN

I have two following tables
table 1)
ID | HOTEL ID | NAME
1 100 xyz
2 101 pqr
3 102 abc
table 2)
ID | BOOKING ID | DEPARTURE DATE | AMOUNT
1 1 2013-04-12 100
2 1 2013-04-14 120
3 1 2013-04-9 90
4 2 2013-04-14 100
5 2 2013-04-18 150
6 3 2013-04-12 100
I want to get reault in mysql such that it take the row from table two with MAX DEPARTURE DATE.
ID | BOOKING ID | DEPARTURE DATE | AMOUNT
2 1 2013-04-14 120
5 2 2013-04-18 150
6 3 2013-04-12 100
SELECT b.ID,
b.BookingID,
a.Name,
b.departureDate,
b.Amount
FROM Table1 a
INNER JOIN Table2 b
ON a.ID = b.BookingID
INNER JOIN
(
SELECT BookingID, MAX(DepartureDate) Max_Date
FROM Table2
GROUP BY BookingID
) c ON b.BookingID = c.BookingID AND
b.DepartureDate = c.Max_date
SQLFiddle Demo
Well,
SELECT * FROM `table2` ORDER BY `DEPARTURE_DATE` DESC LIMIT 0,1
should help