显示最大值和与最大值相关的列的查询

huangapple go评论75阅读模式
英文:

Query to display both max value and the column that relates to the max value

问题

我必须获取产品名称和迄今为止订购该产品的总数量,以便将它们并排显示,以显示具有最大总订单数量的产品。

数据来自两个表(1)Sales_order和(2)Products

销售订单表结构:
ID(整数)
product(整数)
quantity_ordered(整数)
产品表结构:
Product_code(整数)
Product_line(Varchar(20))
它们通过Products.Product_code ->> Sales_order.product链接。

我的查询是:

select x.product_line, x.TotalQty from 	
    ( select P.Product_line, sum(S.quantity_ordered) as TotalQty 		
      from sales_order S 		
      join products P on S.product = P.product_code 		
      group by P.Product_line
     order by TotalQty desc 	
    ) x 
 where x.TotalQty = (select max(x.TotalQty) from x)

内部查询产生以下结果:

product_line    TotalQty
"Classic Cars"	33992
"Vintage Cars"	21069
"Motorcycles"	11663

但一旦我加上外部查询和'where'子句,就会出现以下错误:

错误:'select'附近的语法错误 LINE 8: where x.TotalQty = select max(x.TotalQty) from x

我还尝试了:

where x.TotalQty = (select max(x.TotalQty) from x)

通过在最后一个select周围加上括号,但这会导致错误,表明关系x不存在。

我是SQL新手,无法弄清楚我做错了什么以及如何纠正它。(要求不是“找到答案”,比如通过按降序排序并选择第一个项目。我应该编写代码,仅为最大值或与所有订单数量的平均值完全匹配的产品线和TotalQty一起显示)。

英文:

I have to get the product name and the total qty of that product ordered to date to be displayed side by side for the product that has the maximum total order quantity.

The data comes from two tables (1) Sales_order and (2) Products

Sales order table structure:
ID (INT)
product (INT)
quantity_ordered(INT)
Products table structure:
Product_code (INT)
Product_line (Varchar(20))
They are linked by Products.Product_code ->> Sales_order.product

My query was:

 select x.product_line, x.TotalQty from 	
    ( select P. Product_line,sum(S.quantity_ordered) as TotalQty 		
      from sales_order S 		
      join products P on S.product = P.product_code 		
      group by P. Product_line
     order by TotalQty desc 	
    ) x 
 where x.TotalQty = select max(x.TotalQty) from x

The inner query produced the following result:

product_line    TotalQty
"Classic Cars"	33992
"Vintage Cars"	21069
"Motorcycles"	11663

but once I put the outer query and the 'where' clause, I get the following error:

> ERROR: syntax error at or near "select" LINE 8: where x.TotalQty =
> select max(x.TotalQty) from x

I also tried :

where x.TotalQty = (select max(x.TotalQty) from x) 

by putting parentheses around the last select, but this gave an error that relation x does not exist.

I am new to SQL and can't figure out what I am doing wrong and how to correct it. (The requirement is not to "find the answer" like by using order-by in descending order and picking the first item. I am supposed to write code to display both the product_line and the TotalQty side by side for only the max value, or the one that exactly matches the average of all order quantities ...etc.).

答案1

得分: 1

A CTE will be a better a choice, as you can use the CTE as basis for your queries.

CREATE TABLE sales_order(quantity_ordered int, product int)
CREATE TABLE products (Product_line varchar(10), product_code int)
WITH CTE AS (SELECT P.Product_line, SUM(S.quantity_ordered) as TotalQty        
      FROM sales_order S        
      JOIN products P ON S.product = P.product_code         
      GROUP BY P.Product_line
      ORDER BY TotalQty DESC)
 SELECT x.Product_line, x.TotalQty FROM     
    CTE x
 WHERE x.TotalQty = (SELECT MAX(TotalQty) FROM CTE)
Product_line TotalQty

[fiddle](https://dbfiddle.uk/UI54pVig)

<details>
<summary>英文:</summary>

A CTE will be a better a choice, as you can use the CTE as basis for your queries.

CREATE tABLe sales_order(quantity_ordered int, product int)



CREATE tABLe products (Product_line varchar(10),product_code int)



WITH CTE AS (select P. Product_line,sum(S.quantity_ordered) as TotalQty
from sales_order S
join products P on S.product = P.product_code
group by P. Product_line
order by TotalQty desc)
select x.product_line, x.TotalQty from
CTE x
where x.TotalQty = (select max(TotalQty) from CTE)

| product\_line | totalqty |
|:-------------|---------:|
&gt; ``` status
&gt; SELECT 0
&gt; ```

[fiddle](https://dbfiddle.uk/UI54pVig)


</details>



huangapple
  • 本文由 发表于 2023年7月7日 06:54:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76632975.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定