-1

我有两个表,提交和更新。每次更新提交时,旧值都会保存在更新表中,新值会写入提交中。我需要的是一个查询,它需要一个日期范围,通常是上个月,并返回 submit_id、已更改的列的名称以及列的新旧值。我该怎么办?

我基本上需要创建一个如下所示的报告:

ID    Name     Field Updated     Old Value   New Value
1123  Jim Bob  policyno          515-231     515-321
               zip               77070       77077
1157  John Doe address           123 Main St 122 Main St

CREATE TABLE [dbo].[submissions](
    [submission_id] [int] IDENTITY(100000,1) NOT NULL,
    [policyno] [nvarchar](50) NULL,
    [loanno] [nvarchar](50) NULL,
    [banker] [nvarchar](50) NULL,
    [effdate] [smalldatetime] NOT NULL,
    [name] [nvarchar](128) NULL,
    [name2] [nvarchar](128) NULL,
    [address] [nvarchar](128) NULL,
    [address2] [nvarchar](128) NULL,
    [city] [nvarchar](25) NULL,
    [state] [nvarchar](2) NULL,
    [zip] [nvarchar](5) NULL,
    [county] [nvarchar](50) NULL,
[LastUpdatedBy] [nvarchar](25) NULL,
[LastUpdatedDate] [smalldatetime] NULL)

CREATE TABLE [dbo].[updates](
    [updates_id] [int] IDENTITY(10000,1) NOT NULL,
    [submission_id] [int] NULL,
    [policyno] [nvarchar](50) NULL,
    [loanno] [nvarchar](50) NULL,
    [banker] [nvarchar](50) NULL,
    [effdate] [smalldatetime] NOT NULL,
    [name] [nvarchar](128) NULL,
    [name2] [nvarchar](128) NULL,
    [address] [nvarchar](128) NULL,
    [address2] [nvarchar](128) NULL,
    [city] [nvarchar](25) NULL,
    [state] [nvarchar](2) NULL,
    [zip] [nvarchar](5) NULL,
    [county] [nvarchar](50) NULL,
[LastUpdatedBy] [nvarchar](25) NULL,
[LastUpdatedDate] [smalldatetime] NULL)
4

1 回答 1

1

我使用 SQL 编写 SQL。通过执行此语句:

select 
'select n.updates_id as ID, 
n.submission_id as SubmissionID, 
n.Name as name, 
'''+sc.name+''' as [Field Updated], 
convert(varchar(max), o.'+sc.name+') as [Old Value],
convert(varchar(max), n.'+sc.name+') as [New Value] 
from orderedUpdates o 
inner join orderedUpdates n 
on o.submission_id = n.submission_id
and o.rowId = n.rowId + 1
where n.effDate > dateadd(month,-1,getdate())
and  (o.'+sc.name+' <> n.'+sc.name+' OR o.'+sc.name+' is null and n.'+sc.name+' is not null OR o.'+sc.name+' is not null and n.'+sc.name+' is null)  UNION '  
from sysobjects so inner join syscolumns sc 
on so.id = sc.id
where so.name = 'updates'

我能够为 Updates 表上的每一列生成一个 UNION 语句。然后将该列表配对并将其与 CTE 组合产生以下结果:

with orderedUpdates(rowId,updates_id,submission_id,policyno,
loanno,
banker,
effdate,
name,
name2,
address,
address2,
city,
state,
zip,
county)
as
(
    select Rank() over(partition by submission_id order by updates_id), updates_id, submission_id,policyno,
        loanno,
        banker,
        effdate,
        name,
        name2,
        address,
        address2,
        city,
        state,
        zip,
        county
    from updates

)
select n.updates_id as ID,   n.submission_id as SubmissionID,   n.Name as name,   'policyno' as [Field Updated],   convert(varchar(max), o.policyno) as [Old Value],  convert(varchar(max), n.policyno) as [New Value]   from orderedUpdates o   inner join orderedUpdates n   on o.submission_id = n.submission_id  and o.rowId = n.rowId + 1  where n.effDate > dateadd(month,-1,getdate())  and  (o.policyno <> n.policyno OR o.policyno is null and n.policyno is not null OR o.policyno is not null and n.policyno is null)  UNION 
select n.updates_id as ID,   n.submission_id as SubmissionID,   n.Name as name,   'loanno' as [Field Updated],   convert(varchar(max), o.loanno) as [Old Value],  convert(varchar(max), n.loanno) as [New Value]   from orderedUpdates o   inner join orderedUpdates n   on o.submission_id = n.submission_id  and o.rowId = n.rowId + 1  where n.effDate > dateadd(month,-1,getdate())  and  (o.loanno <> n.loanno OR o.loanno is null and n.loanno is not null OR o.loanno is not null and n.loanno is null)  UNION 
select n.updates_id as ID,   n.submission_id as SubmissionID,   n.Name as name,   'banker' as [Field Updated],   convert(varchar(max), o.banker) as [Old Value],  convert(varchar(max), n.banker) as [New Value]   from orderedUpdates o   inner join orderedUpdates n   on o.submission_id = n.submission_id  and o.rowId = n.rowId + 1  where n.effDate > dateadd(month,-1,getdate())  and  (o.banker <> n.banker OR o.banker is null and n.banker is not null OR o.banker is not null and n.banker is null)  UNION 
select n.updates_id as ID,   n.submission_id as SubmissionID,   n.Name as name,   'effdate' as [Field Updated],   convert(varchar(max), o.effdate) as [Old Value],  convert(varchar(max), n.effdate) as [New Value]   from orderedUpdates o   inner join orderedUpdates n   on o.submission_id = n.submission_id  and o.rowId = n.rowId + 1  where n.effDate > dateadd(month,-1,getdate())  and  (o.effdate <> n.effdate OR o.effdate is null and n.effdate is not null OR o.effdate is not null and n.effdate is null)  UNION 
select n.updates_id as ID,   n.submission_id as SubmissionID,   n.Name as name,   'name' as [Field Updated],   convert(varchar(max), o.name) as [Old Value],  convert(varchar(max), n.name) as [New Value]   from orderedUpdates o   inner join orderedUpdates n   on o.submission_id = n.submission_id  and o.rowId = n.rowId + 1  where n.effDate > dateadd(month,-1,getdate())  and  (o.name <> n.name OR o.name is null and n.name is not null OR o.name is not null and n.name is null)  UNION 
select n.updates_id as ID,   n.submission_id as SubmissionID,   n.Name as name,   'name2' as [Field Updated],   convert(varchar(max), o.name2) as [Old Value],  convert(varchar(max), n.name2) as [New Value]   from orderedUpdates o   inner join orderedUpdates n   on o.submission_id = n.submission_id  and o.rowId = n.rowId + 1  where n.effDate > dateadd(month,-1,getdate())  and  (o.name2 <> n.name2 OR o.name2 is null and n.name2 is not null OR o.name2 is not null and n.name2 is null)  UNION 
select n.updates_id as ID,   n.submission_id as SubmissionID,   n.Name as name,   'address' as [Field Updated],   convert(varchar(max), o.address) as [Old Value],  convert(varchar(max), n.address) as [New Value]   from orderedUpdates o   inner join orderedUpdates n   on o.submission_id = n.submission_id  and o.rowId = n.rowId + 1  where n.effDate > dateadd(month,-1,getdate())  and  (o.address <> n.address OR o.address is null and n.address is not null OR o.address is not null and n.address is null)  UNION 
select n.updates_id as ID,   n.submission_id as SubmissionID,   n.Name as name,   'address2' as [Field Updated],   convert(varchar(max), o.address2) as [Old Value],  convert(varchar(max), n.address2) as [New Value]   from orderedUpdates o   inner join orderedUpdates n   on o.submission_id = n.submission_id  and o.rowId = n.rowId + 1  where n.effDate > dateadd(month,-1,getdate())  and  (o.address2 <> n.address2 OR o.address2 is null and n.address2 is not null OR o.address2 is not null and n.address2 is null)  UNION 
select n.updates_id as ID,   n.submission_id as SubmissionID,   n.Name as name,   'city' as [Field Updated],   convert(varchar(max), o.city) as [Old Value],  convert(varchar(max), n.city) as [New Value]   from orderedUpdates o   inner join orderedUpdates n   on o.submission_id = n.submission_id  and o.rowId = n.rowId + 1  where n.effDate > dateadd(month,-1,getdate())  and  (o.city <> n.city OR o.city is null and n.city is not null OR o.city is not null and n.city is null)  UNION 
select n.updates_id as ID,   n.submission_id as SubmissionID,   n.Name as name,   'state' as [Field Updated],   convert(varchar(max), o.state) as [Old Value],  convert(varchar(max), n.state) as [New Value]   from orderedUpdates o   inner join orderedUpdates n   on o.submission_id = n.submission_id  and o.rowId = n.rowId + 1  where n.effDate > dateadd(month,-1,getdate())  and  (o.state <> n.state OR o.state is null and n.state is not null OR o.state is not null and n.state is null)  UNION 
select n.updates_id as ID,   n.submission_id as SubmissionID,   n.Name as name,   'zip' as [Field Updated],   convert(varchar(max), o.zip) as [Old Value],  convert(varchar(max), n.zip) as [New Value]   from orderedUpdates o   inner join orderedUpdates n   on o.submission_id = n.submission_id  and o.rowId = n.rowId + 1  where n.effDate > dateadd(month,-1,getdate())  and  (o.zip <> n.zip OR o.zip is null and n.zip is not null OR o.zip is not null and n.zip is null)  UNION 
select n.updates_id as ID,   n.submission_id as SubmissionID,   n.Name as name,   'county' as [Field Updated],   convert(varchar(max), o.county) as [Old Value],  convert(varchar(max), n.county) as [New Value]   from orderedUpdates o   inner join orderedUpdates n   on o.submission_id = n.submission_id  and o.rowId = n.rowId + 1  where n.effDate > dateadd(month,-1,getdate())  and  (o.county <> n.county OR o.county is null and n.county is not null OR o.county is not null and n.county is null )
order by 1,2
于 2013-06-06T17:34:22.487 回答