2014-02-05 58 views
0

我有兩個表,我想更新另一個表中的日期相等的列的一個表的列。這裏的問題是兩張表中的日期不同。從SQL Server中的另一個錶轉換數據後的更新表?

SELECT UploadedDateTime = LEFT(CAST(UploadedDateTime AS DATETIME), 11), 
     COUNT(DISTINCT ClientID) AS 'DocsCount' 
FROM ClientUploadedTaxDocuments 
WHERE CONVERT(DATETIME, CONVERT(NVARCHAR(10), UploadedDateTime, 101)) > '1/1/2014' 
GROUP BY LEFT(CAST(UploadedDateTime AS DATETIME), 11) 
ORDER BY LEFT(CAST(UploadedDateTime AS DATETIME), 11) 

出把上面的查詢是,

1 .. enter image description here

SELECT * FROM FilesReceivedReport

2 .. enter image description here

在這裏,我想更新Y2014(列第二個表格)與DocsCount(第一個表格的列)的日期相等。

謝謝

+1

嘗試都轉換日期相同的格式? – Raj

+1

你可以把查詢創建你的表並插入一些示例行嗎? –

+0

@Raj - 我認爲這個問題值得讚賞。你說什麼 ? –

回答

0

首先,給第一個選定的列一些不錯的名字。加上「日」,因此它成爲

SELECT UploadedDateTime = LEFT(CAST(UploadedDateTime AS DATETIME), 11) AS 'Date', 
    COUNT(DISTINCT ClientID) AS 'DocsCount' 
FROM ClientUploadedTaxDocuments 
WHERE CONVERT(DATETIME, CONVERT(NVARCHAR(10), UploadedDateTime, 101)) > '1/1/2014' 
GROUP BY LEFT(CAST(UploadedDateTime AS DATETIME), 11) 
ORDER BY LEFT(CAST(UploadedDateTime AS DATETIME), 11) 

然後,嘗試這樣的事:

UPDATE f set Y2014 = s.DocsCount FROM FilesReceivedReport AS f 
INNER JOIN (your_complicated_select) AS s 
ON f.D == s.Date 
相關問題