Oracle SQL组由列不在组中或具有聚合功能

发布于 2025-01-23 19:17:38 字数 917 浏览 0 评论 0原文

假设我们有一个:

类别商品价格
卡车发动机$ 300
车灯$ 50
卡车制动器$ 100
汽车发动机$ 400

我们如何获得产生此产品的SQL声明?假设物品和价格之间存在一对一的关系,因此当我们找到最便宜的价格时,我们也会显示该商品。

中选择类别,项目,最小(价格),

按类别从表


这可以在没有子查询的情况下完成吗?

可以在Python Pandas Group中完成吗?

Suppose we have this:

CategoryItemPrice
TruckEngine$300
TruckLamp$50
TruckBrake$100
CarEngine$400

How can we have a SQL statement that produces this? Assuming there is a one to one relationship between Item and Price, so that when we find the cheapest price we also display the item.

SELECT Category, Item, min(Price)

From table

group by Category


Can this be done without a subquery?

Can it be done in Python Pandas Group By?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

呆萌少年 2025-01-30 19:17:38

这:

假设项目与价格之间存在一对一的关系

,意味着什么?每个项目只有一个价格?那是“一对一”,不是吗?如果是这样,那有什么问题? 您需要的查询很简单

select category, item, price
from parts;

另一方面,

,您不会说

这样,当我们找到最便宜的价格时,我们也会显示项目

最便宜的价格?那么,毕竟一个项目可以有多个价格吗?那不是一对一的。

无论如何:假设您的表看起来像这样:

SQL> select category, item, price from parts order by 1, 2;

CATEG ITEM        PRICE
----- ------ ----------
Car   Engine        400
Truck Brake         100
Truck Engine        300         --> two prices for truck's engines
Truck Engine        999         --> (300 being the cheapest)
Truck Lamp           50

您编写的查询似乎与您需要的内容很近 - 只需将item添加到 rapears group中:

SQL> select category, item, min(price) price
  2  from parts
  3  group by category, item
  4  order by category, item;

CATEG ITEM        PRICE
----- ------ ----------
Car   Engine        400
Truck Brake         100
Truck Engine        300
Truck Lamp           50

可以在没有子查询的情况下完成此操作?

为什么?并非所有的子都邪恶;要点不是多次从同一表获取数据。这样的事物不是 - 只有子查询(在此示例中以CTE的形式以CTE的形式)从Parts表中获取数据;主查询(第6行)只是以最低价格和项目价格获取行:

SQL> with temp as
  2    (select category, item, price,
  3       rank() over (partition by category, item order by price) rn
  4     from parts
  5    )
  6  select category, item, price
  7  from temp
  8  where rn = 1
  9  order by category, item;

CATEG ITEM        PRICE
----- ------ ----------
Car   Engine        400
Truck Brake         100
Truck Engine        300
Truck Lamp           50

SQL>

可以在python pandas group中完成吗?

我不知道。


如果那不是您想要的,那么您必须更好地解释它。

This:

Assuming there is a one to one relationship between Item and Price

means ... what, exactly? That each item has only one price? That's "one-to-one", isn't it? If so, then what's the problem? Query you need is a simple

select category, item, price
from parts;

On the other hand, you wouldn't be saying

so that when we find the cheapest price we also display the item

Cheapest price? So, one item can have multiple prices, after all? That's not one-on-one, then.

Anyway: presuming your table looks like this:

SQL> select category, item, price from parts order by 1, 2;

CATEG ITEM        PRICE
----- ------ ----------
Car   Engine        400
Truck Brake         100
Truck Engine        300         --> two prices for truck's engines
Truck Engine        999         --> (300 being the cheapest)
Truck Lamp           50

Query you wrote seems to be close to what you need - just include item into the group by clause:

SQL> select category, item, min(price) price
  2  from parts
  3  group by category, item
  4  order by category, item;

CATEG ITEM        PRICE
----- ------ ----------
Car   Engine        400
Truck Brake         100
Truck Engine        300
Truck Lamp           50

Can this be done without a subquery?

Why? Not all subqueries are evil; the point is not to fetch data from the same table multiple times. Something like this doesn't - only the subquery (in a form of a CTE in this example) fetches data from the parts table; main query (line #6) just fetches rows with lowest price per category and item:

SQL> with temp as
  2    (select category, item, price,
  3       rank() over (partition by category, item order by price) rn
  4     from parts
  5    )
  6  select category, item, price
  7  from temp
  8  where rn = 1
  9  order by category, item;

CATEG ITEM        PRICE
----- ------ ----------
Car   Engine        400
Truck Brake         100
Truck Engine        300
Truck Lamp           50

SQL>

Can it be done in Python Pandas Group By?

I have no idea.


If that's not what you're looking for, you'll have to explain it better.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文