我知道这里有不计其数的东西,但是我已经花了一个小时阅读它们并进行尝试。
我要做的就是返回订单数和分配给订单的商品。 总计,而不是每个。
因此,如果我有300个订单,每个订单正好有3个项目,则需要退货:
-- OrderCount: 300 ItemCount: 900
SELECT COUNT (o.id) AS OrderCount, COUNT (i.id) AS ItemCount
FROM order_detail o JOIN item_detail i
ON i.job_id = o.id
WHERE o.customer_terminal_id = 182 AND o.requested_del_date = '2018-12-03'
这将返回所有内容的计数,并且它们都是相同的。 我也尝试过分组。
更新: 这有效:
SELECT COUNT (DISTINCT o.id) AS OrderCount, COUNT (i.id) AS ItemCount
FROM order_detail o JOIN item_detail i
ON i.job_id = o.id
WHERE o.customer_terminal_id = 182 AND o.requested_del_date = '2018-12-04'
,但真正的要求是某些订单转到具有相同地址的位置。我真正需要的是这些唯一地址的数量。 它们位于名为Common_Address_points的表中,该表通过
连接到订单o.Delivery_Location_Id = Common_Address_Points.id
因此,所需的输出将是订单,唯一的地址和物品
因此,在上面,如果所有这些都是1个订单到1个地址,除了1个有10个订单到同一个地址,它将显示:
订单:300 地址:290 物品:900
我认为是这样做的,但我必须验证:
SELECT COUNT (DISTINCT o.id) AS OrderCount, COUNT (i.id) AS ItemCount, COUNT (DISTINCT Common_Address_Point.Id) AS Addresses
FROM order_detail o
JOIN item_detail i ON i.job_id = o.id
JOIN Common_Address_Point ON Common_Address_Point.Id = o.Delivery_Location_Id
WHERE o.customer_terminal_id = 182 AND o.requested_del_date = '2018-12-04'
答案 0 :(得分:1)
您可以在下面尝试-您需要添加不同的订单数量-COUNT (distinct o.id)
SELECT COUNT (distinct o.id) AS OrderCount, COUNT (i.id) AS ItemCount
FROM order_detail o JOIN item_detail i
ON i.job_id = o.id
WHERE o.customer_terminal_id = 182 AND o.requested_del_date = '2018-12-03'