1

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'?

1

3 Answers 3

2

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
Sign up to request clarification or add additional context in comments.

2 Comments

I think you meant count(t.id). count(*) will include rows with no entry in table1.
Perfect!!! It's working as I wanted... Thanks for quick help... I spent whole day trying to get a solution like this...
0

try

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

Comments

0

You need to force the month and VERDICT_id fields when the record does not exist. (The month is forced with the DISTINCT LIST. The VERDICT_id is forced with a JOIN without conditions to Table2)

SELECT LIST.`month` as `month`, Table2.id AS VERDICT_id, COUNT(Detail.id)
FROM 
  (SELECT DISTINCT `month`
  FROM Table1
  WHERE Table1.`month` = 201307) AS LIST
JOIN Table2
LEFT JOIN Table1 AS Detail
  ON LIST.`month` = Detail.`month`
    AND Table2.id = Detail.VERDICT_id
GROUP BY `month`, VERDICT_id
ORDER BY `month`, VERDICT_id;

1 Comment

The solution works but not consistent. For some of the months none of the row is updated with VERDICT_id and it's set as NULL. In that case this query prints only VERDICT_id as "1" and COUNT(*) as "0".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.