Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 需要更好的方法从原始数据中对现有表进行QA。子集原始数据并连接到现有表不是最好的方法_Sql_Sql Server_Tsql_Relational Database_Qa - Fatal编程技术网

Sql 需要更好的方法从原始数据中对现有表进行QA。子集原始数据并连接到现有表不是最好的方法

Sql 需要更好的方法从原始数据中对现有表进行QA。子集原始数据并连接到现有表不是最好的方法,sql,sql-server,tsql,relational-database,qa,Sql,Sql Server,Tsql,Relational Database,Qa,表A:来自销售数据库的原始事务级数据。存在于数据库中 表B:由其他人创建为原始数据的子集。存在于数据库中 表QA:由我创建,作为从原始表a数据中提取的子集。不存在于数据库中,仅存在于我的查询环境中。此表包括表B作为完全外部联接 我的目标是:检查表B是否在事务级别正确创建。AKA原始数据子集中的每个事务都需要匹配开发人员从原始数据中提取的内容,如表B所示 方法:将原始数据子集到我的QA表中,并通过完整的外部联接将其联接到表B,然后让SQL返回表B中的数据与我的QA表中的数据不匹配的行 问题1:我不

表A:来自销售数据库的原始事务级数据。存在于数据库中

表B:由其他人创建为原始数据的子集。存在于数据库中

表QA:由我创建,作为从原始表a数据中提取的子集。不存在于数据库中,仅存在于我的查询环境中。此表包括表B作为完全外部联接

我的目标是:检查表B是否在事务级别正确创建。AKA原始数据子集中的每个事务都需要匹配开发人员从原始数据中提取的内容,如表B所示

方法:将原始数据子集到我的QA表中,并通过完整的外部联接将其联接到表B,然后让SQL返回表B中的数据与我的QA表中的数据不匹配的行

问题1:我不喜欢这种重复检查表B的一般方法,我愿意接受关于如何进行的其他建议

问题2:当一个值为NULL且填充了一个值时,我的结束步骤不起作用

SELECT *
INTO #QA
FROM (
         SELECT
            A.CustomerCode,
            A.ProductCode,
            A.InvoiceDate,
            A.InvoiceAmount,
            A.InvoiceQty,

            b.ProductID,

            d.CustomerID

        FROM TableA A

            --Join to Product master using Product Code in raw data to retrieve internal Product IDs
            LEFT JOIN ProductMaster b
                ON A.ProductCode = b.AProdCode

            --Join to Customer master to match Customer Code to internal Customer IDs.
            LEFT JOIN CustomerMaster d
                ON A.CustomerCode = d.ACustCode

    ) a

--Join to existing Table B which I'm trying to QA
FULL OUTER JOIN (SELECT * FROM TableB) b
    ON  a.CustomerID = b.CustID
    AND a.ProductID = b.ProdID 
    AND a.InvoiceDate = b.InvDate
    AND a.InvoiceAmount = b.InvQty
    AND a.InvoiceQty = b.InvQty;


--Return the rows where Table B doesn't match what I joined it to.
--This does not take into account when one column is NULL and the other has a value.

SELECT * FROM #QA
WHERE a.CustomerID <> b.CustID
    AND a.ProductID <> b.ProdID 
    AND a.InvoiceDate <> b.InvDate
    AND a.InvoiceAmount <> b.InvQty
    AND a.InvoiceQty <> b.InvQty;

请告诉我一个更好的方法。除了将从原始数据中提取的列的逐列所有NULL值更改为'NULL.A',将从表B中提取的列的'NULL.B'更改为varchar,将9999999和8888888更改为整数之外,我想不出任何东西。

您可以使用EXCEPT关键字

SELECT * 
INTO #QA
FROM A

EXCEPT

SELECT * 
FROM B

如果返回了任何内容,则表示该内容存在于A中,但不存在于B中,如果您切换这两个值,则它将为您提供B中不存在于A中的所有值。

您可以使用EXCEPT关键字

SELECT * 
INTO #QA
FROM A

EXCEPT

SELECT * 
FROM B

如果返回任何内容,则表示它存在于A中,但不存在于B中,如果您切换这两个值,则它将为您提供B中所有非A的值。

我认为更简单的方法之一是为要比较的每对列创建一个case语句,将它们包装在isnull中,给它们一个值,如果它们不同,则返回1,然后选择所有不同的记录

例如:

select * 
from
(
    select *,
        case when isnull(a.customerid, ReplaceValue) <> isnull(b.custid, ReplaceValue) then 1 else 0 end as CustomerCompare,
        case when isnull(a.nextcolumn, ReplaceValue) <> isnull(b.nextcolumn, ReplaceValue) then 1 else 0 end as ColumnCompare
    from #QA
) comparison
where customerCompare = 1 and 
    ... ColumnCompare = 1

另外,我不确定这是否是您的预期行为,但您可能希望将where子句中的AND替换为ORs,以检测是否有任何列发生了更改,而不是所有列。

我认为一种更简单的方法是为要比较的每对列创建一个case语句,将它们包装在isnull中,给它们一个值,如果它们不同,则返回1,然后选择所有不同的记录

例如:

select * 
from
(
    select *,
        case when isnull(a.customerid, ReplaceValue) <> isnull(b.custid, ReplaceValue) then 1 else 0 end as CustomerCompare,
        case when isnull(a.nextcolumn, ReplaceValue) <> isnull(b.nextcolumn, ReplaceValue) then 1 else 0 end as ColumnCompare
    from #QA
) comparison
where customerCompare = 1 and 
    ... ColumnCompare = 1

另外,我不确定这是否是您的预期行为,但您可能希望将where子句中的AND替换为ORs,以检测是否有任何列发生了更改,而不是所有列。

通过回吐表B中的列与我的QA表中的列不匹配的行,您的意思是返回不匹配的数据,或者返回表B中不存在但在QA中的列的名称?@DForck42伟大的评论!我的意思是返回不匹配的数据行。通过吐回表B中的列与我的QA表中的列不匹配的行,您的意思是返回不匹配的数据,还是返回表B中不存在但在QA中的列的名称?@DForck42很棒的评论!我的意思是让它返回不匹配的数据行。嘿,我不知道这是一件事,它对我很好。嘿,我不知道这是一件事,它对我很好。