I have a set of MySQL data in the below format:
acode bcode unique_code
BA1100018 SE OX120013
BE1100001 CS CS140005
BE1100001 SE SE140079
CS1400000 CS CS140006
CS1400000 CS CS140011
CS1400000 CS CS140009
CS1400000 CS CS140013
OX1100021 OX OX110010
OX1100021 SE OX110013
OX1100021 OX OX980141
I am trying to identify which acode id's have more than one different bcode id. I would like to return a set of data which would filter the non-relevant data out. In this case the data would be returned as follows:
acode bcode unique_code
BE1100001 CS CS140005
BE1100001 SE SE140079
OX1100021 OX OX110010
OX1100021 SE OX110013
OX1100021 OX OX980141
My initial attempt was:
select count(bcode),acode from mydataset group by acode having
count(bcode)>1
I realised that this wasn't going to return me the bcodes, so I tried various permutations of that. Not to much success. I have been trying to get this done as efficiently as possible, as I am dealing with a large dataset, but I am having difficulty with the aggregation techniques while maintaining the detail I need. Is anybody able to help? Thanks
It's better if you tell us what have you tried ...
SELECT acode,bcode,count(bcode)
FROM mydataset
GROUP BY acode,bcode
and if you want only the ones that have more than 1
SELECT acode,bcode,count(bcode) as num
FROM mydataset
GROUP BY acode,bcode
HAVING num>1
SELECT DISTINCT x.*
FROM my_table x
JOIN my_table y
ON y.acode = x.acode
AND y.bcode <> x.bcode;
This will work for you
SELECT main.bcode, main.acode FROM mydataset main
INNER JOIN (SELECT acode, count(DISTINCT bcode) AS cnt FROM mydataset GROUP BY acode HAVING cnt>1) AS sub
ON main.acode=sub.acode
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.