MySQL query with COUNT and join column from another table

himansusn

I have 2 tables with multiple fields.

Table1:

+--------+---+---------------+                                                  
| month  | id| VERDICT_id    |                                                  
+--------+------+------------+                                                  
| 201307 | 1 |             1 |                                                  
| 201307 | 2 |             4 |                                                  
| 201307 | 3 |             2 |                                                  
| 201307 | 4 |             2 |                                                  
| 201307 | 5 |          NULL |                                                  
| 201307 | 6 |             1 |                                                  
+--------+------+------------+

Like this for every new 'month', for each unique 'id' the 'VERDICT_ID' is set according to the value in Table2.

Table2:

+----+----------------------------------+
| id | title                            |
+----+----------------------------------+
|  1 | Passed                           |
|  2 | Failed (Component Fault)         |
|  3 | Failed (User Fault)              |
|  4 | Failed (Hardware Issue)          |
+----+----------------------------------+

I make a query which gives below output

Actual Output:

+--------+------------+----------+
| month  | VERDICT_id | COUNT(*) |
+--------+------------+----------+
| 201307 |          1 |        2 |
| 201307 |          2 |        2 |
| 201307 |          4 |        1 |
+--------+------------+----------+

What I want is,

+--------+------------+----------+
| month  | VERDICT_id | COUNT(*) |
+--------+------------+----------+
| 201307 |          1 |        2 |
| 201307 |          2 |        2 |
| 201307 |          3 |        0 |
| 201307 |          4 |        1 |
+--------+------------+----------+

The difference between these 2 output is, if any VERDICT_id doesn't exist for a month, then I want to print the VERDICT_id and COUNT as '0'.

But with the below query it's not possible.

select month,VERDICT_id, COUNT(*) FROM Table1 
where (month = 201307) AND (VERDICT_id BETWEEN 1 AND 4) GROUP BY month, VERDICT_id;

Q. Is this possible to make query to print the non existing VERDICT_id and COUNT as '0'?

rs.

try this

SELECT v.*, count(t.verdict_id) as cnt FROM 
(
      SELECT month, id as verdict_id 
      from 
          (SELECT DISTINCT month FROM Table1 where (month = 201307)) M , 
          verdictTable
) v
LEFT OUTER JOIN Table1 t ON v.verdict_id = t.verdict_id and v.month = t.month
GROUP BY v.verdict_id, v.month

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Join a columns count from another table

From Dev

SQL query with count() from another table

From Dev

Adding a count from another table to existing query

From Dev

MYSQL Join multiple column from same table

From Dev

join count from one table to select from another - mysql

From Dev

MySQL Join and COUNT(*) query

From Dev

How I get common values from two query in a table and join it with another table in MySql?

From Dev

MySQL query to get count of column name from a table of a database?

From Dev

Mysql Query with LEFT JOIN and having count of OrderID from another table... Stuck

From Dev

Mysql query to get rows of a table as columns of another, with column names from third table

From Dev

MYSQL join one colum from one table to two count() in another

From Dev

MySQL query to sum one column and count another column, from two tables, based on a common value?

From Dev

MySQL set column value from another table (JOIN 3 tables)

From Dev

Query within another query to count amount of items from another table

From Dev

SQL - Getting a column from another table to join this query

From Dev

MYSQL Join multiple column from same table

From Dev

How to join Count(*) columns with another column in Mysql

From Dev

MySQL join / sum value from one table colum with a count values from another

From Dev

MySQL - Join & Count rows from another table

From Dev

Column loop and update another column with COUNT() value from another table

From Dev

MySql get count(*) with a join from a table

From Dev

Mysql query with count on another table

From Dev

Update value in column based on column count in another table with mysql

From Dev

MySQL Query Count and Join

From Dev

MySQL query - select from one, count from another table

From Dev

JOIN query to retrieve multiple column referencing to a single column from another table

From Dev

MYSQL select from table and count from another

From Dev

MySQL CASE with COUNT query and adding another column

From Dev

Join in MYSQL (Count from One table and list from one table)

Related Related

  1. 1

    Join a columns count from another table

  2. 2

    SQL query with count() from another table

  3. 3

    Adding a count from another table to existing query

  4. 4

    MYSQL Join multiple column from same table

  5. 5

    join count from one table to select from another - mysql

  6. 6

    MySQL Join and COUNT(*) query

  7. 7

    How I get common values from two query in a table and join it with another table in MySql?

  8. 8

    MySQL query to get count of column name from a table of a database?

  9. 9

    Mysql Query with LEFT JOIN and having count of OrderID from another table... Stuck

  10. 10

    Mysql query to get rows of a table as columns of another, with column names from third table

  11. 11

    MYSQL join one colum from one table to two count() in another

  12. 12

    MySQL query to sum one column and count another column, from two tables, based on a common value?

  13. 13

    MySQL set column value from another table (JOIN 3 tables)

  14. 14

    Query within another query to count amount of items from another table

  15. 15

    SQL - Getting a column from another table to join this query

  16. 16

    MYSQL Join multiple column from same table

  17. 17

    How to join Count(*) columns with another column in Mysql

  18. 18

    MySQL join / sum value from one table colum with a count values from another

  19. 19

    MySQL - Join & Count rows from another table

  20. 20

    Column loop and update another column with COUNT() value from another table

  21. 21

    MySql get count(*) with a join from a table

  22. 22

    Mysql query with count on another table

  23. 23

    Update value in column based on column count in another table with mysql

  24. 24

    MySQL Query Count and Join

  25. 25

    MySQL query - select from one, count from another table

  26. 26

    JOIN query to retrieve multiple column referencing to a single column from another table

  27. 27

    MYSQL select from table and count from another

  28. 28

    MySQL CASE with COUNT query and adding another column

  29. 29

    Join in MYSQL (Count from One table and list from one table)

HotTag

Archive