GROUP_CONCAT DISTINCT BY ID - mysql

I have 2 tables like this:
Table a:
ID type
1 1
1 1
2 1
2 2
2 2
3 2
Table b:
ID name
1 a
2 b
3 b
And expected result:
ID type name
1,2 1 a,b
2,3 2 b,b
I have tried:
SELECT GROUP_CONCAT(DISTINCT ID), a.type,
GROUP_CONCAT(SELECT name FROM b WHERE ID = a.ID)
FROM a GROUP BY a.type
but it's not working. I need name is GROUP_CONCAT and DISTINCT by ID.

I have found a solution for my question:
SELECT GROUP_CONCAT(DISTINCT a.id), a.type,
(SELECT GROUP_CONCAT(name) FROM b
WHERE FIND_IN_SET(ID, GROUP_CONCAT(a.ID)))
FROM a GROUP BY a.type

Try this one:
SELECT GROUP_CONCAT(DISTINCT a.id), a.type, GROUP_CONCAT(DISTINCT b.name)
FROM a
LEFT JOIN b ON a.id = b.id
GROUP BY a.type

Related

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;

Mysql group_concat for count in sub query using parent filed is not allowed

I need to process album count for each of the country per artist; however, I have a problem once I do group_concat for count in mysql, I search a bit in stackoverflow, I found I have to do sub select for group_concat. The problem is once I do the sub select in from I can not use a.id from the parent from filed table. I got error like following Unknown column 'a.id' in 'where clause'
This is the query:
SELECT a.seq_id, a.id
(SELECT GROUP_CONCAT(cnt) AS cnt FROM (
SELECT CONCAT_WS('-', mgr.country_code, count(mgr.media_id)) AS cnt
FROM music_album_artists AS ma
JOIN media_geo_restrict AS mgr ON ma.album_id = mgr.media_id
WHERE ma.artist_id = a.id
GROUP BY mgr.country_code
) count_table
) AS album_count
FROM music_artist AS a
WHERE a.seq_id > 0 and a.seq_id < 10000
The sample data in tables:
music_artists:
seq_id id name
1 1 Hola
2 2 Vivi
music_album_artists:
id artist_id album_id
1 1 1
2 1 2
3 1 5
4 1 10
5 2 2
6 2 10
6 2 1
media_geo_restrict:
album_id country_code
1 BE
1 CA
1 DE
1 US
2 CH
2 CA
2 CH
5 DE
10 US
The result I would like to have
seq_id id album_count
1 1 BE--1,CA--2,CH--1,DE--1,US--1
2 2 CA--1,US--2,CH--1
Here is what you need:
select seq_id, id, group_concat(concat(country_code, '--', qtd))
from (
select ma.seq_id, ma.id,
mgr.country_code, count(*) qtd
from music_artists ma
inner join music_album_artists maa
on ma.id = maa.artist_id
inner join media_geo_restrict mgr
on maa.album_id = mgr.album_id
where ma.seq_id > 0 and ma.seq_id < 10000
group by ma.seq_id, ma.id, ma.name,
mgr.country_code
) tb
group by seq_id, id
Here is the working sample: http://sqlfiddle.com/#!9/ff8b5/8
Try this and tell me:
SELECT a.seq_id, a.id, GROUP_CONCAT(cnt) AS cnt
FROM music_artist AS a,
(
SELECT ma.artist_id, CONCAT_WS('-', mgr.country_code, count(mgr.media_id)) AS cnt
FROM music_album_artists AS ma
JOIN media_geo_restrict AS mgr ON ma.album_id = mgr.album_id
GROUP BY mgr.country_code
) AS count_table
WHERE a.seq_id > 0 and a.seq_id < 10000
and a.id=count_table.artist_id
group by a.id

MySQL - how to reorder records depending on counted quantity

Please help me to figure out how to get table like this:
ID Name City
-- ---- ----
1 A 2
2 C 1
3 E 3
4 B 2
5 D 2
6 G 3
7 F 2
... to be sorted like this:
ID Name City
-- ---- ----
1 A 2
4 B 2
5 D 2
7 F 2
3 E 3
6 G 3
2 C 1
in other words I'd like it to be reordered depending on quantity of Names in a City firstly, and by Name secondly.
Cities having more Names should go first.
This should do the trick:
select c.* from c
inner join (
select City, count(*) as cnt from c group by City
) a
on c.City = a.City
order by a.cnt desc, c.name asc
Here is SQL Fiddle
SELECT ID,NAME,CITY,(select count(*) from AS T2 where T2.CITY = T1.CITY) as CITYCOUNT FROM AS T1 order by CITYCOUNT DESC,NAME ;
OR
SELECT ID,NAME,CITY FROM AS T1 order by (select count(*) from AS T2 where T2.CITY = T1.CITY)DESC,NAME ;
Alternatively you can also do this:
ALTER TABLE ADD COLUMN CITYCOUNT INT(11) DEFAULT 0;
UPDATE T1 (select CITY,count(*) as CITYCOUNT from group by CITY) T2 SET T1.CITYCOUNT = T2.CITYCOUNT where T1.CITY = T2.CITY;
SELECT ID,NAME,CITY,CITYCOUNT FROM AS T1 order by CITYCOUNT DESC,NAME
I think your example is not correct, Cities with Value 3 should come on Top, for this you can try this query:
SELECT
ID, Name, City
FROM <TABLE>
ORDER BY City DESC, Name ASC;
Hope this is what you want.
Updated Answer:
SELECT
Id, Name, T.City
FROM <TABLE> T
INNER JOIN
(SELECT DISTINCT
City, Count(City) AS CITYCOUNT
FROM Table_7
GROUP BY City) TEMP ON TEMP.City = T.City
ORDER BY TEMP.CITYCOUNT DESC

Select ONLY one row per category

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