SQL选择所有行与同一股票的所有行,其中至少一个行包括一个特定值

发布于 2025-01-26 02:44:29 字数 577 浏览 2 评论 0原文

我有一个股票股票数据库。每个股票都有一个“结构”,表示我们参与的交易类型以接收股票。结构的示例是主要产品(“ PO”),次要产品(“ SEC”),“ CMPO”,“ PIPE”等。数据库可能会多次包含具有不同结构的股票。例如,股票“ AAPL”可能存在2次 - 一次带有“ PO”结构,另一个时间为“ SEC”。我正在尝试写一个查询,如果至少一个结构是“ po”,可以收集所有票的所有情况。我想展示我们参与该股票的所有结构。

从@mytable中选择 * structure ='po'只会向我展示与结构po的交易。如果股票的结构与“ po”相等,那么无论结构如何,我都希望与该股票的所有交易。

我也尝试过:

select * from (select distinct ticker from @mytable where structure = 'PO') PO
join @mytable m on PO.ticker = m.ticker

这起作用,但是将表连接到同一张桌子似乎是多余的,似乎应该有更好的方法来做到这一点。

我在SQL上编写此查询 - 有人可以帮我为这种情况写一个查询吗?谢谢你!!

I have a database of stock tickers. Each ticker has a "structure" that signifies the type of deal we participated in in order to receive the shares. Examples of structures are Primary Offerings ("PO"), Secondary Offerings ("SEC"), "CMPO", "PIPE", etc. It is possible that the database includes a ticker multiple times with different structure. For example, the ticker "AAPL" could exist 2 times - one time with a structure of "PO" and another time with a structure of "SEC". I am trying to write a query that gathers all of the instances of a ticket if at least one of the structures is "PO". I want to show all of the structures that we have participated in in that ticker.

select * from @mytable where structure = 'PO' will only show me the deals with structure PO. If a ticker has a structure equal to 'PO' then I want all deals with that ticker regardless of the structure.

I have also tried:

select * from (select distinct ticker from @mytable where structure = 'PO') PO
join @mytable m on PO.ticker = m.ticker

This works, but joining a table to the same table seems redundant and it seems like there should be a better way to do this.

I am writing this query in SQL -- could someone please help me write a query for this situation? Thank you!!

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

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

发布评论

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

评论(1

梦开始←不甜 2025-02-02 02:44:29

我认为没有两次从桌子中选择的方法。但是,如果您不喜欢自我加入,则可以进行相关的子查询。

select * from mytable where ticker in
 (select ticker from mytable where structure = 'PO')

I don't think there is a way around selecting from the table twice. however if you don't like the self join you could do a correlated subquery.

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