获取每个客户的电话号码或空(具有最大编辑日期)

时间:2017-09-07 15:09:43

标签: sql oracle date join max

我有2个表:customerphone

表格phone可能包含多个customer的电话号码。

我试图获得具有最大编辑日期的手机。

SELECT customer.customer_name, phone.phone_number
FROM customer
LEFT JOIN phone ON phone.customer_id = customer.customer_id
AND phone.edit_date  =
  (SELECT MAX(edit_date)
  FROM phone ph
  WHERE ph.customer_id = customer.customer_id
  GROUP BY ph.customer_id
  )

我收到了错误

  

ORA-01799:列可能不是外部连接到子查询。

如果有10个客户和40个手机,我必须得到10行才有另外一种方法吗?

3 个答案:

答案 0 :(得分:0)

使用窗口功能:

SELECT c.customer_name, p.phone_number
FROM customer c LEFT JOIN
     (SELECT p.*,
             ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY edit_date DESC) as seqnum
      FROM phone p
     ) p
     ON p.customer_id = c.customer_id and p.seqnum = 1;

答案 1 :(得分:0)

你可以尝试:

with phone_max_edit as  
  (SELECT customer_id, phone_number
     FROM phone ph
    WHERE ph.edit_date  =
      (SELECT MAX(ph2.edit_date)
         FROM phone ph2
        WHERE ph2.customer_id = ph.customer_id
  )
SELECT customer.customer_name, phone_max_edit.phone_number
FROM customer
     LEFT JOIN phone_max_edit 
          ON phone_max_edit.customer_id = customer.customer_id

答案 2 :(得分:0)

首先汇总//############### BENCHMARKS ############### var import_data_res11 []float64 func benchmarkMoving_avg_serial(b *testing.B, window int) { var r []float64 for n := 0; n < b.N; n++ { r = moving_avg_serial(BackTest_res.F["Trading DrawDowns"], window) } import_data_res11 = r } var import_data_res14 []float64 func benchmarkMoving_avg_serial4(b *testing.B, window int) { var r []float64 for n := 0; n < b.N; n++ { r = moving_avg_serial4(BackTest_res.F["Trading DrawDowns"], window) } import_data_res14 = r } var import_data_res16 []float64 func benchmarkMoving_avg_concurrent2(b *testing.B, window, num_goroutines int) { var r []float64 for n := 0; n < b.N; n++ { r = moving_avg_concurrent2(BackTest_res.F["Trading DrawDowns"], window, num_goroutines) } import_data_res16 = r } var import_data_res17 []float64 func benchmarkMoving_avg_concurrent3(b *testing.B, window, num_goroutines int) { var r []float64 for n := 0; n < b.N; n++ { r = moving_avg_concurrent3(BackTest_res.F["Trading DrawDowns"], window, num_goroutines) } import_data_res17 = r } func BenchmarkMoving_avg_serial_261x10(b *testing.B) { benchmarkMoving_avg_serial(b, 261*10) } func BenchmarkMoving_avg_serial4_261x10(b *testing.B) { benchmarkMoving_avg_serial4(b, 261*10) } func BenchmarkMoving_avg_concurrent2_261x10_1(b *testing.B) { benchmarkMoving_avg_concurrent2(b, 261*10, 1) } func BenchmarkMoving_avg_concurrent2_261x10_8(b *testing.B) { benchmarkMoving_avg_concurrent2(b, 261*10, 8) } func BenchmarkMoving_avg_concurrent3_261x10_1(b *testing.B) { benchmarkMoving_avg_concurrent3(b, 261*10, 1) } func BenchmarkMoving_avg_concurrent3_261x10_8(b *testing.B) { benchmarkMoving_avg_concurrent3(b, 261*10, 8) } //############### BENCHMARKS end ############### 表中的数据;使用PHONE聚合函数(许多从业者忽略的一个非常有用的函数 - 甚至是高级函数,如本和其他线程中发布的答案所示)。 https://docs.oracle.com/cd/E11882_01/server.112/e41084/functions083.htm#SQLRF00653

然后在代码中添加LAST

LEFT JOIN