[英]Transposing rows into columns - An Issue faced.
I'm having the following data set (note that my database/schemata is somewhat complex so it's not feasible to include all the details here) 我有以下数据集(请注意,我的数据库/方案有些复杂,因此在此处包括所有详细信息是不可行的)
This is a simplified version of my data which I extracted from my base tables and created a view
. 这是我从基本表中提取并创建
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)
I need to compute (look up in the data to understand better) 我需要计算(在数据中查找以更好地理解)
Grand Total = (10 * 30) + (20 * 12.9) + (20 * 40)grouped by month.
A basic approach here that would aid in multiplying is transpose the rows into columns. 一种有助于乘法的基本方法是将行转置为列。 So the above structure would become -
因此上述结构将变为-
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
For this, I went through several SO posts and after trying out various viable/non-viable (:D) options, I came up with the following query. 为此,我经历了几篇SO文章,并尝试了各种可行/不可行(:D)选项之后,提出了以下查询。
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
And the actual output I got was 我得到的实际输出是
\nworkout_activity_record_id ex17 ex18 workout_exercise_set_created_onexecution_activity_record_id ex17 ex18 Exercise_exercise_set_created_on \n234 10 NULL 2012-02-06 00:00:00
234 10空2012-02-06 00:00:00\n234 NULL 30 2012-02-06 00:00:00
234空30 2012-02-06 00:00:00\n234 20 NULL 2012-03-03 00:00:00
234 20空2012-03-03 00:00:00\n234 NULL 12.9 2012-03-03 00:00:00
234空12.9 2012-03-03 00:00:00\n234 20 NULL 2012-04-06 00:00:00
234 20空2012-04-06 00:00:00\n234 NULL 40 2012-04-02 00:00:00
234空40 2012-04-02 00:00:00\n
Can anyone shed some light over this? 任何人都可以对此有所了解吗? I'd be grateful.
我将不胜感激。 Thanks!
谢谢!
This computes the grand total for you, grouped by year and month: 这将为您计算总计,按年份和月份分组:
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)
Output 产量
YEAR MONTH GRANDTOTAL
2012 2 300
2012 3 258
2012 4 800
Just add aggregate function : 只需添加聚合函数:
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
If you need to show results for different workout_activity_record_id
you should modify group by workout_activity_record_id,workout_exercise_set_created_on
如果您需要显示不同的
workout_activity_record_id
结果, workout_activity_record_id
应按group by workout_activity_record_id,workout_exercise_set_created_on
修改group by workout_activity_record_id,workout_exercise_set_created_on
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.