Oracle SQL – 基于值条件连接表格

huangapple go评论58阅读模式
英文:

Oracle SQL- Joining Tables Based on Value Conditions

问题

我正在寻找一种根据表格值之间的条件将这些表格相加的方法。以下是我尝试做的可视化示意图:

Oracle SQL – 基于值条件连接表格

目标是根据表格2中的测深值与正确的序列号相结合,这将与表格1中的起始深度和结束深度相关联,并且需要应用于表格2中的测深值,基于起始/结束深度的边界。这是否可行?以下是我正在使用的语句:

select 
      Table2.location,
      Table2.Measured_Dpth, 
      Table1.sequence_number,
      Table1.Strt_dpth, 
      Table1.end_dpth
from Table1
join Table2 on (Table1.location = table2.location 
     and (Table2.measured_dpth >= Table1.Strt_dpth and Table2.measured_dpth <= Table1.end_dpth))

**注意:请原谅我的语法,我不是SQL专家,正在学习过程中- 感谢您的帮助和建议!

英文:

I'm looking for a way to add these tables together based on conditions between the table's values. Here's a visual for what I'm trying to do:

Oracle SQL – 基于值条件连接表格

The goal is to combine the two tables with the correct corresponding sequence number, based on the Measured Depth value in table 2. The Sequence Number will correlate to the Start and End Depth from Table 1 and needs to be applied to the Measured Depth in Table 2, based on it's value and boundaries of the Start/End Depths. Is this possible? Here's my statement that I'm working with:
<br>

select 
      Table2.location,
      Table2.Measured_Dpth, 
      Table1.sequence_number,
      Table1.Strt_dpth, 
      Table1.end_dpth
      
     join Table2 on (Table1.location =table2.location 
     and (Table2.measured_dpth&gt;= Table3.Strt_dpth and Table2.measured_dpth&lt;= Table3.end_depth) )

**Note: Please forgive my syntax, I'm not a pro at SQL and am in the learning process- appreciate the help and advice!

答案1

得分: 0

表格1中的深度边界设置错误。您不能(即 不应该) 将相同的深度设置为起始深度和结束深度,所以我在示例数据中进行了修复。

SQL> with
  2  table1 (location, seq_number, start_Depth, end_Depth) as
  3    (select 'X', 1,  100,  300 from dual union all
  4     select 'X', 2,  301,  600 from dual union all
  5     select 'X', 3,  601,  850 from dual union all
  6     select 'X', 4,  851, 1000 from dual union all
  7     select 'X', 5, 1001, 1500 from dual union all
  8     select 'X', 6, 1501, 2000 from dual
  9    ),
 10  table2 (location, measured_depth) as
 11    (select 'X',  150 from dual union all
 12     select 'X',  400 from dual union all
 13     select 'X',  850 from dual union all
 14     select 'X',  900 from dual union all
 15     select 'X', 1100 from dual union all
 16     select 'X', 1175 from dual
 17    )

Query begins here:

 18  select b.location, b.measured_depth, a.seq_number
 19  from table2 b join table1 a on a.location = b.location
 20                             and b.measured_Depth between a.start_depth and a.end_Depth;
    
LOCATION   MEASURED_DEPTH SEQ_NUMBER
---------- -------------- ----------
X                     150          1
X                     400          2
X                     850          3
X                     900          4
X                    1100          5
X                    1175          5
    
6 rows selected.

SQL>;

[编辑]

如果无法修复边界,那么 - 而不是使用 between - 可以比较每个深度:

 18     select b.location, b.measured_depth, a.seq_number
 19     from table2 b join table1 a on a.location = b.location
 20                                and b.measured_depth > a.start_depth
 21                                and b.measured_depth <= a.end_depth;
    
LOCATION   MEASURED_DEPTH SEQ_NUMBER
---------- -------------- ----------
X                     150          1
X                     400          2
X                     850          3
X                     900          4
X                    1100          5
X                    1175          5
    
6 rows selected.

SQL>;
英文:

Depth boundaries in table 1 are wrongly set. You can't (i.e. shouldn't) have the same depth set as start and end depth, so I kind of fixed that in my sample data.

SQL&gt; with
  2  table1 (location, seq_number, start_Depth, end_Depth) as
  3    (select &#39;X&#39;, 1,  100,  300 from dual union all
  4     select &#39;X&#39;, 2,  301,  600 from dual union all
  5     select &#39;X&#39;, 3,  601,  850 from dual union all
  6     select &#39;X&#39;, 4,  851, 1000 from dual union all
  7     select &#39;X&#39;, 5, 1001, 1500 from dual union all
  8     select &#39;X&#39;, 6, 1501, 2000 from dual
  9    ),
 10  table2 (location, measured_depth) as
 11    (select &#39;X&#39;,  150 from dual union all
 12     select &#39;X&#39;,  400 from dual union all
 13     select &#39;X&#39;,  850 from dual union all
 14     select &#39;X&#39;,  900 from dual union all
 15     select &#39;X&#39;, 1100 from dual union all
 16     select &#39;X&#39;, 1175 from dual
 17    )

Query begins here:

 18  select b.location, b.measured_depth, a.seq_number
 19  from table2 b join table1 a on a.location = b.location
 20                             and b.measured_Depth between a.start_depth and a.end_Depth;

LOCATION   MEASURED_DEPTH SEQ_NUMBER
---------- -------------- ----------
X                     150          1
X                     400          2
X                     850          3
X                     900          4
X                    1100          5
X                    1175          5

6 rows selected.

SQL&gt;

[EDIT]

If you can't fix boundaries, then - instead of between compare each depth:

 18     select b.location, b.measured_depth, a.seq_number
 19     from table2 b join table1 a on a.location = b.location
 20                                and b.measured_depth &gt; a.start_Depth
 21                                and b.measured_depth &lt;= a.end_depth;

LOCATION   MEASURED_DEPTH SEQ_NUMBER
---------- -------------- ----------
X                     150          1
X                     400          2
X                     850          3
X                     900          4
X                    1100          5
X                    1175          5

6 rows selected.

SQL&gt;

答案2

得分: 0

使深度范围的下限不包含在内:

SELECT t2.*, t1.sequence_number
FROM   table2 t2
       INNER JOIN table1 t1
       ON (   t1.location       =  t2.location
          AND t1.start_depth    <  t2.measured_depth
          AND t2.measured_depth <= t1.end_depth );

然后,对于示例数据:

CREATE TABLE table1 (location, sequence_number, start_depth, end_depth) AS
SELECT 'X', 1,  100,  300 FROM DUAL UNION ALL
SELECT 'X', 2,  300,  600 FROM DUAL UNION ALL
SELECT 'X', 3,  600,  850 FROM DUAL UNION ALL
SELECT 'X', 4,  850, 1000 FROM DUAL UNION ALL
SELECT 'X', 5, 1000, 1500 FROM DUAL UNION ALL
SELECT 'X', 6, 1500, 2000 FROM DUAL;

CREATE TABLE table2 (dt, location, value, measured_depth) AS
SELECT DATE '2023-01-01', 'X', 0.01,  150 FROM DUAL UNION ALL
SELECT DATE '2023-01-01', 'X', 0.02,  400 FROM DUAL UNION ALL
SELECT DATE '2023-01-05', 'X', 0.02,  850 FROM DUAL UNION ALL
SELECT DATE '2023-01-06', 'X', 0.01,  900 FROM DUAL UNION ALL
SELECT DATE '2023-01-06', 'X', 0.02, 1100 FROM DUAL UNION ALL
SELECT DATE '2023-01-06', 'X', 0.02, 1175 FROM DUAL;

输出:

DT LOCATION VALUE MEASURED_DEPTH SEQUENCE_NUMBER
2023-01-01 00:00:00 X 0.01 150 1
2023-01-01 00:00:00 X 0.02 400 2
2023-01-05 00:00:00 X 0.02 850 3
2023-01-06 00:00:00 X 0.01 900 4
2023-01-06 00:00:00 X 0.02 1100 5
2023-01-06 00:00:00 X 0.02 1175 5

fiddle

英文:

Make the lower bound of the depth range non-inclusive:

SELECT t2.*, t1.sequence_number
FROM   table2 t2
       INNER JOIN table1 t1
       ON (   t1.location       =  t2.location
          AND t1.start_depth    &lt;  t2.measured_depth
          AND t2.measured_depth &lt;= t1.end_depth );

Then, for the sample data:

CREATE TABLE table1 (location, sequence_number, start_depth, end_depth) AS
SELECT &#39;X&#39;, 1,  100,  300 FROM DUAL UNION ALL
SELECT &#39;X&#39;, 2,  300,  600 FROM DUAL UNION ALL
SELECT &#39;X&#39;, 3,  600,  850 FROM DUAL UNION ALL
SELECT &#39;X&#39;, 4,  850, 1000 FROM DUAL UNION ALL
SELECT &#39;X&#39;, 5, 1000, 1500 FROM DUAL UNION ALL
SELECT &#39;X&#39;, 6, 1500, 2000 FROM DUAL;

CREATE TABLE table2 (dt, location, value, measured_depth) AS
SELECT DATE &#39;2023-01-01&#39;, &#39;X&#39;, 0.01,  150 FROM DUAL UNION ALL
SELECT DATE &#39;2023-01-01&#39;, &#39;X&#39;, 0.02,  400 FROM DUAL UNION ALL
SELECT DATE &#39;2023-01-05&#39;, &#39;X&#39;, 0.02,  850 FROM DUAL UNION ALL
SELECT DATE &#39;2023-01-06&#39;, &#39;X&#39;, 0.01,  900 FROM DUAL UNION ALL
SELECT DATE &#39;2023-01-06&#39;, &#39;X&#39;, 0.02, 1100 FROM DUAL UNION ALL
SELECT DATE &#39;2023-01-06&#39;, &#39;X&#39;, 0.02, 1175 FROM DUAL;

Outputs:

DT LOCATION VALUE MEASURED_DEPTH SEQUENCE_NUMBER
2023-01-01 00:00:00 X .01 150 1
2023-01-01 00:00:00 X .02 400 2
2023-01-05 00:00:00 X .02 850 3
2023-01-06 00:00:00 X .01 900 4
2023-01-06 00:00:00 X .02 1100 5
2023-01-06 00:00:00 X .02 1175 5

fiddle

huangapple
  • 本文由 发表于 2023年7月7日 02:26:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76631608.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定