代码之家  ›  专栏  ›  技术社区  ›  Nix

SQL Server将类似表与查询进行比较

  •  1
  • Nix  · 技术社区  · 14 年前

    简单的概念,我们基本上是做一些审计,比较什么进来,什么实际发生的过程中处理。我正在寻找一种更好的方法来执行查询,该查询可以与名称和潜在类型略有不同的列进行并排表比较。

    日志 (未更改的数据记录。)
    -逻辑标识
    -记录ID*
    -姓名


    -产品
    -等等。

    审计 (后处理记录)
    -卡迪德*
    -肉身
    -部署日期

    -选项
    -等等。

    如果您忽略了烦人的编写复杂性和性能问题,就可以工作。

    查询只是连接左右两个元素,并将它们选作字符串。显示匹配的每个字段。

    select 
      cast(log.RecordID as varchar(40)) + '=' + cast(audit.CardID as varchar(40),
      log.Name+ '=' + audit.Name ,
      cast(log.Date as varchar(40)) + '=' + cast(audit.DeploymentDate as varchar(40), 
      log.Address + '=' + audit.ShippingAddress,
      log.Products+ '=' + audit.Options
      --etc
    from Audit audit, Log log
      where audit.CardID=log.RecordId
    

    1=1 Test=TestName 11/09/2009=11/10/2009 null=我的地址null=车轮

    这是工作,但是非常烦人的建设。我想到的另一件事就是给列起别名,合并两个表,并对它们进行排序,使它们成为列表形式。这将允许我查看列比较。这显然伴随着工会的开销。

    即:


    对如何更好地审核这些数据有什么建议吗?

    告诉我你还有什么问题。

    附加说明。我们将要减少不重要的信息,因此在某些情况下,如果它们相等,我们可能会使列为空(但我知道它太慢)

      case when log.[Name]<>audit.[CarName] then (log.[Name] + '!=' + audit.[CarName]) else null end
    

    或者如果我们用第二种方法

      nullif(log.[Name], audit.[CarName]) as [Name]
      ,nullif(audit.[CarName], log.[Name]) as [Name]
    
    2 回复  |  直到 14 年前
        1
  •  2
  •   Joe Stefanelli    14 年前

    我找到了既定的惯例 here 由jeffsmith编写,有助于在过去进行表格比较。这至少可以给你一个良好的基础。该链接上给出的代码是:

    CREATE PROCEDURE CompareTables(@table1 varchar(100), 
        @table2 Varchar(100), @T1ColumnList varchar(1000),
        @T2ColumnList varchar(1000) = '')
    AS
    
    -- Table1, Table2 are the tables or views to compare.
    -- T1ColumnList is the list of columns to compare, from table1.
    -- Just list them comma-separated, like in a GROUP BY clause.
    -- If T2ColumnList is not specified, it is assumed to be the same
    -- as T1ColumnList.  Otherwise, list the columns of Table2 in
    -- the same order as the columns in table1 that you wish to compare.
    --
    -- The result is all records from either table that do NOT match
    -- the other table, along with which table the record is from.
    
    declare @SQL varchar(8000);
    
    IF @t2ColumnList = '' SET @T2ColumnList = @T1ColumnList
    
    set @SQL = 'SELECT ''' + @table1 + ''' AS TableName, ' + @t1ColumnList +
     ' FROM ' + @Table1 + ' UNION ALL SELECT ''' + @table2 + ''' As TableName, ' +
     @t2ColumnList + ' FROM ' + @Table2
    
    set @SQL = 'SELECT Max(TableName) as TableName, ' + @t1ColumnList +
     ' FROM (' + @SQL + ') A GROUP BY ' + @t1ColumnList + 
     ' HAVING COUNT(*) = 1'
    
    exec ( @SQL)
    
        2
  •  0
  •   Abe Miessler    14 年前

    想为你做点什么吗:

    select 
      (Case when log.RecordID = audit.CardID THEN 1 else 0) as RecordIdEqual,
      (Case when log.Name = audit.Name THEN 1 else 0) as NamesEqual ,
      (Case when log.Date = audit.DeploymentDate THEN 1 else 0) as DatesEqual, 
      (Case when log.Address = audit.ShippingAddress THEN 1 else 0) as AddressEqual,
      (Case when log.Products = audit.Options THEN 1 else 0) as ProductsEqual
      --etc
    from Audit audit, Log log
      where audit.CardID=log.RecordId
    

    这将根据列名对相等项进行细分。似乎这可能比做所有的铸造和解释产生的字符串更容易。。。