我有一张表Orders (Id, OrderDate, CreatorId)
和一张表OrderLines (Id, OrderId, OwnerIdentity, ProductId, Amount)
场景如下:有人打开Order
,然后其他用户可以在该订单上下达产品订单。这些用户是OwnerId
的{{1}}。
我需要检索用户下订单的前3个最新订单并显示他下达的所有订单,以便让他了解他最近的个人订单。
所以我的最终结果将是
OrderLines
答案 0 :(得分:0)
<my:UserControl1>
<my:UserControl1.Items>
<my:MyItem ButtonText="test" Imagepath="../img.png" />
<my:MyItem ButtonText="test2" Imagepath="../img2.png" />
</my:UserControl1.Items>
</my:UserControl1>
您可以将日期时间列添加到OrderLines表以查询最新的个人订单,然后通过将“Order by OrderDate desc”部分移动到子选择查询来更新代码。
答案 1 :(得分:0)
尝试以下查询:
SELECT OL.OrderId, OL.ProductID, OL.Amount
FROM OrderLines OL WHERE OL.OrderId IN
(
SELECT TOP 3 O.OrderID FROM orders O LEFT JOIN OrderLines OL2
ON OL2.orderId=O.OrderID
WHERE OL2.OwnerIdentity =...
ORDER BY O.OrderDate DESC
) AND WHERE OL.OwnerIdentity =...
答案 2 :(得分:0)
select * from
(
select OrderId, ProductId, Amount
row_number() over (partition by OrderID order by Orders.OrderDate) as rn
from OrderLines
join Orders
on OrderLines.OrderId = Orders.Id
where OwnerIdentity = x
) lskdfj
where rn <= 3
答案 3 :(得分:0)
;WITH cte AS (
SELECT ol.OrderId, ol.ProductId, ol.Amount,
ROW_NUMBER()OVER (PARTITION BY ol.OrderId ORDER BY o.OrderDate DESC) rn
FROM OrderLines ol
JOIN Orders o ON ol.OrderId = o.Id
WHERE OwnerIdentity = @OwnerId
)
SELECT OrderId, ProductId, Amount
FROM cte
WHERE rn <= 3