MySQL交叉表/数据透视聚合。根据其他表

时间:2016-05-03 21:29:50

标签: mysql pivot crosstab

数据

我有以下格式的一组数据:

CAR_INVENTORY TABLE
CAR_ID  MAKE_MODEL      COLOR   YEAR
1       Ford Fusion     Black   2015
2       Tesla Model S   White   2014
3       Acura ILX       Blue    2013
4       Ford Fusion     Black   2013
5       Toyota Corolla  Blue    2014
6       Ford Fusion     Blue    2013
7       Toyota Corolla  Blue    2012
8       Acura ILX       Black   2015
9       Ford Focus      Blue    2012
10      Ford Fusion     White   2013
11      Acura ILX       Black   2012
12      Toyota Corolla  Black   2015
13      Toyota Corolla  Blue    2014
14      Ford Focus      White   2015
15      Tesla Model S   Red     2015
16      Acura TLX       White   2014
17      Toyota Corolla  Blue    2014
18      Ford Focus      Black   2013

INVENTORY_LOG TABLE
LOG_ID  CAR_ID  NOTE
1       7       Issue with Fuel Guage
2       3       Sweet Ride
3       16      Zippy
4       14      Issue with transmission
5       3       Fun to Drive
6       2       *NULL*
7       8       *NULL*
8       10      Economic
9       15      WOW
10      9       Good Fuel Economy
11      16      Minor issue with Shifting
12      7       Issue with Airbag
13      17      Great Mileage
14      1       Nice Tech
15      13      *NULL*
16      11      Trunk is small
17      12      *NULL*
18      2       Very Speedy
19      7       Good Mileage
20      10      Roomy
21      4       *NULL*
22      6       Nice Tech Package
23      5       Good Economy
24      18      Cool

我知道它并没有完全正常化。我们假设我不能搞乱这些数据。

car_inventory表中每辆车的库存有一行。 inventory_log表至少包含car_inventory中列出的每辆车的一个条目,因此每辆车可能有许多日志条目。 inventory_log中的条目可以为null。

我做了什么

如果汽车的日志中包含“问题”一词,则需要将其标记为此类。我想出了那个部分:

SELECT
    ci.car_id,
    CONCAT(ci.color, " ", ci.make_model) as car,
    SUM(IF (LOWER(il.note) LIKE '%issue%', TRUE, FALSE)) AS issue
FROM car_inventory ci
    LEFT JOIN inventory_log il USING (car_id)
GROUP BY ci.car_id
ORDER BY ci.car_id;

这个孩子:

car_id  car                     issue
1       Black Ford Fusion       0
2       White Tesla Model S     0
3       Blue Acura ILX          0
4       Black Ford Fusion       0
5       Blue Toyota Corolla     0
6       Blue Ford Fusion        0
7       Blue Toyota Corolla     2
8       Black Acura ILX         0
9       Blue Ford Focus         0
10      White Ford Fusion       0
11      Black Acura ILX         0
12      Black Toyota Corolla    0
13      Blue Toyota Corolla     0
14      White Ford Focus        1
15      Red Tesla Model S       0
16      White Acura TLX         1
17      Blue Toyota Corolla     0
18      Black Ford Focus        0

对于任何有问题的汽车,这都会给出非零结果。

我需要做的下一件事就是根据他们的颜色计算所有品牌,超过一年。让我们假设我们只对黑色,白色和蓝色感兴趣,我们只有福特,讴歌,丰田和特斯拉(我知道我可以使用准备好的陈述来制作这种动态)。也有一个在袋子里:

SELECT
    CASE
        WHEN ci.make_model LIKE "Acura%" THEN "Acura"
        WHEN ci.make_model LIKE "Ford%" THEN "Ford"
        WHEN ci.make_model LIKE "Toyota%" THEN "Toyota"
        WHEN ci.make_model LIKE "Tesla%" THEN "Tesla"
    END AS Make,
    SUM(CASE WHEN ci.color = "Black" THEN 1 ELSE 0 END) as Black,
    SUM(CASE WHEN ci.color = "Blue" THEN 1 ELSE 0 END) as Blue,
    SUM(CASE WHEN ci.color = "White" THEN 1 ELSE 0 END) as White
FROM car_inventory ci
    LEFT JOIN inventory_log il USING (car_id)
WHERE 
    ci.year > 2012
GROUP BY Make
ORDER BY Make;

这给了我:

Make    Black   Blue    White
Acura   1       1       1
Ford    3       1       2
Tesla   0       0       1
Toyota  1       3       0

快速计算car_inventory表,有比2012更新的14辆车,分别为黑色,蓝色或白色。

问题

这是我遇到麻烦的地方:

我想要做的就是将两者结合起来。我需要按颜色计算所有品牌,没有问题。

这是我想要获得的结果集:

DESIRED RESULT
MAKE            Black   Blue    White   
Acura           1       1       0
Ford            3       1       1
Tesla           0       0       1
Toyota          1       2       0

删除了以下三辆汽车:

car_id  car                     issues
7       Blue Toyota Corolla     2
14      White Ford Focus        1
16      White Acura TLX         1

我尝试将AND SUM(IF (LOWER(il.note) LIKE '%issue%', TRUE, FALSE)) = 0添加到where子句中。这会导致mysql错误1111“无效使用组功能”。

我也试过HAVING SUM(IF (LOWER(il.note) LIKE '%issue%', TRUE, FALSE)) != 0。它的结果不正确,只显示了特斯拉和丰田的行。

问题

如何在MySQL中创建交叉表查询,以便不计算带有日志条目(来自inventory_log)且其中包含“问题”一词的汽车(来自car_inventory)?

2 个答案:

答案 0 :(得分:1)

SELECT
    CASE
        WHEN ci.make_model LIKE "Acura%" THEN "Acura"
        WHEN ci.make_model LIKE "Ford%" THEN "Ford"
        WHEN ci.make_model LIKE "Toyota%" THEN "Toyota"
        WHEN ci.make_model LIKE "Tesla%" THEN "Tesla"
    END AS Make,
    SUM(CASE WHEN ci.color = "Black" THEN 1 ELSE 0 END) as Black,
    SUM(CASE WHEN ci.color = "Blue" THEN 1 ELSE 0 END) as Blue,
    SUM(CASE WHEN ci.color = "White" THEN 1 ELSE 0 END) as White
FROM car_inventory ci
WHERE 
    (ci.year > 2012) and
    (ci.car_id not in (select distinct il.car_id from inventory_log il where il.note like '%issue%'))
GROUP BY Make
ORDER BY Make;

答案 1 :(得分:0)

所以我已经弄清楚如何使用SubQuery做到这一点:

SELECT
    CASE
        WHEN ci.make_model LIKE "Acura%" THEN "Acura"
        WHEN ci.make_model LIKE "Ford%" THEN "Ford"
        WHEN ci.make_model LIKE "Toyota%" THEN "Toyota"
        WHEN ci.make_model LIKE "Tesla%" THEN "Tesla"
    END AS Make,
    SUM(CASE WHEN ci.color = "Black" THEN 1 ELSE 0 END) as Black,
    SUM(CASE WHEN ci.color = "Blue" THEN 1 ELSE 0 END) as Blue,
    SUM(CASE WHEN ci.color = "White" THEN 1 ELSE 0 END) as White
FROM (

        SELECT
            ci2.car_id,
            ci2.make_model,
            ci2.color
        FROM car_inventory ci2
            LEFT JOIN inventory_log il2 USING (car_id)
        WHERE
            ci2.year > 2012
        GROUP BY ci2.car_id
        HAVING SUM(IF (LOWER(il2.note) LIKE '%issue%', TRUE, FALSE)) = 0

) as ci
GROUP BY Make
ORDER BY Make;

这给了我:

Make    Black   Blue    White
Acura   1       1       0
Ford    3       1       1
Tesla   0       0       1
Toyota  1       3       0

我不会将此标记为已接受的答案,因为我非常确定在没有子查询的情况下,这是一种更好,更高效的方法。