简体   繁体   English

如何在同一个表的两个不同列中选择匹配的记录?

[英]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.

相关问题 如何在同一表上选择具有相同ID和名称的两个匹配记录 - How to select both matching records on same table with same ID and Name 选择1条记录,其中两个记录在不同的列中具有相同的值 - Select 1 record where two records have the the same values in different columns 如何在MySQL的同一张表上的两个不同输入值之间的两个不同列之间选择行 - How to select rows between two different columns with two different input values on same table on mysql 如何从具有匹配列的两个不同 SELECT 语句中获取行 - How to get rows from two different SELECT statements with matching columns 如何将不同记录中的记录与两个不同列中的匹配数据排序为输出中的连续行 - How to order the records from different records with matching data in two differents columns as continous rows in output 甲骨文 SQL。 在同一表或不同表的两个不同列和不同行中查找匹配值 - Oracle SQL. Find Matching values in two different columns and different rows from same table or different one Select 在不同列中具有相同 email 的记录 - Select Records with same email in different columns 两个共有2列的SQL表需要在表A中但不在表B中的记录(列1相同,但列2不同) - two SQL tables with 2 columns in common, need records that are in table A but not in table B (column 1 is same in both but different column 2) MYSQL从同一个表中选择不同的记录 - MYSQL Select different records from same table 如何在Select语句中显示与两列相同的同一行的不同行? - How to show different rows of same column as two columns in Select statement?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM