doubt with select sum() Mysql

Asked

Viewed 138 times

-1

I have these two tables, January and February:

inserir a descrição da imagem aqui

each field corresponds to the day of the month, D1= day 01, etc. I need to create a select that shows the monthly value of each account plan, example:

inserir a descrição da imagem aqui

how would this sql ?

2 answers

4

Try using the following code:

SELECT
  SUM(d1 + d2 + d3 + ... + d12) as soma
FROM
  janeiro
  • Dan, I thought about doing this, but there will be 12 tables (one for each month), I wanted to know if I would have a better sql. And how would group the plans of accounts?

  • Unfortunately the way you modeled the bank may be the only way (in my logic), to group by plane, you must perform a select by pulling the plane and performing a group by. It would be more interesting to create a single table, containing the columns: id, value, month. There you have only one table, it would be the most correct form in this example.

  • Bad models generate bad and unnecessarily complicated sqls.

2


Your model is bad make one of the kind

Table

Date Valor

With date functions it becomes easy to total by month , year, week etc.

Browser other questions tagged

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