SQL Server 按采购订单号、帐号、产品类型分组按插入时间戳排序

42fyovps  于 2023-01-01  发布在  其他
关注(0)|答案(1)|浏览(141)

我们有一个很大的订单表,这些订单在处理时按PurchaseOrderNumber、AccountNumber和ProductType分组。客户端请求我们根据InsertionTimestamp处理这些订单。
下面是我们使用的查询,它工作正常,但需要根据客户端的请求进行更改。

Select Purchase_Order_Number, Account_Number, Max(LineNumber) LineNumber, Product_Type 
From dbo.PDF_Orders_Validation 
Where BotReady = 'Y' 
Group By Purchase_Order_Number, Account_Number, Product_Type

我试过但不管用...

Select Purchase_Order_Number, Account_Number, Max(LineNumber) LineNumber, Product_Type
From dbo.PDF_Orders_Validation 
Where BotReady = '' 
Group By Purchase_Order_Number, Account_Number, Product_Type
Order By InsertionTimestamp

我也试过这个...

Select Purchase_Order_Number, Account_Number, Max(LineNumber) LineNumber, Product_Type
From dbo.PDF_Orders_Validation 
Where BotReady = '' 
Group By Purchase_Order_Number, Account_Number, Product_Type, InsertionTimestamp
Order By InsertionTimestamp

...该方法有效,但创建了太多行,可能会导致订单重复。
我需要的是保留原始查询,但只需按InsertionTimestamp的升序对行重新排序。
对此的任何帮助将不胜感激。

zsbz8rwp

zsbz8rwp1#

你试过这个吗?

Select Purchase_Order_Number, Account_Number, Max(LineNumber) LineNumber, Product_Type
From dbo.PDF_Orders_Validation 
Where BotReady = '' 
Group By Purchase_Order_Number, Account_Number, Product_Type
Order By MAX(InsertionTimestamp)

注意--不确定您的处理管道,因此可能需要MIN()而不是MAX()

相关问题