使用行中的值获取同一行另一列的数据

发布于 2024-12-24 17:11:39 字数 479 浏览 1 评论 0原文

可能的重复:
从 Oracle 表获取值计数

我对 2 有一个简单的查询表 A 和 B

Table A
========
LOCATION, NAME

Table B
========
LOCATION, SKU

查询是:

select A.LOCATION, B.SKU 
  from A, B 
 where A.Location = B.Location

在此查询的结果中,我想添加另一列,其中包含 B 中所有位置的计数,这些位置具有与上述查询返回的 SKU 相同的 SKU。

Possible Duplicate:
Geting value count from an Oracle Table

I have a simple query on 2 tables A and B

Table A
========
LOCATION, NAME

Table B
========
LOCATION, SKU

The query is:

select A.LOCATION, B.SKU 
  from A, B 
 where A.Location = B.Location

In the result of this query, I would like to add another column with the count of all locations in B which have the same SKU as returned by the above query.

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

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

发布评论

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

评论(2

ぺ禁宫浮华殁 2024-12-31 17:11:39

我在这里假设您需要 A.Location、B.SKU,然后是 B.SKU 存在的 B.Location 值的计数。如果这是真的,那么它应该只是一个左外连接:

Select A.Location, B.SKU, Count(B.Location)
  from 
B Left outer join A on (B.Location = A.Location)
Where B.SKU in ( /* value */ )
group by A.Location, B.SKU

这里唯一需要注意的是,对于为 SKU 返回的每个 A.Location,您将看到一行包含该位置、SKU 和重复计数的行:

Location1, ABC, 100
Location2, ABC, 100

Location3, ABC,100

I'm making the assumption here that you want A.Location, B.SKU and then the count of B.Location values that exist for B.SKU. If that is true then it should just be a left outer join:

Select A.Location, B.SKU, Count(B.Location)
  from 
B Left outer join A on (B.Location = A.Location)
Where B.SKU in ( /* value */ )
group by A.Location, B.SKU

The only thing to note here is that for each A.Location returned for a SKU, you will see a line with that location, the SKU and the repeated count:

Location1, ABC, 100
Location2, ABC, 100

Location3, ABC, 100

自演自醉 2024-12-31 17:11:39

您应该查看 Oracle 分析函数。这允许您添加聚合列(例如计数),而无需对数据进行分组。

所以,也许是这样的:

 select A.LOCATION, B.SKU, count(b.location) over (partition by sku) as locskucount
  from A, B
 where A.Location=B.Location
 order by a.location

这将为您提供每个 SKU 的位置计数。

You should look into the Oracle analytical functions. This allows you to add aggregate columns (such as count) without grouping the data.

So, perhaps something like this:

 select A.LOCATION, B.SKU, count(b.location) over (partition by sku) as locskucount
  from A, B
 where A.Location=B.Location
 order by a.location

This will give you a count of locations per SKU.

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