具有计算的动态最小值最大值的 SQL 查询

发布于 2024-12-19 03:15:03 字数 1155 浏览 3 评论 0原文

我遇到了 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 技术交流群。

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

发布评论

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

评论(2

原来分手还会想你 2024-12-26 03:15:03

不确定 MySQL,但如果您使用 Oracle 执行此操作,我会使用 LEAD 分析函数获取 tableX 中未来的下一个日期值,然后将其连接到 tableY。

例如:

select
  tabX_id,
  date_val as min_date,
  next_date_val as max_date,
  valueZ,
  valueX,
  valueY,
  y.date_val as tabY_date
from (
  select
    tabX_id,
    date_val,
    lead(date_val) over (partition by tabx_id order by date_val) 
      as next_date_val,
    valueZ
  from tabX
) x
join tabY y on (x.tabX_id = y.tabX_id and 
                y.date_val >= x.date_val and 
                (x._next_date_val is null or y.date_val < x.next_date_val))

请注意,我没有修改日期的下一个值,因此使用小于条件。如果您的任何日期字段中有时间部分,这可能是合适的,但如果它们只是日期值,则可能不完全是您想要的。

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:

select
  tabX_id,
  date_val as min_date,
  next_date_val as max_date,
  valueZ,
  valueX,
  valueY,
  y.date_val as tabY_date
from (
  select
    tabX_id,
    date_val,
    lead(date_val) over (partition by tabx_id order by date_val) 
      as next_date_val,
    valueZ
  from tabX
) x
join tabY y on (x.tabX_id = y.tabX_id and 
                y.date_val >= x.date_val and 
                (x._next_date_val is null or y.date_val < x.next_date_val))

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.

初雪 2024-12-26 03:15:03

这是一个简单的连接和分组依据:

 select x.TabX_ID, y.DATE, min(ValueX), min(ValueY)
 from TableX x
  join TableY y
    on x.TabX_ID = y.TabX_ID
   and x.DATE = y.DATE
 group by x.TabX_ID, y.DATE

This is a simple join and group by:

 select x.TabX_ID, y.DATE, min(ValueX), min(ValueY)
 from TableX x
  join TableY y
    on x.TabX_ID = y.TabX_ID
   and x.DATE = y.DATE
 group by x.TabX_ID, y.DATE
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文