Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用MySQL进行数据透视?_Mysql_Pivot - Fatal编程技术网

如何使用MySQL进行数据透视?

如何使用MySQL进行数据透视?,mysql,pivot,Mysql,Pivot,我有一个MySQL表: 我希望输出类似下面的内容(类似于旋转),Pen Pencil和Glue列中的值必须从最近的时间戳填充 您可以通过子查询的轴心来获得结果,该子查询标识每个类别-产品组的最新记录 SELECT t1.Category, MAX(CASE WHEN t1.Product = 'pen' THEN CONCAT(t1.Flag, '(productid-', t1.Product_ID, ')') ELSE NULL END) AS Pen,

我有一个MySQL表:

我希望输出类似下面的内容(类似于旋转),Pen Pencil和Glue列中的值必须从最近的时间戳填充


您可以通过子查询的轴心来获得结果,该子查询标识每个
类别
-
产品
组的最新记录

SELECT t1.Category,
       MAX(CASE WHEN t1.Product = 'pen'    THEN CONCAT(t1.Flag, '(productid-', t1.Product_ID, ')') ELSE NULL END) AS Pen,
       MAX(CASE WHEN t1.Product = 'pencil' THEN CONCAT(t1.Flag, '(productid-', t1.Product_ID, ')') ELSE NULL END) AS Pencil,
       MAX(CASE WHEN t1.Product = 'glue'   THEN CONCAT(t1.Flag, '(productid-', t1.Product_ID, ')') ELSE NULL END) AS Glue
FROM yourTable t1
INNER JOIN
(
    SELECT Category, Product, MAX(timestamp) AS timestamp
    FROM yourTable
    GROUP BY Category, Product
) t2
    ON t1.Category  = t2.Category AND
       t1.Product   = t2.Product AND
       t1.timestamp = t2.timestamp
GROUP BY t1.Category
按照下面的链接进行运行演示:


您可以通过子查询的轴心来获得结果,该子查询标识每个
类别
-
产品
组的最新记录

SELECT t1.Category,
       MAX(CASE WHEN t1.Product = 'pen'    THEN CONCAT(t1.Flag, '(productid-', t1.Product_ID, ')') ELSE NULL END) AS Pen,
       MAX(CASE WHEN t1.Product = 'pencil' THEN CONCAT(t1.Flag, '(productid-', t1.Product_ID, ')') ELSE NULL END) AS Pencil,
       MAX(CASE WHEN t1.Product = 'glue'   THEN CONCAT(t1.Flag, '(productid-', t1.Product_ID, ')') ELSE NULL END) AS Glue
FROM yourTable t1
INNER JOIN
(
    SELECT Category, Product, MAX(timestamp) AS timestamp
    FROM yourTable
    GROUP BY Category, Product
) t2
    ON t1.Category  = t2.Category AND
       t1.Product   = t2.Product AND
       t1.timestamp = t2.timestamp
GROUP BY t1.Category
按照下面的链接进行运行演示:

试试这个:

select
    Category,
    max(case when Product = 'pen' then concat(`Flag`, '(productid-', Product_ID, ')') else null end) as `Pen`,
    max(case when Product = 'pencil' then concat(`Flag`, '(productid-', Product_ID, ')') else null end) as `Pencil`,
    max(case when Product = 'glue' then concat(`Flag`, '(productid-', Product_ID, ')') else null end) as `Glue`
from (
    select t1.*
    from yourtable t1
    join (
        select Product, Category, max(`timestamp`) as `timestamp`
        from yourtable
        group by Product, Category
    ) t2 on t1.Product  = t2.Product and t1.`timestamp` = t2.`timestamp` and t1.Category = t2.Category
) t
group by Category
试试这个:

select
    Category,
    max(case when Product = 'pen' then concat(`Flag`, '(productid-', Product_ID, ')') else null end) as `Pen`,
    max(case when Product = 'pencil' then concat(`Flag`, '(productid-', Product_ID, ')') else null end) as `Pencil`,
    max(case when Product = 'glue' then concat(`Flag`, '(productid-', Product_ID, ')') else null end) as `Glue`
from (
    select t1.*
    from yourtable t1
    join (
        select Product, Category, max(`timestamp`) as `timestamp`
        from yourtable
        group by Product, Category
    ) t2 on t1.Product  = t2.Product and t1.`timestamp` = t2.`timestamp` and t1.Category = t2.Category
) t
group by Category

显示您尝试此查询的内容:从product_表中选择category、max(如果(
product
='pen',flag,null))作为'pen',max(如果(
product
='pen',flag,null))作为'Glue',这是给我的输出,但是,我不知道如何检索最新时间戳的标志值显示您尝试过的查询:选择类别,max(如果(
product
='pen',flag,null))为'pen',max(如果(
product
='pen',flag,null))为'Pencil',max(如果(
product
='pen',flag,null))作为Product_表中的“Glue”,这给了我输出,但我不知道如何检索最新时间的标志值谢谢Tim…这对我很有效。你节省了我的时间谢谢Tim…这对我很有效。你节省了我的时间谢谢你的快速回复,这对我也很有效。非常感谢:)谢谢你的快速回复,这对我来说也很有效。非常感谢:)