0

I have a table as

mysql> select * FROM testa;
+---------+-------+
| month_x | money |
+---------+-------+
| 11101   | 12345 |
| 11105   |   100 |
| 11105   |   100 |
| 11105   |   100 |
| 11105   |   100 |
| 11106   | 12345 |
+---------+-------+
6 rows in set (0.00 sec)

where last two digits in the month_x are months now i want my output as

Month   TOTAL
01  12345
02  0
03  0
04  0
05  400
06  12345
07  0
08  0
09  0
10  0
11  0
12  0

IS possible using the If else or case.

0

3 Answers 3

2

You can use modular arithmetic to obtain the trailing two digits (they're the remainder when the number is divided by 100), then assuming you wish to sum money when your data is "grouped by" month:

SELECT month_x % 100 AS Month, SUM(money) AS TOTAL
FROM testa
GROUP BY Month
ORDER BY Month ASC;

Alternatively, you could use rely on MySQL's implicit type conversion and use its string functions:

SELECT RIGHT(month_x, 2) AS Month, SUM(money) AS TOTAL
FROM testa
GROUP BY Month
ORDER BY Month ASC;

UPDATE

As @shiplu.mokadd.im states, to show every month (even those for which you have no data), you need to obtain numbers 1 through 12 from a temporary table. However, you can create such a temporary table in your query using UNION:

      SELECT 1
UNION SELECT 2
UNION SELECT 3 -- etc

Therefore:

SELECT Month, Sum(money) AS TOTAL
FROM   testa
  RIGHT JOIN (
        SELECT  1 AS Month
  UNION SELECT  2 UNION SELECT  3 UNION SELECT  4 UNION SELECT  5 UNION SELECT  6
  UNION SELECT  7 UNION SELECT  8 UNION SELECT  9 UNION SELECT 10 UNION SELECT 11
  UNION SELECT 12
  ) months ON testa.month_x % 100 = months.Month
GROUP BY Month; 

HOWEVER I would note that usually one doesn't usually do this in the database, as it really belongs in the presentation layer: from whatever language you're accessing the database, you'd loop over 1...12 and assume TOTAL to be 0 if there's no corresponding record in the resultset.

Sign up to request clarification or add additional context in comments.

3 Comments

It is ok.but i want if the month is not present as in my actual table the 01,05,06 are present but there are twelve months in year so if that is not in the base table it should be in output with sum as 0.As i have given the sample output pattern
@AbdulManaf It can only be done by using a temporary table. See my answer.
@AbdulManaf: See alternative in my revised answer above.
1

For this you need to create a table first with months' numeric value in it.

CREATE TABLE `months` (
  `mon` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
INSERT INTO `months` VALUES (1), (2), (3), (4), (5), (6), (7), (8), (9), (10), (11), (12);

Then execute this query,

SELECT m.mon, 
       IF(Sum(t.money) IS NULL, 0, Sum(t.money)) AS `money` 
FROM   testa t 
       RIGHT OUTER JOIN months m 
         ON ( t.month_x%100 = m.mon ) 
GROUP  BY m.mon; 

Result is,

+------+-------+
| mon  | money |
+------+-------+
|    1 | 12345 |
|    2 |     0 |
|    3 |     0 |
|    4 |     0 |
|    5 |   400 |
|    6 | 12345 |
|    7 |     0 |
|    8 |     0 |
|    9 |     0 |
|   10 |     0 |
|   11 |     0 |
|   12 |     0 |
+------+-------+

2 Comments

Is it not possible to it with that single table..??
No. That's not possible. You can put the whole thing in a procedure however. But creating temporary table every-time for such query will decrease the performance. That's why its better to have such permanent table
0

You can use IF statements - yes. Look @ this: http://dev.mysql.com/doc/refman/5.5/en/if-statement.html

Comments

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.