[英]SQL SELECT earliest last update of each unique product and only filled orders with quantity of 1
I need to make a query to select the first order filled of each unique product so if I have three orders for apples 2 orders for bananas and 5 orders for oranges I need to get the first order filled for each apple, banana, and orange from the table 'Orders'我需要查询 select 每个独特产品的第一个订单,所以如果我有 3 个苹果订单、2 个香蕉订单和 5 个橙子订单,我需要为每个苹果、香蕉和橙子获取第一个订单“订单”表
I tried multiple things but this was my best effort我尝试了很多事情,但这是我最大的努力
SELECT [orderID]
,[productname]
,[orderStatus]
,[customerID]
,[lastUpdateTime]
FROM [Orders]
Where lastUpdateTime > cast( getdate() as date )
and (orderStatus = 'filled')
and (productname = UNIQUE)
order by lastUpdateTime;
expecting to get three rows, one for each productname in order of lastupdatetime, each with all that orders information but got an error 'Incorrect syntax near the keyword 'UNIQUE''期望获得三行,每个产品名称按照上次更新时间排序,每行包含所有订单信息,但出现错误“关键字‘UNIQUE’附近的语法不正确”
Here is another similar question I answered. 这是我回答的另一个类似问题。 Please read that for the how and why, but I will adjust so you can see how it applies to your request.请阅读其中的方式和原因,但我会进行调整,以便您了解它如何适用于您的请求。
WITH QryEachProduct AS (
SELECT
o.OrderID,
o.ProductName,
o.OrderStatus,
o.CustomerID,
o.LastUpdateTime,
ROW_NUMBER() OVER (PARTITION BY o.ProductName
ORDER BY o.LastUpdateTime) row_num
FROM
Orders o
)
SELECT
Q.*
FROM
QryEachProduct Q
WHERE
Q.row_num = 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.