我有两个具有以下格式的表
**Table1**
"date_Time1"
"2018-09-13 04:27:00.000"
"2018-09-13 04:28:00.000"
**Table2**
"date_Time2"
"2018-09-13 04:27:00.000"
"2018-09-13 04:29:00.000"
现在,无论日期时间是否匹配,我都想比较这两个表。 结果应为
+--------------------------+---------+
| Date_Time | Is_match|
+--------------------------+---------+
|"2018-09-13 04:27:00.000" | True |
|"2018-09-13 04:28:00.000" | False |
|"2018-09-13 04:29:00.000" | False |
+-----------------------------------+
答案 0 :(得分:0)
尝试一下:
SELECT ISNULL(t1.Date_Time,t2.Date_Time) AS Date_Time,
CASE WHEN t1.Date_Time=t2.Date_Time THEN 'True' ELSE 'False' END AS Is_Match
FROM Table1 t1
OUTER JOIN Table2 t2 ON t1.Date_Time=t2.Date_Time
答案 1 :(得分:0)
您可以在下面的查询中尝试
{{1}}
答案 2 :(得分:0)
另一种可能的方法,该方法将在两个表之间查找重复的日期,并在每个表中查找可能的重复的日期:
WITH cte (date_time) AS (
SELECT date_time1
FROM DateTimeTable1
UNION ALL
SELECT date_time2
FROM DateTimeTable2
)
SELECT
date_time,
CASE
WHEN COUNT(*) > 1 THEN 'True'
ELSE 'False'
END AS is_match
FROM cte
GROUP BY date_time
ORDER BY date_time