具有计算的动态最小值最大值的 SQL 查询
我遇到了 sql 问题。我有两个表,如下所示:
first TABLE X second TABLE Y
TabX_ID| DATE | Value Z TabY_ID|TabX_ID | DATE | Value X | Value Y
4711 | 15.01 | 12 1 | 4711 | 15.01| 123 | 876
4711 | 20.01 | 5 2 | 4711 | 16.01| 12 | 54
4711 | 25.01 | 67 3 | 4711 | 17.01| 23 | 38
4 | 4711 | 20.01| 56 | 13
5 | 4711 | 23.01| 1 | 5
我需要将表 Y 中的所有数据分配给表 X 日期中的数据以进行拟合 大体时间。
我不能使用简单的最小值-最大值,因为它会改变。
1. DATE min 15.01 DATE-max:19.01
2. DATE-min:20.01 DATE-max:24.01
3. DATE-min:25.01 DATE-max:...
所以它看起来像这样
1 | 15.01 | 123 | 876
4711 | 15.01 | 12 -> 2 | 16.01 | 12 | 54
3 | 17.01 | 23 | 38
4711 | 20.01 | 5 -> 4 | 20.01 | 56 | 13
5 | 23.01 | 1 | 5
首先我需要使用表 Y 值 X 和 Y 执行计算,然后我需要值 Z 从表 X 来看。看起来像这样:
ID | DATE | Calculated_Val
4711| 15.01 | 345
4711| 20.01 | 892
有办法做到这一点吗?
提前谢谢
I got an sql issue. I have two tables which look like this:
first TABLE X second TABLE Y
TabX_ID| DATE | Value Z TabY_ID|TabX_ID | DATE | Value X | Value Y
4711 | 15.01 | 12 1 | 4711 | 15.01| 123 | 876
4711 | 20.01 | 5 2 | 4711 | 16.01| 12 | 54
4711 | 25.01 | 67 3 | 4711 | 17.01| 23 | 38
4 | 4711 | 20.01| 56 | 13
5 | 4711 | 23.01| 1 | 5
I need to assing all the data from TABLE Y to the data in the TABLE X DATE to the fitting
timeframe.
I cant use a simple min - max because it changes.
1. DATE min 15.01 DATE-max:19.01
2. DATE-min:20.01 DATE-max:24.01
3. DATE-min:25.01 DATE-max:...
So it looks like this
1 | 15.01 | 123 | 876
4711 | 15.01 | 12 -> 2 | 16.01 | 12 | 54
3 | 17.01 | 23 | 38
4711 | 20.01 | 5 -> 4 | 20.01 | 56 | 13
5 | 23.01 | 1 | 5
First I need to perform calculations with the TABLE Y VALUES X an Y and after that I need the VALUE Z
from TABLE X. So it looks like this:
ID | DATE | Calculated_Val
4711| 15.01 | 345
4711| 20.01 | 892
Is there a way to do this?
thx in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不确定 MySQL,但如果您使用 Oracle 执行此操作,我会使用
LEAD
分析函数获取 tableX 中未来的下一个日期值,然后将其连接到 tableY。例如:
请注意,我没有修改日期的下一个值,因此使用小于条件。如果您的任何日期字段中有时间部分,这可能是合适的,但如果它们只是日期值,则可能不完全是您想要的。
Not sure about MySQL but if you are doing this with Oracle, I would use the
LEAD
analytic function to get the next date value in the future in tableX and then join that to tableY.An example of this would be:
Note that I haven't modified the next value of the date so am using a less-than condition. This is probably appropriate if you have a time component in any of the date fields but might not be exactly what you want if they are just date value.
这是一个简单的连接和分组依据:
This is a simple join and group by: