Domanda

I want to create an application which one is summary the values of each column. I have a table like this:

Each rows contains one goods

Date      | Company_Name | Order_cost | Weight |
2013-05-15| Dunaferr     | 310        | 1200   |
2013-05-18| Pentele      | 220        | 1600   |
2013-05-25| Dunaferr     | 310        | 1340   |

and what I exactly need is a table or view which contains the totals for the weights column for each week which is supposed to be extracted from the date column!

Something like that

company_name | week1 | week2  | week3 | week4 ...
dunaferr     | 35000 | 36000  | 28000 | 3411
pentele      | 34000 | 255000 | 3341  | 3433

Is there any way to do this?

È stato utile?

Soluzione

I would do this in two steps:

First step complete an sql query getting a summary with a sum for weight with a group by for yearweek

SELECT Company_Name, YEARWEEK(Date), sum(weight) FROM table GROUP BY Company_Name, YEARWEEK(Date)

http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_yearweek.

Second step would be to process this into the required format in the application year.

If you absolutely have to do this in the database, then you are looking at implementing a pivot table, which has previously been covered here: MySQL pivot table

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top