Select ONLY one row per category - mysql

Ok I have siple table which contains data:
id cat_id title (with random values)
1 1 test
2 1 tstt
3 3 tewt
4 2 4324
5 3 rterter
Now, I need to create a query which selects only ONE raw per category (cat_id)
(possibly with lowest ID and ordered by cat_id)
So the result should be:
1 1 test
4 2 4324
3 3 tewt

Use GROUP BY :
SELECT MIN(id), cat_id, title FROM table GROUP BY cat_id

SELECT a.*
FROM tableName a
INNER JOIN
(
SELECT cat_id, MIN(id) id
FROM tableName
GROUP BY cat_id
) b ON a.cat_id = b.cat_id AND
a.id = b.id
ORDER BY a.cat_id

Related

mysql count children of selected rows having selected rows as parent?

Table structure(representative)
ID NAME PARENT
--------------------
1 cat1 0
2 cat1 1
3 cat2 1
4 cat1 2
5 cat2 2
6 cat3 2
7 cat1 3
8 cat2 3
9 cat3 3
10 cat1 1
FOREIGN TABLE data for foreign_sub_category_count
id_parent name
-----------------------
2 a
2 b
2 c
3 a
3 b
3 c
categories may have sub categories.
SELECT t.name,t.id
FROM TABLE_NAME AS t
WHERE t.parent = SOME_ID
SOME_ID = 1
gives me the name,id of all categories with SOME_ID parent id
what i want is to get a count of all sub categories of each row in above result set besides the name
WHERE t.id is parent of sub categories and get count of categories from another table which has the same t.id as parent
EXPECTED RESULT
t.id t.name sub_category_count foreign_sub_category_count
2 cat1 3 3
3 cat2 3 3
10 cat1 0 0
I suspect that you are looking for a recursive query - available in MySQL 8.0:
with recursive cte as (
select id root_id, id from mytable
union all
select c.root_id, t.id from cte c inner join mytable t on t.parent = c.id
)
select
t.*,
(select count(*) - 1 from cte c where c.root_id = t.id) no_children
from mytable t
This adds one column to your original table, which contains the number of direct and indirect descendants of the current row.
Try this:
select
tab1.id,
tab1.name,
coalesce(tab2.counts,0) as sub_category_count,
coalesce(tab3.counts,0) as foreign_sub_category_count
from
(select id,name,parent from representative) tab1
left join
(select t1.parent tab_id, count(*) as counts from representative t1 inner join representative t2 on t1.parent=t2.id group by t1.parent) tab2
on tab1.id=tab2.tab_id
left join
(select parent_id,count(*) as counts from foreign_table group by parent_id) tab3
on tab1.id=tab3.parent_id
where tab1.parent=1 --SOME_ID
you can change the parent_id in where tab1.parent=1 of you choice
Example on DB-FIDDLE

Check if ID from a table exists in another table, and if so, how many times

Let's say I have two tables
Table a
some_ID
1
2
3
4
Table b
some_ID
1
2
1
4
Now what I would like to receive is a table like
id amount
1 | 2
2 | 1
I tried with a following query:
SELECT COUNT(a.some_id) as id
FROM Table_a
INNER JOIN Table_b
ON Table_a.some_id = Table.b.some_id
but that only returned how many id rows there are in both tables.
Any help?
Do the grouping on table_b and then join that result set on table_a
SELECT b.* FROM
(
SELECT id, COUNT(*) AS Cnt
FROM Table_b
GROUP BY id
) b
INNER JOIN Table_a a ON a.id = b.id
SQLFiddle
If you want the zero counts:
SELECT a.some_id AS id, count(b.some_id) as amount
FROM a LEFT JOIN b ON a.some_id = b.some_id
GROUP BY a.some_id
Result:
id | amount
1 | 2
2 | 1
3 | 0
4 | 1
If not:
SELECT a.some_id AS id, count(*) as amount
FROM a INNER JOIN b ON a.some_id = b.some_id
GROUP BY a.some_id
Result:
id | amount
1 | 2
2 | 1
4 | 1
The difference is the join type. Once left outer join. Then inner join. Note that in the first case it is important to count with count(b.some_id). With count(*) the rows with missing b entries would be counted as 1. count(*) counts the rows. count(expression) counts the non-null values.
If I understand correctly, you want a histogram of histograms:
select cnt, count(*) as num_ids
from (select id, count(*) as cnt
from b
group by id
) b
group by cnt;

Query to find the sum of the values for duplicate id in MySQL

I have a requirement :
ID Quantity
1 5
1 4
1 2
1 4
2 1
2 2
2 3
3 1
3 3
3 2
Output required :
ID Quantity
1 15
2 6
3 6
I have tried the below query on MySQL but unable to get appropriate result.
SELECT
a.id, sum(b.Quantity)
FROM
​table_name a
INNER JOIN
​table_name b
ON a.id = b.id
​ ​group by
a.id, b.Quantity
Just remove ​b.Quantity from ​group by statement. SQL fiddle
SELECT
a.id, sum(b.Quantity)
FROM
​table_name a
INNER JOIN
​table_name b
ON a.id = b.id
​ ​group by
a.id
http://sqlfiddle.com/#!9/003625/1
SELECT id, SUM(quantity)
FROM `table_name`
GROUP BY id
You don't need any join. Simply try:
select id,sum(quantity) from table_name group by id
Use GROUP BY clause along with sum() like the query below-
select ID, sum(Quantity) FROM table_name GROUP BY ID;

Multiple Aggregate with different Group By

I have table User, Company, ParentCompany and table Goal.
Each Company have ParentCompany, and each User inside one Company. Goal have number of action, and type of Goal, user who execute the goal, and time executed.
I want to calculate the number of action in a date range for each type of Goal, for each user, company, and parent_company. Number of action for each company equal to sum of action for user that reside in that company.
More or less after some join query, I able to get this table below, where column id is id of company, parent_id is id of companyparent, and num is number of goal for all user inside of id company.
id parent_id num
----------- -------------------- -----------------------
1 3 1
2 1 2
3 1 1
4 2 4
Now I want to make it like below:
id parent_id sum_id sum_parent
----------- -------------------- -------------- -------------
1 3 1 1
2 1 2 3
3 1 1 3
4 2 4 4
How can I make it works? I can get one of the value (sum_id or sum_parent) with GROUP BY,
SELECT id,SUM(num) AS sum_id FROM tableA GROUP BY id
or
SELECT parent_id,SUM(num) AS sum_parent FROM tableA GROUP BY parent_id
but is there any way to make it only in one query? tableA results from query with 5 join inside.
Try this:
SELECT a.id, a.parent_id, a.sum_id, b.sum_parent
FROM (SELECT id, parent_id, SUM(num) sum_id FROM tableA a GROUP BY id) a
INNER JOIN (SELECT parent_id, SUM(num) sum_parent FROM tableA a GROUP BY parent_id) b ON a.parent_id = b.parent_id
Try this :
SELECT
t1.id, t1.parent_id, t1.sum_id, t2.sum_parent
FROM
(SELECT id, parent_id, SUM(num) AS sum_id FROM mytable GROUP BY id) t1
INNER JOIN
(SELECT parent_id, SUM(num) AS sum_parent FROM mytable GROUP BY parent_id) t2
ON t1.parent_id = t2.parent_id
Apparently what I want can be done with WITH ROLLUP statement. (http://dev.mysql.com/doc/refman/5.0/en/group-by-modifiers.html)
With
SELECT id,sum(num) FROM tableA GROUP BY parent_id, id WITH ROLLUP;
will results in
parent_id id sum(num)
----------- -------------------- --------------
1 2 2
1 3 1
1 null 3
2 4 4
2 null 4
3 1 2
3 null 2

How can i get the following results using MySQL

Hi I have a table called orders_pending
it has columns order_number and model
order_number | model
1 1
1 2
2 1
2 3
2 5
3 5
4 2
As you can see 1 order can have multiple models , in my table no unique key , but if i take both the order number and model it is unique .
I want a result like this
order_number | model
2 1
2 3
2 5
1 1
1 2
Only order_number's that have more than one model and it should be sort according to the number of orders which are presented in the table
I tried lots of solutions but could not succeed .
Please help . Thanks in advance .
Create a subquery which gets the total number of models and filters out order_number which has multiple models. The result is then join with the table itself. Use the TotalCount column to order the result.
SELECT a.*
FROM orders_pending a
INNER JOIN
(
SELECT order_number, COUNT(*) TotalCount
FROM orders_pending
GROUP BY order_number
HAVING COUNT(*) > 1
) b ON a.order_number = b.order_number
ORDER BY b.TotalCount DESC, a.order_number, a.model
SQLFiddle Demo
First we get the order_numbers which have more than one model.
SELECT order_number, COUNT(*) AS how_many_orders
FROM
orders_pending
GROUP BY order_number
HAVING COUNT(*) > 1;
Then we join this to get every row in the table that belong to those order_numbers.
SELECT
op.order_number, op.model
FROM
orders_pending op
INNER JOIN (
SELECT order_number, COUNT(*) AS how_many_orders
FROM
orders_pending
GROUP BY order_number
HAVING COUNT(*) > 1
) sq ON op.order_number = sq.order_number
ORDER BY how_many_orders, op.order_number