简体   繁体   中英

Update table after converting data from another table in SQL Server?

i have two tables, i want to update column of one table with column of another table where date is equal in both tables. Here problems is date in two tables is different.

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) 

out put of above query is,

1.. 在此处输入图片说明

select * from FilesReceivedReport

2.. 在此处输入图片说明

here i want to update Y2014(Column of 2nd table) with DocsCount(Column of 1st table) where date is equal.

Thanks

First, give the first selected column some nice name. Add "AS date", so it becomes

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) 

Then, try something like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM