将行转换为列 - 面临的问题。

时间:2012-06-26 15:00:49

标签: mysql

我有以下数据集(请注意,我的数据库/模式有点复杂,所以在此处包含所有细节是不可行的)

这是我的数据的简化版本,我从基表中提取并创建了view

workout_activity_record_id  attribute_metric_id attribute_metric_value  workout_exercise_set_created_on
234                                17              10                          2012-02-06 00:00:00
234                                18              30                          2012-02-06 00:00:00
234                                17              20                          2012-03-03 00:00:00
234                                18              12.9                        2012-03-03 00:00:00
234                                17              20                          2012-04-02 00:00:00
234                                18              40                          2012-04-02 00:00:00
.
.
. (So on for further workout_activity_record_ids)

我需要计算(查看数据以便更好地理解)

Grand Total = (10 * 30) + (20 * 12.9) + (20 * 40)
按月分组。

这里有助于乘法的基本方法是将行转换为列。所以上面的结构会变成 -

workout_activity_record_id     ex17  ex18       workout_exercise_set_created_on     
234                            10     30        2012-02-06 00:00:00
234                            20     12.9      2012-03-03 00:00:00
234                            20     40        2012-04-02 00:00:00
.
.
.
.
.  (And so on for remaining workout_activity_record_ids)

为此,我经历了几个SO帖子,在尝试了各种可行/不可行(:D)选项后,我想出了以下查询。

SELECT
       CASE attribute_metric_id
           WHEN '17' THEN attribute_metric_value END AS 'ex17',
       CASE attribute_metric_id WHEN '18' THEN attribute_metric_value END AS 'ex18', 
       workout_exercise_set_created_on
FROM exercise_attribute
WHERE workout_activity_record_id =234

我得到的实际输出是

workout_activity_record_id  ex17    ex18    workout_exercise_set_created_on 
234                          10      NULL   2012-02-06 00:00:00
234                          NULL    30     2012-02-06 00:00:00
234                          20      NULL   2012-03-03 00:00:00
234                          NULL    12.9   2012-03-03 00:00:00
234                          20      NULL   2012-04-06 00:00:00
234                          NULL    40     2012-04-02 00:00:00

任何人都可以对此有所了解吗?我将不胜感激。谢谢!

2 个答案:

答案 0 :(得分:3)

这会计算您的总计,按年和月分组:

SELECT YEAR(workout_exercise_set_created_on) AS YEAR,
       MONTH(workout_exercise_set_created_on) AS MONTH,
       sum(SubTotal) AS GrandTotal
FROM
  (SELECT workout_exercise_set_created_on,
          max(CASE WHEN attribute_metric_id = 17 THEN attribute_metric_value END) * max(CASE WHEN attribute_metric_id = 18 THEN attribute_metric_value END) AS SubTotal
   FROM MyTable
   GROUP BY workout_exercise_set_created_on) a
GROUP BY YEAR(workout_exercise_set_created_on),
         MONTH(workout_exercise_set_created_on)

SQL Fiddle Example

<强>输出

YEAR    MONTH   GRANDTOTAL
2012    2        300
2012    3        258
2012    4        800

答案 1 :(得分:1)

只需添加聚合函数:

SELECT
   MAX(CASE attribute_metric_id
        WHEN '17' THEN attribute_metric_value 
      END) AS 'ex17',
   MAX(CASE attribute_metric_id 
        WHEN '18' THEN attribute_metric_value 
       END) AS 'ex18', 
   workout_exercise_set_created_on
   FROM exercise_attribute
   WHERE workout_activity_record_id =234
   GROUP BY workout_exercise_set_created_on

如果您需要显示不同workout_activity_record_id的结果,则应修改group by workout_activity_record_id,workout_exercise_set_created_on