Oracle SQL组由列不在组中或具有聚合功能
假设我们有一个:
类别 | 商品 | 价格 |
---|---|---|
卡车 | 发动机 | $ 300 |
卡 | 车灯 | $ 50 |
卡车 | 制动器 | $ 100 |
汽车 | 发动机 | $ 400 |
我们如何获得产生此产品的SQL声明?假设物品和价格之间存在一对一的关系,因此当我们找到最便宜的价格时,我们也会显示该商品。
中选择类别,项目,最小(价格),
按类别从表
组
这可以在没有子查询的情况下完成吗?
可以在Python Pandas Group中完成吗?
Suppose we have this:
Category | Item | Price |
---|---|---|
Truck | Engine | $300 |
Truck | Lamp | $50 |
Truck | Brake | $100 |
Car | Engine | $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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这:
,意味着什么?每个项目只有一个价格?那是“一对一”,不是吗?如果是这样,那有什么问题? 您需要的查询很简单
另一方面,
最便宜的价格?那么,毕竟一个项目可以有多个价格吗?那不是一对一的。
无论如何:假设您的表看起来像这样:
您编写的查询似乎与您需要的内容很近 - 只需将
item
添加到 rapears group中:为什么?并非所有的子都邪恶;要点不是多次从同一表获取数据。这样的事物不是 - 只有子查询(在此示例中以CTE的形式以CTE的形式)从
Parts
表中获取数据;主查询(第6行)只是以最低价格和项目价格获取行:我不知道。
如果那不是您想要的,那么您必须更好地解释它。
This:
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
On the other hand, you wouldn't be saying
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:
Query you wrote seems to be close to what you need - just include
item
into thegroup by
clause: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:I have no idea.
If that's not what you're looking for, you'll have to explain it better.