Separate database tables to obtain values separately

Asked

Viewed 20 times

0

I have a table with values every month of several years, I would like to separate each year with the respective index calculation for another table or make the queries in this same table, as I would separate?

anomes  indice
201201  0.48
201202  0.48
201203  0.19
201204  0.63
201205  0.39
201206  0.15
201207  0.5
201208  0.45
201209  0.62
201210  0.57
201211  0.51
201212  0.73
201301  0.78
201302  0.61
201303  0.42
  • Why do you need different tables? Wouldn’t it be enough to select each year with a simple WHERE clause? Who knows a view for each year if a different "table" is essential for each year?

1 answer

1


You don’t need another table, just do a query for it, grouping by year.

In your question does not state which bank, to demonstrate we assume that it is mysql, query would look like this:

select substring(ano,1,4) ano,
       sum(indice) soma,
       avg(indice) media
  from teste
 group by substring(ano,1,4);

As I also didn’t say how I really want the Intel, I put in the query the sum and average. The result of this query is:

| ano      | soma | media    |
| -------- | ---- | -------- |
| 2012     | 5.70 | 0.475000 |
| 2013     | 1.81 | 0.603333 |

You can see it working in www.db-fiddle.com

Of course this can be a View and treat as if it were a table, or still take that result and create a new table from with CREATE TABLE SELECT or SELECT INTO.

  • 1

    Maybe the question is a xy problem.

  • can be tbm @anonimo, I just responded by posting this solution because of this phrase that you have in the question "or make queries in this same table"

Browser other questions tagged

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