且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

T-SQL:最多两个其他列的SELECT相关列数据

更新时间:2022-12-09 12:21:22

我认为您想要这样做:

select order_type
     , po_num
     , max(order_num)
  from orders o1
 where order_type = (
         select max(order_type)
           from orders o2
          WHERE o2.po_num = o1.po_num
      ) 
 group by po_num,order_type

在group by中包含order_type是一个 artifact ,这是必需的,因为表格的设计方式如此.

The inclusion of order_type in the group by is an artifact, it is required because of how the table is designed.

FWIW,报价和订单应分为两个表.当您遇到像这样的怪异SQL时,会遇到困难或有条件的唯一约束,这就是表设计问题.

FWIW, the quotes and orders should be split out into two tables. When you get weird SQL like this an difficult or conditional unique constraints it is a table design issue.