从子查询中选择结果

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

Selecting results from subquery

问题

I have a table "Article" [product in ENG] which contains as attributes (numArticle [PK], poids [weight in English], des_ [Designation], couleur [color]) and I want to select the designation of the products with highest weight from each color.

I tried this

SELECT des_
FROM Article
WHERE poids = ANY (SELECT MAX(poids) 'poidse'
                   FROM Article
                   GROUP BY couleur);

but didn't work and I didn't know why at first but figured it out later.

Simply put I want to assign to each weight of a product in subquery it's designation.

The subquery returns the highest weight of each color but doesn't give the designation since I can't use select designation without including it in GROUP BY Clause because if I did that its gives unwanted result.

英文:

I have a table "Article" [product in ENG] which contains as attributes (numArticle [PK], poids [weight in English], des_ [Designation], couleur [color]) and I want to select the designation of the products with highest weight from each color.

I tried this

SELECT des_
FROM Article
WHERE poids = ANY (SELECT  MAX(poids) 'poidse'
                   FROM Article
                   GROUP BY couleur);

but didn't work and I didn't know why at first but figured it out later.

Simply put I want to assign to each weight of a product in subquery it's designation.

The subquery returns the highest weight of each color but doesn't give the designation since I can't use select designation without including it in GROUP BY Clause because if I did that its gives unwanted result.

答案1

得分: 2

你可以使用 ROW_NUMBER 来按照最高 poids 对每种颜色的记录进行排序。然后,使用 SELECT 仅选择每种颜色中的第一个记录,使用 TOP 1 WITH TIES

SELECT TOP 1 WITH TIES 
    numArticle,
    poids,
    des_,
    couleur
FROM Article
ORDER BY ROW_NUMBER() OVER (PARTITION BY couleur ORDER BY poids DESC)
英文:

You can use ROW_NUMBER to order the records for each color starting with the one with highest poids. Then, SELECT only the first ones from each color using TOP 1 WITH TIES:

SELECT TOP 1 WITH TIES 
		numArticle,
		poids,
		des_,
		couleur
FROM Article
ORDER BY ROW_NUMBER() OVER (PARTITION BY couleur ORDER BY poids DESC) 

答案2

得分: 1

以下是翻译的内容:

我对你提出的问题的理解如下:

  • 你想要同时按颜色分组并获取最大权重,但不能像这样执行:select des_,max(weight),color from [table] gourp by color
  • 所以你需要构建一个子查询,类似这样:SELECT couleur, MAX(poids) AS max_poids FROM Article GROUP BY couleur 来关联权重和颜色
  • 你可以使用连接来解决这个问题。具体而言,你可以使用内连接将Article表与按最大权重和颜色分组的子查询连接起来,通过使用子查询中每个颜色的最大权重以及文章表中的相应指定来检索所需的结果。

解决方案:

SELECT Article.des_
FROM Article
INNER JOIN (
    SELECT couleur, MAX(poids) AS max_poids
    FROM Article
    GROUP BY couleur
) AS max_poids_coul
ON Article.couleur = max_poids_coul.couleur AND Article.poids = max_poids_coul.max_poids;

附注:
在此查询中,子查询使用GROUP BY检索每种颜色的最大权重,并使用AS关键字为最大权重列添加别名。然后,通过使用INNER JOIN将子查询结果与Article表连接,通过匹配颜色和权重来检索所需的指定。

英文:

My understanding of the question you raised is as follows:

  • you want to group by color and MAX in weight at the same time,but you can't do like this select des_,max(weight),color from [table] gourp by color
  • so you need construct a subquery like this SELECT couleur, MAX(poids) AS max_poids
    FROM Article
    GROUP BY couleur
    to connect weight and color
  • You can use a join to solve this problem. Specifically, you can use an inner join to join the Article table with the subquery that groups by maximum weight and color, and retrieve the desired result by using the maximum weight of each color from the subquery and the corresponding designation from the article table.

Solution

SELECT Article.des_
FROM Article
INNER JOIN (
    SELECT couleur, MAX(poids) AS max_poids
    FROM Article
    GROUP BY couleur
) AS max_poids_coul
ON Article.couleur = max_poids_coul.couleur AND Article.poids = max_poids_coul.max_poids;

PS:

In this query, the subquery uses GROUP BY to retrieve the maximum weight for each color and adds an alias to the maximum weight column using the AS keyword. Then, by using an INNER JOIN to connect the subquery result with the Article table, the desired Designation is retrieved by matching the color and weight.

huangapple
  • 本文由 发表于 2023年4月11日 14:01:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75982797.html
匿名

发表评论

匿名网友

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

确定