Quantcast
Channel: SQLServerCentral » SQL Server 2014 » Development - SQL Server 2014 » Latest topics
Viewing all articles
Browse latest Browse all 3145

How to select record that exist in other table

$
0
0
I have two tables [quote]CREATE TABLE [dbo].[StatusAudit]( [RowID] [int] IDENTITY(1,1) NOT NULL primary Key, [AccountID] [varchar](20) NOT NULL, [Status] [int] NOT NULL, [AuditDte] [datetime] NOT NULL)[/quote]and [quote]CREATE TABLE [dbo].[Status]( [AccountID] [varchar](20) NOT NULL primary Key, [Status] [int] NOT NULL, [AuditDte] [datetime] NOT NULL) [/quote]and this is a process, the data is loaded into Status table, which will always have one record, when the Order status change, the old Status goes into StatusAudit table. Let's say the Status was 5, then changes to 2, we will only have 2 in Status table and 5 into StatusAudit. when the 2 in Status table changes into 7, we will have 7 in the Status table and 5 and 2 in the Status Audit.Now I have to move data from Status and StatusAudit to Dim_Table. the process is, we load StatusAudit Table as it is, but before we load Status Table, we check first if the Status value in the Status Table does exist in the StatusAudit table, If it does, don't load it into the Dim(because we loaded StatusAudit already, so that value will be loaded as well), if it doesn't then load it to the Dim_Table. This is what I have tried to do:[quote]CREATE TABLE ##NewRecords( AccountID int ,Status int ,AuditDte datetime)insert into ##NewRecordsselect AccountID ,DEAStatus ,Auditdte = convert(datetime, Auditdte, 121) FROM dbo.StatusAudit insert into ##NewRecordsselect a.AccountID ,a.DEAStatus ,Auditdte = convert(datetime, a.Auditdte, 121)from dbo.Status ainner join ##NewRecords bon a.AccountID = b.AccountIDwhere a.Auditdte = b.AuditDteand a.Status not in (select Status from ##NewRecords c where c.AccountID = a.AccountID)The second part of my query gives me problems, I don't know where I'm getting it wrong[/quote]

Viewing all articles
Browse latest Browse all 3145

Trending Articles