1
我有兩個表的內容:SQL返回同時匹配與來自其他表
表時間表(具有固定的內容)
Value |
-------
1
2
3
4
5
6
7
表PROD_ENTRY(從時間到時間條目)
entry_1 | entry_2 | entry_3 |
---------------------------------------------------
2017/09/25 01:25:00 1 aaa
2017/09/25 01:26:00 1 bbb
2017/09/25 03:32:00 2 ccc
2017/09/25 04:15:00 3 ddd
2017/09/25 05:05:00 5 eee
2017/09/26 13:25:00 7 fff
我想表總是返回格式,而表B項匹配到返回
new_value | entry_time | entry_2 | entry_3 |
----------------------------------------------------------
1 2017/09/25 01:26:00 1 bbb
2 2017/09/25 03:32:00 2 ccc
3 2017/09/25 04:15:00 3 ddd
4 null null null
5 2017/09/25 05:05:00 5 eee
6 null null null
7 null null null
以下是我的代碼:
SELECT
coalesce(T1.entry_2, T2.Value) as timecode ,
T1.*
FROM
(SELECT tt.* ----
FROM prod_entry tt |
INNER JOIN |
(SELECT entry_2, MAX(entry_time) AS MaxDateTime |- to remove duplicated entry_2
FROM prod_entry |
GROUP BY entry_2) newtt |
ON tt.entry_2 = groupedtt.entry_2 |
AND tt.entry_time = newtt.MaxDateTime) T1 ----
FULL OUTER JOIN TimeLine T2
on T1.entry_3 = T2.Value
WHERE (T1.entry_3 is null or T2.Value is null)
OR
T1.entry_time > '2017-09-25 00:00:00' AND T1.entry_time < '2017-09-25 23:59:00' AND T1.entry_1 = '1'
order by timecode
但是我得到以下,以NEW_VALUE「7」缺少
new_value | entry_1 | entry_2 | entry_3 |
----------------------------------------------------------
1 2017/09/25 01:26:00 1 bbb
2 2017/09/25 03:32:00 2 ccc
3 2017/09/25 04:15:00 3 ddd
4 null null null
5 2017/09/25 05:05:00 5 eee
6 null null null
我可以知道我在刪除重複做哪部分出錯誤?
您正在使用哪個版本的SQL? –
您好Tim,我正在SSRS 2008上運行此查詢 – jctan
Left justified SQL ...很難閱讀... – jarlh