Solved

MySQL Group By Outputs List

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

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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Update Query 23 93
generate a dynamic mysql query 2 41
[MYSQL]: Delete is very slow 4 52
selecting date modified field from a table 2 39
Introduction In this article, I will by showing a nice little trick for MySQL similar to that of my previous EE Article for SQLite (http://www.sqlite.org/), A SQLite Tidbit: Quick Numbers Table Generation (http://www.experts-exchange.com/A_3570.htm…
As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…
A short film showing how OnPage and Connectwise integration works.

932 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now