我在最终结果中有重复。我尝试使用Distinct
以及group by
子句。
Select distinct
Orders.OrderNumber, Tracking.TrackingID,
Orders.BalanceDue, Orders.Approved, Orders.Name, Orders.ShipCity ,
Orders.OrderDate, Orders.country, Orders.Shipping, Orders.CartId
from
orders
left join
Tracking on Orders.OrderNumber = Tracking.ordernum
group by
Orders.OrderNumber, Orders.BalanceDue, Tracking.TrackingID,
Orders.Approved, Orders.Name, Orders.ShipCity,
Orders.OrderDate, Orders.country, Orders.Shipping, Orders.CartId
Orders表中有8166行,我想在最终结果中只有那么多列。
![在此输入图片说明] [3]
答案 0 :(得分:2)
获取一个跟踪ID的一种方法是使用聚合函数和从trackingid
子句中删除group by
:
Select o.OrderNumber, max(t.TrackingID) as TrackingId,
o.BalanceDue, o.Approved, o.Name, o.ShipCity,
o.OrderDate, o.country, o.Shipping, o.CartId
from orders l left join
Tracking t
on o.OrderNumber = t.ordernum
group by o.OrderNumber, o.BalanceDue, o.Approved, o.Name, o.ShipCity,
o.OrderDate, o.country, o.Shipping, o.CartId;