February 10, 2013 at 11:02 pm
Master table
ComputerName. AuditId. AuditDate.
A. 111. 1/1/13
A. 222. 2/1/13
A. 000. 2/1/13
B. 111. 1/1/13
C. 000. 1/1/13
C. 111. 2/1/13
AuditId equals to zero telling no vulnerability found on that day scan.
I need to write a query to move computer A to different table because there is no vulnerable auditId found after the 2/1/13.
Computer C won't be moved because there is another vulnerability in the next day. Please help. Thank you
February 11, 2013 at 6:06 am
Is this what you're looking for?
insert into different_table(computername, auditid, auditdate)
select computername, auditid, auditdate
from original_table a1
where auditdate = (select max(auditdate)
from original_table a2
where a2.computername = a1.computername)
and auditid = '000';
This will move the row with auditid '000' to different_table. You can then delete it from original_table.
February 11, 2013 at 6:14 am
huy1002 (2/10/2013)
Master tableComputerName. AuditId. AuditDate.
A. 111. 1/1/13
A. 222. 2/1/13
A. 000. 2/1/13
B. 111. 1/1/13
C. 000. 1/1/13
C. 111. 2/1/13
AuditId equals to zero telling no vulnerability found on that day scan.
I need to write a query to move computer A to different table because there is no vulnerable auditId found after the 2/1/13.
Computer C won't be moved because there is another vulnerability in the next day. Please help. Thank you
Use DELETE with an OUTPUT clause. If you can post ddl and dml for both tables as shown in the link in my sig ("please read this"), someone will show you how.
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
February 11, 2013 at 8:59 am
Hi Eights, I want to move all the scans that are related to computer A as well, because there are no other vulnerability found after the day it is scanned with code 000.
February 11, 2013 at 9:02 am
Ed Wagner (2/11/2013)
Is this what you're looking for?
insert into different_table(computername, auditid, auditdate)
select computername, auditid, auditdate
from original_table a1
where auditdate = (select max(auditdate)
from original_table a2
where a2.computername = a1.computername)
and auditid = '000';
This will move the row with auditid '000' to different_table. You can then delete it from original_table.
This might be simpler:
DELETE FROM dbo.SourceTable
OUTPUT deleted.column1, deleted.column2, deleted.column3
INTO dbo.DestinationTable
WHERE 0 = 1 -- or whatever
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
February 11, 2013 at 9:24 am
Much better than my suggestion - I didn't even consider that. I love it! Thanks.
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply