我碰巧拥有带有一组通用ID的动态数据。因此,我决定为其创建数据透视表。假设我在创建数据透视表之前先输入了以下数据。
part_id data_element value
------------------------------------------------
part1 part_name Motor
part1 operator_id Kishore
part1 shift_id B
part1 production_count 20
part2 part_name LVDT
part2 operator_id Kumar
part2 shift_id A
part2 production_count 3
如上表所示,对于 part1 ,我有不同的 data_elements 及其各自的值。而且我必须将 varchar 用作value列的通用数据类型,因为value可以是任何东西。基于此,我创建了以下数据透视表。
part_id part_name operator_id shift_id production_count
-----------------------------------------------------------
part1 Motor Kishore B 20
part2 LVDT Kumar A 3
请找到我用来实现上述数据透视表的数据透视查询
CREATE VIEW table_view_name AS (
SELECT
`table_name`.`part_id`,
`table_name`.`data_element`,
`table_name`.`value`,
(CASE WHEN (`table_name`.`data_element` = 'part_name') THEN
`table_name`.`value` END) AS `part_name`,
(CASE WHEN (`table_name`.`data_element` = 'operator_id') THEN
`table_name`.`value` END) AS `operator_id`,
(CASE WHEN (`table_name`.`data_element` = 'shift_id') THEN
`table_name`.`value` END) AS `shift_id`,
(CASE WHEN (`table_name`.`data_element` = 'production_count') THEN
`table_name`.`value` END) AS `production_count`
FROM `table_name`)
还有
CREATE VIEW `table_extended_view` AS (
SELECT
`table_view_name`.`part_id`,
MAX(`table_view_name`.`part_name`) AS `part_name`,
MAX(`table_view_name`.`operator_id`) AS `operator_id`,
MAX(`table_view_name`.`shift_id`) AS `shift_id`,
MAX(`table_view_name`.`production_count`) AS `production_count`
FROM `table_view_name`
GROUP BY `table_view_name`.`part_id`)
但是在这里,尽管所有列及其值均已隔离,但每列的数据类型仅是varchar。因此,这里我唯一的问题是,是否可以更改数据透视表中列的数据类型?如果是这样,怎么可能?