?
Solved

MySQL Group By Outputs List

Posted on 2011-03-01
4
Medium Priority
?
1,202 Views
Last Modified: 2012-05-11
I have a table that looks like this:

+----id-----+------mid------+-----sid------+------file-----+----encode-----+
        1                 A                                       file1                0
        2                 A                                       file2                1
        3                 A                                       file3                0
        4                                      B                  file4                0
        5                                      B                  file5                0


I'm trying to create a query that will list the number of files that have an encode value of 0, grouped by the mid or sid.  So, one query would give me this result


+------mid------+-----sid------+------files-----+
            A                                         2              
                                B                     2              

I don't know what the value the 'mid' or 'sid' columns will have.  I just want a count, grouped by the files' sid or mid values.

Not exactly sure how to do this.

Thanks,
marc
0
Comment
Question by:marcparillo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 24

Expert Comment

by:jimyX
ID: 35013882
This one will do:
select mid, sid, count(encode) files from Yourtable group by mid, sid, encode having encode = 0;
0
 
LVL 3

Assisted Solution

by:dtocci
dtocci earned 1000 total points
ID: 35014163
I wouldn't group by "encode" at all and I'd use a where clause first.  I think this will work:

SELECT mid, sid, count(encode) AS files FROM Yourtable WHERE encode = 0 GROUP BY mid, sid;
0
 
LVL 22

Accepted Solution

by:
Thomasian earned 1000 total points
ID: 35014166
If you want to return files=0:
select mid, sid, count(nullif(encode,0)) files
from tablename
group by mid, sid

Open in new window

if you don't need to return files=0:
select mid, sid, count(*) files
from tablename
where encode=0
group by mid, sid

Open in new window

0
 
LVL 41

Expert Comment

by:Sharath
ID: 35014722
If you have data like this, you will get an extra record for A,B combination with Thomasian's query. Is that what you are looking for?
mysql> select * from tab2;
+------+------+------+-------+--------+
| id   | mid  | sid  | file  | encode |
+------+------+------+-------+--------+
|    1 | A    | NULL | file1 |      0 |
|    2 | A    | NULL | file2 |      1 |
|    3 | A    | NULL | file3 |      0 |
|    4 | NULL | B    | file4 |      0 |
|    5 | NULL | B    | file5 |      0 |
|    5 | A    | NULL | file6 |      0 |
|    5 | A    | B    | file7 |      0 |
+------+------+------+-------+--------+
7 rows in set (0.00 sec)

mysql> select mid, sid, count(*) files
    ->   from tab2
    ->  where encode=0
    ->  group by mid, sid;
+------+------+-------+
| mid  | sid  | files |
+------+------+-------+
| NULL | B    |     2 |
| A    | NULL |     3 |
| A    | B    |     1 |
+------+------+-------+
3 rows in set (0.00 sec)

Open in new window

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question