2012-10-17 36 views
0
SELECT TOP 1 * FROM 
(
    select max(updatedtimestamp) from db1.dbo.policyrepresentationcache with(nolock) 
    UNION 
    select max(updatedtimestamp) from db2.dbo.policyrepresentationcache with(nolock) 
    ) 
ORDER BY updatedtimestamp 

我在這裏遇到語法錯誤,我不知道如何設置它。如何從兩個表格的聯合中選擇最大日期?

回答

3

你只是缺少聯盟產生的表的別名。我已將其別名爲a以下。

SELECT TOP 1 a.updatedtimestamp FROM 
(
    select max(updatedtimestamp) as updatedtimestamp 
    from db1.dbo.policyrepresentationcache with(nolock) 
    UNION 
    select max(updatedtimestamp) 
    from db2.dbo.policyrepresentationcache with(nolock) 
) a 
ORDER BY a.updatedtimestamp 
+0

它給我一個無效的對象名稱「updatedtimestamp」 – Testifier

+0

我仔細檢查過拼寫 - 你一定要包括聯合聲明的身體嗎?請注意,我在聯合聲明中的第一列用了別名,以便您可以按順序包含它。 –

+0

我明白了。謝謝! :) – Testifier

0
SELECT TOP 1 Temp.* FROM 
(
    select max(updatedtimestamp) from db1.dbo.policyrepresentationcache with(nolock) 
    UNION 
    select max(updatedtimestamp) from db2.dbo.policyrepresentationcache with(nolock) 
    ) AS Temp 
ORDER BY Temp.updatedtimestamp 
相關問題