对 T-SQL 中 2 列的最大值进行子查询的最佳方法是什么
What is the best way to do a sub query for a max on 2 columns in T-SQL
我有三个table。
Sales_Header
:Sales_Ordernum,客户,总成本
Sales Details
: Sales_Ordernum, Product_number, Sales_Cost
Product
: Product_number, Product_name, Product_height, Product_length
出于交付目的,我希望 return Product_height
和 Product_length
的最大值可以来自产品 table.
中的多个产品
所以我希望输出看起来像这样:
Sales_Ordernum,
Customer,
Max_Productheight,
Max_Productlength
感谢任何帮助
谢谢
您可以加入和聚合:
select sh.sales_ordernum, sh.customer,
max(p.product_height) as max_product_height,
max(p.product_length) as max_product_length
from sales_header sh
inner join sales_details sd on sd.sales_ordernum = sh.sales_ordernum
inner join product p on p.product_number = sd.product_number
group by sh.sales_ordernum, sh.customer
我有三个table。
Sales_Header
:Sales_Ordernum,客户,总成本Sales Details
: Sales_Ordernum, Product_number, Sales_CostProduct
: Product_number, Product_name, Product_height, Product_length
出于交付目的,我希望 return Product_height
和 Product_length
的最大值可以来自产品 table.
所以我希望输出看起来像这样:
Sales_Ordernum,
Customer,
Max_Productheight,
Max_Productlength
感谢任何帮助
谢谢
您可以加入和聚合:
select sh.sales_ordernum, sh.customer,
max(p.product_height) as max_product_height,
max(p.product_length) as max_product_length
from sales_header sh
inner join sales_details sd on sd.sales_ordernum = sh.sales_ordernum
inner join product p on p.product_number = sd.product_number
group by sh.sales_ordernum, sh.customer