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
Related
For input, When quantity value greater then 1, convert in a new row with value 1 for quantity column.
INPUT
ID ProductFK Quantity Price
------------------------------------------------
10 1 2 100
11 2 3 150
12 1 1 120
OUTPUT
ID ProductFK Quantity Price
------------------------------------------------
10 1 1 100
10 1 1 100
11 2 1 150
11 2 1 150
11 2 1 150
12 1 1 120
We can do this using a sequence table trick. Inner join your current table to a sequence on the condition that the quantity be greater than or equal to the sequence value. For example:
SELECT t1.ID, t1.ProductFK, 1 AS Quantity, t1.Price
FROM yourTable t1
INNER JOIN (SELECT 1 AS Quantity UNION ALL SELECT 2 UNION ALL SELECT 3) t2
ON t1.Quantity >= t2.Quantity
ORDER BY t1.ID;
Demo
This question already has answers here:
How can I do a FULL OUTER JOIN in MySQL?
(15 answers)
Closed 2 years ago.
I have two table that I want to combine without FULL OUTER JOIN as it not work in h2 db. I have to create select sql query:I have tried much with UNION too but I think it will not be possible with UNION.
Table 1
id c_id s_id p_date
---------------------------------------------
1 1 1 2020-10-10
2 1 1 2020-10-11
3 2 1 2020-10-11
4 2 2 2020-10-12
Table 2
id c_id s_id s_date
---------------------------------------------
1 1 1 2020-10-15
2 1 2 2020-10-16
3 2 2 2020-10-17
4 2 2 2020-10-17
I am expecting below result:
c_id s_id p_date s_date
-------------------------------------------------
1 1 2020-10-10 2020-10-15
1 1 2020-10-11 -
1 2 - 2020-10-16
2 1 2020-10-11 -
2 2 2020-10-12 2020-10-17
2 2 - 2020-10-17
Please help to get this result.
You can use union all like this:
select t1.c_id, t1.s_id, t1.p_date, t2.s_date
from table1 t1 left join
table2 t2
on t1.c_id = t2.c_id and t1.s_id = t2.s_id
union all
select t2.c_id, t2.s_id, t1.p_date, t2.s_date
from table2 t2 left join
table1 t1
on t1.c_id = t2.c_id and t1.s_id = t2.s_id
where t1.c_id is null;
The first subquery gets all the rows where there are matches between the two tables plus rows where table2 has no match for table1.
The second subquery gets the additional rows from table2 that have no match in table1.
Here is a db<>fiddle.
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
I have two tables like this:
table_1
id user_id item_id item_number
15 1 1 7
16 1 2 12
17 1 3 1
18 1 4 0
19 1 5 11
20 5 1 2
21 5 2 2
22 5 3 5
23 5 4 7
24 5 5 1
table_2
id user_id item_id attribute
41 5 1 1
42 1 1 1
43 7 5 1
44 1 4 1
45 1 4 0
I would like to select user_id, item_id and item_number from table_1 and number of rows from table_2 where table_1.user_id = table_2.user_id and table_1.item_id = table_2.item_id.
I have this:
SELECT item_id, item_number, COUNT(attribute) AS number
FROM table_1
LEFT OUTER JOIN table_2 ON table_1.user_id = table_2.user_id
WHERE table_1.user_id='1'
Can you help me?
Expected result:
user_id item_id item_number number
1 1 7 1
1 2 12 0
1 3 1 0
1 4 0 2
1 5 11 0
Try this:
SELECT t1.user_id, t1.item_id, t1.item_number, COUNT(attribute) AS number
FROM table_1 t1
LEFT OUTER JOIN table_2 t2 ON t1.user_id = t2.user_id AND t1.item_id = t2.item_id
WHERE t1.user_id = '1'
GROUP BY t1.user_id, t1.item_id;
you have to use alias as same column names confuses the server to identify which column have to fetch..so you can use this..
SELECT t1.user_id,t1.item_id, t1.item_number, COUNT(attribute) AS number
FROM table_1 t1
LEFT OUTER JOIN table_2 t2 ON t1.user_id = t2.user_id
WHERE t1.user_id='1' GROUP BY t1.user_id, t1.item_id;
Since you don't really need any column values from table_2, I'd go with a sub-query to get the count:
select user_id, item_id, item_number,
(select count(*) from table_2
where table_1.user_id = table_2.user_id
and table_1.item_id = table_2.item_id)
from table_1
WHERE table_1.user_id='1'
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