MySQL代码显示基于另一个...的特定行?

daz*_*ays 0 mysql group-by

我很难弄清楚如何做到这一点,我无法很好地解释它。由于我不知道如何解释,我似乎无法通过简单的研究在网上找到答案。所以,我必须把它画出来并寻求帮助。以下是详细信息:

示例列:我的原始 SQL 查询创建了此表(特定于此问题的数据来自一个巨大的表)

示例代码:

SELECT 
inventory .01 as  `Item#`, 
inventory .02 as `warehouse`, 
inventory .03 as `qty`, 
inventory .04 as `price`, 
inventory .05 as `weight`, 
inventory .06 as `other`
FROM
Inventory
WHERE
Inventory.other = 5
Run Code Online (Sandbox Code Playgroud)

返回这个:

Item# | warehouse | qty | price | weight | other
1           1        1      3        4       5
1           2        6      3        4       5
1           3        3      3        4       5
Run Code Online (Sandbox Code Playgroud)

我使用 group by 重写了表,使数据看起来像这样:

SELECT 
inventory .01 as  `Item#`, 
inventory .03 as `qty (whse1)`, 
inventory .03 as `qty (whse2)`, 
inventory .03 as `qty (whse3)`, 
inventory .04 as `price`, 
inventory .05 as `weight`, 
inventory .06 as `other`
FROM
Inventory
WHERE
Inventory.other = 5
GROUP BY
inventory .01
Run Code Online (Sandbox Code Playgroud)

返回此:

Item# | qty (whse1)| qty (whse2) | qty (whse3) | price | weight | other
  1         1            1             1           3        4       5
Run Code Online (Sandbox Code Playgroud)

这就是我被困的地方。我不知道如何告诉编写 SQL 代码使 3 个仓库字段显示如下:

Item# | qty (whse1)| qty (whse2) | qty (whse3) | price | weight | other
  1         1            6             3           3        4       5
Run Code Online (Sandbox Code Playgroud)

我相信这很容易实现。我就是想不通。

我的数据库包含 30K 行,每个项目包含 10 次(每个仓库一次)。我希望能够根据项目编号查看 3K 行,以及每个仓库的数量。这只是数据的一个样本,真实的表和字段都有明确的标签。这只是我正在处理的问题的一个示例。

所有物品的价格、重量和其他都是一样的。这些部件唯一真正的区别是每个仓库的数量。


添加以给出真实的代码示例。到目前为止,我收到的答复似乎来自从头开始创建表与从现有表中选择数据。我已经尝试实施该示例,但出现错误。我可以附上一些数据的屏幕截图,以举例说明数据库中包含的内容。

SELECT
inventory.`CODE` AS `Item No.`,
inventory.INV_DESCRIPTION AS Description,
COALESCE(inventory.ONHAND,0) AS `On Hand (LA)`,
COALESCE(inventory.ONHAND,0) AS `On Hand (SF)`,
COALESCE(inventory.ONHAND,0) AS `On Hand (HP)`,
COALESCE(inventory.ONHAND,0) AS `On Hand (CHINA)`,
COALESCE(inventory.ONHAND,0) AS `On Hand (INDO)`,
pricing.BVRTLPRICE01 AS `SF Warehouse`,
pricing.BVRTLPRICE02 AS `LA Warehouse`,
pricing.BVRTLPRICE03 AS `HP Warehouse`,
pricing.BVRTLPRICE04 AS `FOB China/Indonesia`,
pricing.BVRTLPRICE05 AS ECOMM,
inventory.CUBE,
inventory.WEIGHT,
inventory.DIMENSION,
inventory.PROD AS `Status`
FROM
inventory
LEFT JOIN inventory.ONHAND on (inventory.`Item No.` = inventory.`On Hand (LA)` 
AND inventory.WHSE=00)
LEFT JOIN inventory.ONHAND on (inventory.`Item No.` = inventory.`On Hand (SF)` 
AND inventory.WHSE=10)
LEFT JOIN inventory.ONHAND on (inventory.`Item No.` = inventory.`On Hand (HP)` 
AND inventory.WHSE=20)
LEFT JOIN inventory.ONHAND on (inventory.`Item No.` = inventory.`On Hand (CHINA)
` AND inventory.WHSE=50)
LEFT JOIN inventory.ONHAND on (inventory.`Item No.` = inventory.`On Hand (INDO)
` AND inventory.WHSE=70)
INNER JOIN pricing ON inventory.`CODE` = pricing.BVSPECPRICEPARTNO
WHERE
inventory.WHSE IN (00, 10, 20, 50, 70) AND
inventory.PROD IN ('A', 'A-70', 'B', 'C', 'S', 'KIT', 'SET')
GROUP BY
inventory.`CODE`,
inventory.WHSE ASC
ORDER BY
`Item No.` ASC,
inventory.WHSE ASC,
inventory.PROD
Run Code Online (Sandbox Code Playgroud)

red*_*guy 5

鉴于此测试数据:(这是关于如何在未来提出更具可读性的问题的提示):

create table inventory(
  id int unsigned primary key auto_increment,
  item int unsigned,
  warehouse int unsigned,
  qty int unsigned,
  price int unsigned,
  weight int unsigned,
  other int unsigned);

insert into inventory(item, warehouse, qty, price, weight, other) values
  (1,1,1,3,4,5),
  (1,2,6,3,4,5),
  (1,3,3,3,4,5);

> select item,warehouse,qty,price,weight,other from inventory;
+------+-----------+------+-------+--------+-------+
| item | warehouse | qty  | price | weight | other |
+------+-----------+------+-------+--------+-------+
|    1 |         1 |    1 |     3 |      4 |     5 |
|    1 |         2 |    6 |     3 |      4 |     5 |
|    1 |         3 |    3 |     3 |      4 |     5 |
+------+-----------+------+-------+--------+-------+
Run Code Online (Sandbox Code Playgroud)

您可以通过以下查询获得所需的输出:

> select distinct
  i.item,
  COALESCE(w1.qty,0) as w1,
  COALESCE(w2.qty,0) as w2,
  COALESCE(w3.qty,0) as w3,
  i.price,
  i.weight,
  i.other
from inventory i
  left join inventory w1 on (i.item = w1.item AND w1.warehouse=1)
  left join inventory w2 on (i.item = w2.item and w2.warehouse=2)
  left join inventory w3 on (i.item = w3.item and w3.warehouse=3);

+------+------+------+------+-------+--------+-------+
| item | w1   | w2   | w3   | price | weight | other |
+------+------+------+------+-------+--------+-------+
|    1 |    1 |    6 |    3 |     3 |      4 |     5 |
+------+------+------+------+-------+--------+-------+
Run Code Online (Sandbox Code Playgroud)

COALESCE 函数和LEFT JOINs 是针对item、仓库组合没有行的情况