如何与WITH一起使用不存在的位置?

时间:2018-07-27 14:36:16

标签: sql sql-server sql-server-2012

我有两组具有相同列的列,并且我试图获取第一组中第二组中不存在的项目的列表。第二个列表是通过使用WITH的递归选择构建的,我不知道如何在WHERE NOT EXISTS中使用它。

SELECT *
FROM (
    SELECT DISTINCT [Fruit]
        ,[Color]
    FROM [dbo].[CostCentreMapping]
    WHERE [Color] IS NOT NULL
        AND [Color] <> ''
        AND LEFT([Fruit], 1) = 'C'
    ) AS a
WHERE NOT EXISTS (
        --Incorrect syntax error occurs here
        WITH tmp(Fruit, DataItem, ManyColors) AS (
                SELECT Fruit
                    ,CAST(LEFT(ManyColors, CHARINDEX(',', ManyColors + ',') - 1) AS VARCHAR(200))
                    ,CAST(STUFF(ManyColors, 1, CHARINDEX(',', ManyColors + ','), '') AS VARCHAR(200))
                FROM ExecutiveSummaryDisplay

                UNION ALL

                SELECT Fruit
                    ,CAST(LEFT(ManyColors, CHARINDEX(',', ManyColors + ',') - 1) AS VARCHAR(200))
                    ,CAST(STUFF(ManyColors, 1, CHARINDEX(',', ManyColors + ','), '') AS VARCHAR(200))
                FROM tmp
                WHERE ManyColors > ''
                )
        SELECT DISTINCT Fruit AS [Fruit]
            ,DataItem AS [Color]
        FROM tmp
        WHERE Fruit IS NOT NULL
            AND Fruit <> ''
            AND DataItem IS NOT NULL
            AND DataItem <> ''
        )

3 个答案:

答案 0 :(得分:2)

您可以:

with tmp (Fruit, DataItem, ManyColors) as
(
  select . . . 
  union 
  select . . .
)

SELECT DISTINCT [Fruit], [Color]
FROM [dbo].[CostCentreMapping] cm
WHERE [Color] IS NOT NULL AND [Color] <> '' AND LEFT([Fruit], 1) = 'C') AND
      NOT EXISTS (SELECT 1 FROM tmp where . . .);

答案 1 :(得分:1)

您可以使用临时表:

WITH tmp(Fruit, DataItem, ManyColors) AS
    (
        SELECT
            Fruit,
            CAST(LEFT(ManyColors, CHARINDEX(',', ManyColors + ',') - 1) as varchar(200)),
            CAST(STUFF(ManyColors, 1, CHARINDEX(',', ManyColors + ','), '') as varchar(200))
        FROM ExecutiveSummaryDisplay
        UNION all

        SELECT
            Fruit,
            CAST(LEFT(ManyColors, CHARINDEX(',', ManyColors + ',') - 1) as varchar(200)),
            CAST(STUFF(ManyColors, 1, CHARINDEX(',', ManyColors + ','), '') as varchar(200))
        FROM tmp
        WHERE
            ManyColors > ''
    )

    SELECT DISTINCT INTO #TEMPTABLE
        Fruit AS [Fruit],
        DataItem AS [Color]
    FROM tmp
    WHERE Fruit IS NOT NULL AND Fruit <> '' AND DataItem IS NOT NULL AND DataItem <> '')


    SELECT DISTINCT [Fruit]
          ,[Color]
      FROM [dbo].[CostCentreMapping]
      WHERE [Color] IS NOT NULL AND [Color] <> '' AND LEFT([Fruit], 1) = 'C'
            AND NOT EXISTS(
                select * from #TEMPTABLE T WHERE 
                T.Color=CostCentreMapping.Color AND 
                T.Fruit=CostCentreMapping.Fruit)

答案 2 :(得分:1)

如果您不想使用临时表#TEMPTABLE,这是另一种解决方案:

WITH tmp(Fruit, DataItem, ManyColors) AS
    (
        SELECT
            Fruit,
            CAST(LEFT(ManyColors, CHARINDEX(',', ManyColors + ',') - 1) as varchar(200)),
            CAST(STUFF(ManyColors, 1, CHARINDEX(',', ManyColors + ','), '') as varchar(200))
        FROM ExecutiveSummaryDisplay
        UNION all

        SELECT
            Fruit,
            CAST(LEFT(ManyColors, CHARINDEX(',', ManyColors + ',') - 1) as varchar(200)),
            CAST(STUFF(ManyColors, 1, CHARINDEX(',', ManyColors + ','), '') as varchar(200))
        FROM tmp
        WHERE
            ManyColors > ''
    )


     SELECT DISTINCT [Fruit], [Color]
      FROM [dbo].[CostCentreMapping]
      WHERE [Color] IS NOT NULL AND [Color] <> '' AND LEFT([Fruit], 1) = 'C'
      AND NOT EXISTS(SELECT DISTINCT Fruit AS [Fruit], DataItem AS [Color] FROM tmp T
                     WHERE Fruit IS NOT NULL AND Fruit <> '' AND DataItem IS NOT NULL AND DataItem <> ''
                     and T.Color=CostCentreMapping.Color AND T.Fruit=CostCentreMapping.Fruit
                     )