简体   繁体   中英

Mysql Limit By sum of count

I am having 3 column field in mysql table id, status and count i want to get group of status and sum of count with distinct id value example is given below

id - status - count

1 - test1   -   1
1 - test1   -   1
1 - test2   -   1
1 - zer1   -    0
2 - exam1   -   1
2 - exam1   -   1
2 - zer2   -    0
2 - exam2   -   1
3 - mit1    -   1
3 - mit2    -   1
3 - zer3   -    0
4 - term1   -   1

i want the result like the following

id - status - count
1 - test1   -   2
2 - exam1   -   2
3 - mit1    -   1
4 - term1   -   1

i am tried the following query for the result but i didn't get the result

SELECT id,status,sum(count) as count FROM `test` where count=1 group by status order by id

for the above query i am getting the following result

id - status - count
 1  test1   2
 2  exam1   2
 2  exam2   1
 3  mit1    1
 3  mit2    1
 4  term1   1

i am unable to find the right query to remove the duplicate id's and order by sum of count.Please guide me to get the result.

You can use multiple query here like:

    Select distinct id from 
    (SELECT id,status,
   sum(count) as count 
   FROM `test` where count=1 group by status order by id 
    )

This is a tricky one because of two entries with id 3 having the same count . However I think this query will do what you want:

SELECT c1.id, MIN(c1.status) AS status, c1.sum_c
FROM (SELECT id, status, SUM(`count`) AS sum_c 
      FROM `counts` 
      GROUP BY id, status
     ) c1
JOIN (SELECT id, MAX(sum_c) AS max_c
      FROM (SELECT id, status, SUM(`count`) AS sum_c 
            FROM `counts` 
            GROUP BY id, status
           ) c1
      GROUP BY id) c2
ON c2.id = c1.id AND c2.max_c = c1.sum_c
GROUP BY c1.id

Output:

id  status  sum_c
1   test1   2
2   exam1   2
3   mit1    1
4   term1   1

Try below using subquery:

   select t2.id, b.status, t2.scount 
from 
(SELECT id,max(scount) as scount 
FROM
(
   SELECT id,status,sum(count) as scount 
     FROM t where count=1
     group by status,id
)a group by id) t2
inner join 
(
    SELECT id,status,sum(count) as scount 
     FROM t where count=1
     group by status,id

)b on t2.id =b.id and t2.scount=b.scount

use co-related sub-query

select min(status) status,id,s as count from
(
select * from 
(
select id,status,sum(cnt) s from t 
 where cnt>0
 group by id,status
) as  t1 where t1.s in( select max(s) from 
                          (select status,id,sum(cnt) s from t
                           where cnt>0
                            group by
                          status,id) t2 where t1.id=t2.id
                           group by t2.id
                       )
                       ) as t3 group by id,s

https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=f15f06ce7e6967e1f6c1d21c830b3985

status  id  count
test1   1   2
exam1   2   2
mit1    3   1
term1   4   1

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM