Complete number of query records

Asked

Viewed 39 times

0

Hello! I have a table in BD that can contain from 0 there are 12 records. (as a monthly income chart during the year. totaling 12 records)

I want to make a query in this table and I want it to return me exactly 12 records of the query.

Example: the table has only 3 records:

month 01 | 35,00

month 02 | 25,00

month 03 | 98,00

The query should return me these 3 existing months and complete the registration number with the 9 missing months.

month 01 | 35,00

month 02 | 25,00

month 03 | 98,00

  • |

  • |

  • |

  • |

  • |

  • |

  • |

  • |

  • 1

    Hello, you can add the code of what you have tried?

  • I did no code. this all has to be done only in a select in the database

2 answers

0


The solution is to consult a set of pre-defined records with the months, then join the table of rents:

SELECT      M.Nome
        ,   SUM(IFNULL(Renda, 0)) AS Renda
FROM        (
                SELECT "Mes 01" AS Nome, 1 AS Mes
                UNION ALL
                SELECT "Mes 02" AS Nome, 2 AS Mes
                UNION ALL
                SELECT "Mes 03" AS Nome, 3 AS Mes
                UNION ALL
                SELECT "Mes 04" AS Nome, 4 AS Mes
                UNION ALL
                SELECT "Mes 05" AS Nome, 5 AS Mes
                UNION ALL
                SELECT "Mes 06" AS Nome, 6 AS Mes
                UNION ALL
                SELECT "Mes 07" AS Nome, 7 AS Mes
                UNION ALL
                SELECT "Mes 08" AS Nome, 8 AS Mes
                UNION ALL
                SELECT "Mes 09" AS Nome, 9 AS Mes
                UNION ALL
                SELECT "Mes 10" AS Nome, 10 AS Mes
                UNION ALL
                SELECT "Mes 11" AS Nome, 11 AS Mes
                UNION ALL
                SELECT "Mes 12" AS Nome, 12 AS Mes
          ) M
LEFT JOIN   Rendas R ON R.Mes = M.Mes
GROUP BY    M.Nome

You can see a functional example in DB Fiddle: https://www.db-fiddle.com/f/t7feMk8o95L83WjEq3w7EP/0

  • Cool, I’ll try this and I’ll get back to you

0

You can use the case when in Select for that. Follow an example of use:

SELECT 
   CASE WHEN valor = 0 THEN "-"
   ELSE mes
END as mes,
   CASE WHEN valor = 0 THEN " "
   ELSE valor
END AS valor
FROM tabela;

You are saying the following: when the "value" column is reset, the month will display a dash, and the value will be a blank.

  • no. the table has only 3 records. 9 records are missing. If I make a select in the table, this select will return me only 3 records. but I need you to return 12 records

  • a CASE WHEN will not work because there are these 9 records in the table.

  • If you are using the data to be displayed in another language (php, for example), within the query loop you can complete the information until you give 12 results. What I would do, if I didn’t use another language to receive the data, would be to fill all 12 records in the database with zero values so that my Select can be used.

  • I understood your answer perfectly, it would be a great solution. only that in my case I have no way to make any

Browser other questions tagged

You are not signed in. Login or sign up in order to post.