Sql = 想要使用内连接语句但不使用表

发布于 2024-08-15 16:58:24 字数 600 浏览 12 评论 0原文

我想利用这样一个事实:在两个表 t1t2 上,我可以使用 on t1.colum1>t2.colum2 进行内部联接计算返回向量的最大回撤。问题是内部联接只能与两个存储的数据库或表一起使用,并且我想只选择一部分表。

还有其他可能性吗,我对 sql 完全陌生,找不到任何其他选项?

谢谢您

在操作我的内部联接之前编辑

以便能够计算我的最大回撤我必须能够在表上的选择而不是表本身上进行此内部联接。所以我听从了马克的建议,但我仍然收到错误。这是我的查询:

select * 
from (select * from bars where rownum <= 10 as x)as tab1
inner join (select * from bars where rownum <= 10  as y) as tab2
on tab1.x=tab2.y

错误是 ora-00907 缺少右括号

  • 从 OP 的消息中提取的附加信息,作为这篇文章的答案发布。 *

I want to use the fact that on two tables t1 and t2 I can make an inner join with on t1.colum1>t2.colum2 to calculate the maximum drawdown of a return vector. The problem is that an inner join is only possible with two stored databases or tables and I wanted to do it selecting just a part of the tables.

Is there any other possibility, I am totally new to sql and I can't find any other option?

Thank you

edit

before manipulating my inner join to be able to calculate my maximum drawdown I have to be able to make this inner join on a selection on the tables and not the tables themselves. So I followed Mark's advice but I am still getting an error. Here is my query:

select * 
from (select * from bars where rownum <= 10 as x)as tab1
inner join (select * from bars where rownum <= 10  as y) as tab2
on tab1.x=tab2.y

The error is ora-00907 missing right parenthesis

  • additional information extracted from OP's message published as answer to this post. *

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

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

发布评论

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

评论(3

み零 2024-08-22 16:58:24

您也可以在子选择上进行内部联接,您只需为子选择指定一个别名:

SELECT *
FROM (SELECT 1 AS X) AS T1
INNER JOIN (SELECT 1 AS Y) AS T2
ON T1.X = T2.Y

如果您发布非工作查询,我可以为您提供一个更好的答案,更适合您的确切表。

You can inner join on subselects too, you just need to give the subselects an alias:

SELECT *
FROM (SELECT 1 AS X) AS T1
INNER JOIN (SELECT 1 AS Y) AS T2
ON T1.X = T2.Y

If you post your non-working query, I can give you a better answer more tailored to your exact tables.

埋葬我深情 2024-08-22 16:58:24

(内部)联接不限于整个表。

(inner) join is not limited to whole tables.

狼性发作 2024-08-22 16:58:24

我从一个投资网站得到了最大回撤的定义(感谢谷歌!)。所以我认为我们需要计算图表中最高点与其随后的最低点之间的百分比下降。

以下查询计算过去 12 个月内 Oracle 股票投资的最大回撤。它将investments 表与其自身连接起来,并使用别名来区分表的版本(一个代表最高峰,一个代表最低谷)。这可能无法反映您精确的业务逻辑,但它显示了 Oracle 为您提供的 SQL 技术。

select round(((max_return-min_return)/max_return)*100, 2) as max_drawdown
from
    ( select max(t1.return_amt) as max_return
             , min(t2.return_amt) as min_return
      from investments t1
           join  investments t2
           on ( t1.stock_id = 'ORCL'
                and   t2.stock_id = t1.stock_id
                and   t2.created_date > t1.created_date )
      where t1.created_date >= add_months(sysdate, -12)
      and t2.created_date >= add_months(sysdate, -12)
    )
/

如果股票在窗口期间没有经历下跌,则此查询将返回零。它也不会检查随后的上涨(据我所知,回撤应该是低谷的底部,只有在股票开始再次攀升时我们才能确定这一点)。

关于在家培训,我们可以从 Oracle TechNet 用于此目的。如果带宽或磁盘空间有问题,请选择 Express Edition;它不具备所有功能,但您可能暂时不会需要它们。 Oracle 确实提供了一个免费的 IDE,SQL Developer。顾名思义,它主要针对开发人员,但它具有 DB Artisan 的许多面向 DBA 的功能。对于全面的数据库管理,Oracle 提供了 企业经理

编辑

在评论中建议

您可以添加t1.return_amt >
t2.return_amt
在连接中作为次要
优化

我认为 return_amt 不太可能被索引,所以我认为这样的子句不太可能对性能产生影响。它将做的是改变没有回撤的股票的行为。对于在时间窗口内持续增长的股票,我提出的查询返回零。在这种情况下,附加过滤器将返回 NULL。哪个结果更理想取决于品味(或需求规范)。

I got a definition of maximum drawdown from an investment website (thanks Google!). So I think we need to calculate the percentage drop between the highest point in a graph and its subsequent lowest point.

The following query calculates the maximum drawdown on investments in Oracle stock over the last twelve months. It joins the investments table to itself, with aliases to distinguish the versions of the table (one for the highest peak, one for the lowest trough). This may not mirror your precise business logic, but it shows the SQL techniques which Oracle offers you.

select round(((max_return-min_return)/max_return)*100, 2) as max_drawdown
from
    ( select max(t1.return_amt) as max_return
             , min(t2.return_amt) as min_return
      from investments t1
           join  investments t2
           on ( t1.stock_id = 'ORCL'
                and   t2.stock_id = t1.stock_id
                and   t2.created_date > t1.created_date )
      where t1.created_date >= add_months(sysdate, -12)
      and t2.created_date >= add_months(sysdate, -12)
    )
/

This query will return zero if the stock has not experienced a drop during the window. It also does not check for a following upturn (as I understand drawdown it is supposed to be the bottom of a trough, a point we can only establish once the stock has started to climb again).

With regard to training at home, we can download software from Oracle TechNet for that purpose. If bandwidth or disk space are an issue go for the Express Edition; it doesn't have all the features but you probably won't want them for a while yet. Oracle do provide a free IDE, SQL Developer. As its name suggests it is primarily targeted at developers but it has many of the DBA-oriented features of DB Artisan. For full-on database management Oracle offers Enterprise Manager.

edit

In the comments outis suggests

You could add a t1.return_amt >
t2.return_amt
in the join as a minor
optimization

I think it is unlikely that return_amt would be indexed, so I think it is unlikely that such a clause would have an impact on performance. What it would do is change the behaviour for stocks which do not have a drawdown. The query I presented returns zero for stocks which have increased continuously through the time window. The additional filter would return a NULL in such a case. Which is the more desirable outcome is a matter of taste (or requirements spec).

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