比较2个表之间的列值和条件默认值

时间:2019-03-27 15:47:35

标签: compare sql-server-2017 isnull not-exists isnullorempty

我有2个表,需要在其中比较它们的列值。比较效果很好。但是,如果源表或目标表中的任何一个表列均为NULL,则需要添加一个额外的条件才能填写默认值。但是,仅当我在“默认”列中有值时。本质上,Default列值将用作NULL表值的替代。

我已经提供了CREATE TABLE脚本,并且已经准备好完整的代码,这些代码已通过源表和目标表之间的列比较准备就绪。

/****** Object:  Table [dbo].[SourceTable]    Script Date: 9/14/2017 7:57:37 PM ******/
SET ANSI_NULLS ON
GO
CREATE TABLE [dbo].SourceTable(
    ProductID [int] NOT NULL,
    [Provider] [int] NOT NULL,
    MaturityDate [datetime] NULL,
    Price [numeric](28, 10) NULL,
    Price2 [numeric](28, 10) NULL,
    [Default] [numeric](28, 10) NULL
)
GO
/****** Object:  Table [dbo].[TargetTable]    Script Date: 9/14/2017 7:57:37 PM ******/
SET ANSI_NULLS ON
GO
CREATE TABLE [dbo].TargetTable(
    ProductID [int] NOT NULL,
    [Provider] [int] NOT NULL,
    MaturityDate [datetime] NULL,
    Price [numeric](28, 10) NULL,
    Price2 [numeric](28, 10) NULL,
    [Default] [numeric](28, 10) NULL
)

-- Populate Source Table
INSERT [dbo].SourceTable (ProductID, [Provider], MaturityDate, Price, Price2, [Default]) VALUES (1, 28, N'2019-01-01', CAST(10 AS Numeric(28, 10)), CAST(0 AS Numeric(28, 10)), CAST(NULL AS Numeric(28, 10)))
INSERT [dbo].SourceTable (ProductID, [Provider], MaturityDate, Price, Price2, [Default]) VALUES (2, 28, N'2019-01-01', CAST(15 AS Numeric(28, 10)), CAST(0 AS Numeric(28, 10)), CAST(NULL AS Numeric(28, 10)))
INSERT [dbo].SourceTable (ProductID, [Provider], MaturityDate, Price, Price2, [Default]) VALUES (3, 28, N'2019-01-01', CAST(5 AS Numeric(28, 10)), CAST(0 AS Numeric(28, 10)), CAST(NULL AS Numeric(28, 10)))
INSERT [dbo].SourceTable (ProductID, [Provider], MaturityDate, Price, Price2, [Default]) VALUES (4, 28, N'2019-01-01', CAST(0 AS Numeric(28, 10)), CAST(0 AS Numeric(28, 10)), CAST(NULL AS Numeric(28, 10)))
INSERT [dbo].SourceTable (ProductID, [Provider], MaturityDate, Price, Price2, [Default]) VALUES (5, 28, N'2019-01-01', CAST(0 AS Numeric(28, 10)), CAST(0 AS Numeric(28, 10)), CAST(NULL AS Numeric(28, 10)))
INSERT [dbo].SourceTable (ProductID, [Provider], MaturityDate, Price, Price2, [Default]) VALUES (6, 28, N'2019-01-01', CAST(0 AS Numeric(28, 10)), CAST(0 AS Numeric(28, 10)), CAST(NULL AS Numeric(28, 10)))
INSERT [dbo].SourceTable (ProductID, [Provider], MaturityDate, Price, Price2, [Default]) VALUES (7, 28, N'2019-01-01', CAST(0 AS Numeric(28, 10)), CAST(0 AS Numeric(28, 10)), CAST(NULL AS Numeric(28, 10)))
INSERT [dbo].SourceTable (ProductID, [Provider], MaturityDate, Price, Price2, [Default]) VALUES (8, 28, N'2019-01-01', CAST(0 AS Numeric(28, 10)), CAST(0 AS Numeric(28, 10)), CAST(NULL AS Numeric(28, 10)))
INSERT [dbo].SourceTable (ProductID, [Provider], MaturityDate, Price, Price2, [Default]) VALUES (9, 28, N'2019-01-01', CAST(1 AS Numeric(28, 10)), CAST(0 AS Numeric(28, 10)), CAST(NULL AS Numeric(28, 10)))
INSERT [dbo].SourceTable (ProductID, [Provider], MaturityDate, Price, Price2, [Default]) VALUES (10, 28, N'2019-01-01', CAST(1 AS Numeric(28, 10)), CAST(0 AS Numeric(28, 10)), CAST(NULL AS Numeric(28, 10)))
GO


-- Populate Target Table
INSERT [dbo].TargetTable (ProductID, [Provider], MaturityDate, Price, Price2, [Default]) VALUES (1, 28, N'2019-01-01', CAST(NULL AS Numeric(28,10)), CAST(NULL AS Numeric(28, 10)), CAST(0 AS Numeric(28, 10)))
INSERT [dbo].TargetTable (ProductID, [Provider], MaturityDate, Price, Price2, [Default]) VALUES (2, 28, N'2019-01-01', CAST(NULL AS Numeric(28,10)), CAST(NULL AS Numeric(28, 10)), CAST(0 AS Numeric(28, 10)))
INSERT [dbo].TargetTable (ProductID, [Provider], MaturityDate, Price, Price2, [Default]) VALUES (3, 28, N'2019-01-01', CAST(NULL AS Numeric(28,10)), CAST(NULL AS Numeric(28, 10)), CAST(0 AS Numeric(28, 10)))
INSERT [dbo].TargetTable (ProductID, [Provider], MaturityDate, Price, Price2, [Default]) VALUES (4, 28, N'2019-01-01', CAST(NULL AS Numeric(28,10)), CAST(NULL AS Numeric(28, 10)), CAST(0 AS Numeric(28, 10)))
INSERT [dbo].TargetTable (ProductID, [Provider], MaturityDate, Price, Price2, [Default]) VALUES (5, 28, N'2019-01-01', CAST(NULL AS Numeric(28,10)), CAST(NULL AS Numeric(28, 10)), CAST(0 AS Numeric(28, 10)))
INSERT [dbo].TargetTable (ProductID, [Provider], MaturityDate, Price, Price2, [Default]) VALUES (6, 28, N'2019-01-01', CAST(NULL AS Numeric(28,10)), CAST(NULL AS Numeric(28, 10)), CAST(0 AS Numeric(28, 10)))
INSERT [dbo].TargetTable (ProductID, [Provider], MaturityDate, Price, Price2, [Default]) VALUES (7, 28, N'2019-01-01', CAST(NULL AS Numeric(28,10)), CAST(NULL AS Numeric(28, 10)), CAST(0 AS Numeric(28, 10)))
INSERT [dbo].TargetTable (ProductID, [Provider], MaturityDate, Price, Price2, [Default]) VALUES (8, 28, N'2019-01-01', CAST(NULL AS Numeric(28,10)), CAST(NULL AS Numeric(28, 10)), CAST(0 AS Numeric(28, 10)))
INSERT [dbo].TargetTable (ProductID, [Provider], MaturityDate, Price, Price2, [Default]) VALUES (9, 28, N'2019-01-01', CAST(NULL AS Numeric(28,10)), CAST(NULL AS Numeric(28, 10)), CAST(0 AS Numeric(28, 10)))
INSERT [dbo].TargetTable (ProductID, [Provider], MaturityDate, Price, Price2, [Default]) VALUES (10, 28, N'2019-01-01', CAST(NULL AS Numeric(28,10)), CAST(NULL AS Numeric(28, 10)), CAST(0 AS Numeric(28, 10)))
GO

    ;with 

    compare_source ([Provider], ProductID, [Default],

    /*** Source columns to compare ***/

                Col1Source, Col2Source

    )

    as (

                  SELECT
                             [Provider]
                            ,ProductID
                            ,Price
                            ,Price2
                            ,[Default]

                  FROM  dbo.SourceTable

    ),

    compare_target ([Provider], ProductID, [Default],

    /*** Target columns to compare ***/

                Col1Target, Col2Target

    )

    as 

        (
                SELECT
                             [Provider]
                            ,ProductID
                            ,Price
                            ,Price2
                            ,[Default]

                  FROM  dbo.TargetTable
        )

            SELECT ProductID

            , Col1Source, Col1Target

            , Col2Source, Col2Target

            , [Provider]

    FROM 
    (
                SELECT 
                          s.ProductID
                        , s.[Provider]
                        , s.Col1Source, s.Col2Source 
                        , t.Col1Target, t.Col2Target

                FROM compare_source s

                left join compare_target t on t.ProductID = s.ProductID

                WHERE not exists

                (
                        SELECT 1 FROM compare_target t WHERE

                        s.ProductID = t.ProductID AND (( Col1Source = Col1Target ) OR ( ISNULL ( Col1Source, Col1Target ) IS NULL )) AND
                        s.ProductID = t.ProductID AND (( Col2Source = Col2Target ) OR ( ISNULL ( Col2Source, Col2Target ) IS NULL )) 

                )

    ) diff

Here are the current results:

ProductID | Col1Source   | Col1Target | Col2Source | Col2Target    | Provider
    1     | 0.0000000000 |   NULL     |   NULL     | 0.0000000000  |    28
    2     | 0.0000000000 |   NULL     |   NULL     | 0.0000000000  |    28
    3     | 0.0000000000 |   NULL     |   NULL     | 0.0000000000  |    28
    4     | 0.0000000000 |   NULL     |   NULL     | 0.0000000000  |    28
    5     | 0.0000000000 |   NULL     |   NULL     | 0.0000000000  |    28
    6     | 0.0000000000 |   NULL     |   NULL     | 0.0000000000  |    28
    7     | 0.0000000000 |   NULL     |   NULL     | 0.0000000000  |    28
    8     | 0.0000000000 |   NULL     |   NULL     | 0.0000000000  |    28
    9     | 0.0000000000 |   NULL     |   NULL     | 0.0000000000  |    28
    10    | 0.0000000000 |   NULL     |   NULL     | 0.0000000000  |    28


Since I have values in my TargetTable Default field (zeroes), I would like to insert these default values (zeroes) in my NULL values in my Target table.  In essence, these Default values server as an "override" to replace all of my NULL values in my tables.  The rule is, if there are values in my Default column, then use these values to replace all of my NULL fields.  This would have the effect of eliminating some of the column value differences between tables. Thus, the expected results after the replacement of the NULLs by the Default field values should give the following result:

ProductID | Col1Source   | Col1Target      | Col2Source | Col2Target    | Provider
    1     | 0.0000000000 |   0.0000000000  |   NULL     | 0.0000000000  |    28
    2     | 0.0000000000 |   0.0000000000  |   NULL     | 0.0000000000  |    28
    3     | 0.0000000000 |   0.0000000000  |   NULL     | 0.0000000000  |    28
    4     | 0.0000000000 |   0.0000000000  |   NULL     | 0.0000000000  |    28
    5     | 0.0000000000 |   0.0000000000  |   NULL     | 0.0000000000  |    28
    6     | 0.0000000000 |   0.0000000000  |   NULL     | 0.0000000000  |    28
    7     | 0.0000000000 |   0.0000000000  |   NULL     | 0.0000000000  |    28
    8     | 0.0000000000 |   0.0000000000  |   NULL     | 0.0000000000  |    28
    9     | 0.0000000000 |   0.0000000000  |   NULL     | 0.0000000000  |    28
    10    | 0.0000000000 |   0.0000000000  |   NULL     | 0.0000000000  |    28

In theory, I no longer want to see my fields which are identical.  Thus, at the end, here is my result of the comparison after the Default values have replaced my NULLs:

ProductID | Col2Source | Col2Target    | Provider
    1     |    NULL     | 0.0000000000  |    28
    2     |    NULL     | 0.0000000000  |    28
    3     |    NULL     | 0.0000000000  |    28
    4     |    NULL     | 0.0000000000  |    28
    5     |    NULL     | 0.0000000000  |    28
    6     |    NULL     | 0.0000000000  |    28
    7     |    NULL     | 0.0000000000  |    28
    8     |    NULL     | 0.0000000000  |    28
    9     |    NULL     | 0.0000000000  |    28
    10    |    NULL     | 0.0000000000  |    28

I'm hoping it's mostly clear.

0 个答案:

没有答案