Solved

MySQL Group By Outputs List

Posted on 2011-03-01
4
1,201 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 250 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 250 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

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

691 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