SQL选择所有行与同一股票的所有行,其中至少一个行包括一个特定值
我有一个股票股票数据库。每个股票都有一个“结构”,表示我们参与的交易类型以接收股票。结构的示例是主要产品(“ 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为没有两次从桌子中选择的方法。但是,如果您不喜欢自我加入,则可以进行相关的子查询。
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.