3

I want to convert multiple rows to a single row, based on week. It should look like the following. Can any one help me?

id      |   Weight   |  Created   |
 1      |    120     | 02-04-2012 |
 2      |    110     | 09-04-2012 |
 1      |    100     | 16-04-2012 |
 1      |    130     | 23-04-2012 |
 2      |    140     | 30-04-2012 |
 3      |    150     | 07-05-2012 |

Result should look like this:

id      |   Weight_week1  | Weight_week2  |  weight_week3  | weight_week4  |
 1      |     120         |     100       |      130       |               |
 2      |     110         |     140       |                |               |
 3      |     150         |               |                |               |

Thanks in advance.

3
  • 2
    How to define Week 1, Week 2 and so on ? Commented May 7, 2012 at 11:01
  • one query cannot complete your wish. You have to use multiple queries for your task. Commented May 7, 2012 at 11:02
  • possible duplicate of MYSQL - Rows to Columns Commented May 7, 2012 at 12:05

4 Answers 4

1

if this a single table then

SELECT GROUP_CONCAT(weight) as Weight,
        WEEK(Created) as Week
Group by Week(Created)

This will give you a row each having week id and comma seperated whights

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

Comments

1

You could do it like this:

SELECT
    t.id,
    SUM(CASE WHEN WeekNbr=1 THEN Table1.Weight ELSE 0 END) AS Weight_week1,
    SUM(CASE WHEN WeekNbr=2 THEN Table1.Weight ELSE 0 END) AS Weight_week2,
    SUM(CASE WHEN WeekNbr=3 THEN Table1.Weight ELSE 0 END) AS Weight_week3,
    SUM(CASE WHEN WeekNbr=4 THEN Table1.Weight ELSE 0 END) AS Weight_week4
FROM
    (
    SELECT  
        (
           WEEK(Created, 5) - 
           WEEK(DATE_SUB(Created, INTERVAL DAYOFMONTH(Created) - 1 DAY), 5) + 1 
        )as WeekNbr,
        Table1.id,
        Table1.Weight,
        Table1.Created
    FROM
        Table1
    ) AS t
GROUP BY
    t.id

I don't know if you want a AVG,SUM,MAX or MIN but you can change the aggregate to what you want.

Useful references:

Comments

0

you cannot create fields on the fly like that but you can group them.

use GROUP_CONCAT to deliver results with a delimiter that you can separate on later.

1 Comment

Maybe the OP wants some other form of aggregation? SUM? AVG? Question is ambiguous.
0

You could also do this:

SELECT id, created, weight, (
    SELECT MIN( created ) FROM weights WHERE w.id = weights.id
) AS `min` , round( DATEDIFF( created, (
   SELECT MIN( created )
FROM weights
WHERE w.id = weights.id ) ) /7) AS diff
FROM weights AS w
ORDER BY id, diff

This code does not do pivot table. You should add some additional code to convert the data to your needs. You may run into trouble if you use WEEK() because of the years.

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.