sql - MySQL Query help - Grouping and count problems -
i have mysql database trying specific count of data catch. best way explain showing you.
i have table has username , id_no, id not going unique there circumstances show duplication of (which expected), table might this
username id_no user1 1111111 user1 1111111 user1 1111111 user1 1111111 user1 2222222 user1 3333333 user1 3333333 user1 444444 user1 444444 user1 444444 user1 555555 user1 666666
i need unique count of piece of cake
select username, count(distinct id_no) cases group username
that gives me count of 6. gets interesting need join table add category table might
username cat id_no user1 category1 1111111 user1 category2 1111111 user1 category2 1111111 user1 category2 1111111 user1 category5 2222222 user1 category6 3333333 user1 category7 3333333 user1 category8 444444 user1 category8 444444 user1 category8 444444 user1 category11 555555 user1 category12 666666
with need unquie count, catch here need match same number before (6), stuck how can in mysql because can see there categories overlap query simple looks this
select username, cat, count(distinct id_no) cases c join case_details cd on c.id_no=cd.id_no group username, cat
because having group cat count of 8. if sql server inner query using top 1 on id (i think), mysql not let me use limit within subquery.
so clear need this
username cat id_no user1 category1 1111111 user1 category5 2222222 user1 category6 3333333 user1 category8 444444 user1 category11 555555 user1 category12 666666
you notice first category had id shown. total count 6 same first, looking for.
anyone got ideas here?
Comments
Post a Comment