我正在寻找一个基于桌子的查询,我的商店里有所有商品。该表包含以下列:
location: Shop Name
product: Item Number
attributinstance_id: not in use so it always is NULL
Units:count of Items
我喜欢写一个查询,在那里我可以看到哪些商品在其他商店中,以及它们中的多少和结果应该在同一行。
例如:
Shop 1 Shop 2 Shop 3
item 1 1 2 5
我可以使用所有树店中可用或可用的物品运行,但是当我查找仅在其中一个树店中可用的物品时,它们将无法显示。有没有人有想法?
到目前为止我的想法:
SELECT Distinct
products.name AS Artikel,
eins.units AS Europa_Center,
zwei.Units AS O_Strasse,
drei.units AS K_Strasse
FROM stockcurrent eins
LEFT JOIN stockcurrent zwei
ON (eins.product = zwei.product
AND eins.location ='0'
AND zwei.location ='a59cb899-27f4-460c-b5df-89a89eaaef75')
LEFT JOIN stockcurrent drei
ON (zwei.product = drei.product
AND zwei.location ='a59cb899-27f4-460c-b5df-89a89eaaef75'
AND drei.location ='3b53adf5-eaee-4a13-b22b-39d50b14e497')
JOIN products
ON products.id = eins.product
JOIN locations
ON locations.id = eins.location
整体认为是POS系统Unicentas 4.3。表格如下:
Location Product atributsinstance_id Units shop_1 item_1 NULL 5 shop_2 item_1 NULL 1 shop_3 item_1 NULL 0 shop_2 item_2 Null 3 shop_1 item_3 Null 2 shop_2 item_3 Null 4 shop_3 item_3 Null 1
答案 0 :(得分:1)
您需要条件聚合:
SELECT product_id,
SUM(CASE WHEN location = 'SHOP1' THEN units ELSE 0 END) as shop_1,
SUM(CASE WHEN location = 'SHOP2' THEN units ELSE 0 END) as shop_2,
SUM(CASE WHEN location = 'SHOP3' THEN units ELSE 0 END) as shop_3
FROM stock
GROUP BY product_id