[英]How to select matching records in two different columns in same table?
I have two table like below, and I need below output.我有两个如下表,我需要下面的输出。 I have around millions of records so I cannot hard code
value="***"
:我有大约数百万条记录,所以我不能硬编码
value="***"
:
Col1 Col2 Col1_dtm Col2_dtm
a a 01/01/1900 03/01/1900
b c 01/01/1900 04/01/1900
c1 b 01/01/1900 02/01/1900
d1 g 01/01/1900 01/01/1900
e1 f 01/01/1900 06/12/1900
f d 01/01/1900 05/01/1900
c 01/01/1900 01/01/1900
d 01/01/1900 01/01/1900
e 01/01/1900 01/01/1900
g 01/01/1900 01/01/1900
I need output as below:我需要如下输出:
Col1 Col2 Col2_dtm-col1_dtm
a a 59 (days)
b b 31 (days)
c c 90 (days)
d d 120 (days)
f f 162 (days)
g g 0 (days)
Try this:尝试这个:
select t1.col1,
t2.col2,
datediff(day, t1.col1_dtm, t2.col2_dtm)
from my_table t1
join my_table t2 on t1.col1 = t2.col2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.