当我无法加入时,Sql Server如何结合2 WITH数据集

时间:2017-10-30 07:23:47

标签: sql sql-server insert union

我希望合并2 WITH个数据集,但它有点复杂,因为我从一个表中收集了第一个WITH数据集,而从另一个表收集了另一个WITH语句,但是列我选择的是从不同的表中相同。

这是我WITH选择的方式:

;WITH A AS
    (
    SELECT DISTINCT PROJECTID, 
            SUBSTRING(redirect, CHARINDEX('p', redirect, 6), CHARINDEX('.aspx', redirect) - CHARINDEX('p', redirect, 6)) Link,
            CAST(CASE WHEN redirect LIKE '%s-1.mytest.com%' THEN 2 ELSE 1 END AS tinyint) PROJECTS_DOMAINID,
            CAST(CASE WHEN redirect LIKE '%/test_p%' THEN 1 else 0 END AS BIT) test_project
    FROM LOG_REDIRECT WITH(NOLOCK) 
    WHERE (is_valid = 1) AND (INSERT_DATE > @date_from) -- AND (PROJECTID = 120515) 
    )

    INSERT INTO #temp_Tbl ( PROJECTID, Link, PROJECTS_DOMAINID, test_project)

使用相同的INSERT

反对另一个WITH语句
    ;WITH B as 
        (
        SELECT DISTINCT
            PROJECTID
            , SUBSTRING(ds.link, 0, CHARINDEX('?__', ds.link)) AS link 
            FROM dbo.LINKS_PROJECTS as PROJECTS_DOMAINID
        WHERE ds.link > '' AND ds.link LIKE '%mytest.com%' AND (ds.INSERT_DATE > @date_from  OR ds.UPDATE_DATE > @date_from)
        )
INSERT INTO #temp_Tbl ( PROJECTID, Link, PROJECTS_DOMAINID, test_project)

我如何union这两个WITH数据集?基本上我想结合,因为我认为我可以使用一个INSERT而不是两个INSERTS到同一个临时表。

3 个答案:

答案 0 :(得分:0)

;WITH A AS
    (
    SELECT DISTINCT PROJECTID, 
            SUBSTRING(redirect, CHARINDEX('p', redirect, 6), CHARINDEX('.aspx', redirect) - CHARINDEX('p', redirect, 6)) Link,
            CAST(CASE WHEN redirect LIKE '%s-1.mytest.com%' THEN 2 ELSE 1 END AS tinyint) PROJECTS_DOMAINID,
            CAST(CASE WHEN redirect LIKE '%/test_p%' THEN 1 else 0 END AS BIT) test_project
    FROM LOG_REDIRECT WITH(NOLOCK) 
    WHERE (is_valid = 1) AND (INSERT_DATE > @date_from) -- AND (PROJECTID = 120515) 
    ),  B as 
        (
        SELECT DISTINCT
            PROJECTID
            , SUBSTRING(ds.link, 0, CHARINDEX('?__', ds.link)) AS link 
            FROM dbo.LINKS_PROJECTS as PROJECTS_DOMAINID
        WHERE ds.link > '' AND ds.link LIKE '%mytest.com%' AND (ds.INSERT_DATE > @date_from  OR ds.UPDATE_DATE > @date_from)
        )
INSERT INTO #temp_Tbl ( PROJECTID, Link, PROJECTS_DOMAINID, test_project)
SELECT * FROM A
UNION
SELECT * FROM B

答案 1 :(得分:0)

我们可以只有一个with子句。

INSERT INTO #temp_Tbl ( PROJECTID, Link, PROJECTS_DOMAINID, test_project)
WITH A AS
    (
    SELECT PROJECTID, 
            SUBSTRING(redirect, CHARINDEX('p', redirect, 6), CHARINDEX('.aspx', redirect) - CHARINDEX('p', redirect, 6)) Link,
            CAST(CASE WHEN redirect LIKE '%s-1.mytest.com%' THEN 2 ELSE 1 END AS tinyint) PROJECTS_DOMAINID,
            CAST(CASE WHEN redirect LIKE '%/test_p%' THEN 1 else 0 END AS BIT) test_project
    FROM LOG_REDIRECT WITH(NOLOCK) 
    WHERE (is_valid = 1) AND (INSERT_DATE > @date_from) -- AND (PROJECTID = 120515) 
  UNION
        SELECT 
            PROJECTID
            , SUBSTRING(ds.link, 0, CHARINDEX('?__', ds.link)) AS link 
            FROM dbo.LINKS_PROJECTS as PROJECTS_DOMAINID
        WHERE ds.link > '' AND ds.link LIKE '%mytest.com%' AND (ds.INSERT_DATE > @date_from  OR ds.UPDATE_DATE > @date_from)
    )
select PROJECTID, Link, PROJECTS_DOMAINID, test_project from A;

当UNION删除重复项,然后将数据插入临时表时,不需要有两个区别。

答案 2 :(得分:0)

尝试这种方式有点不同:

IF OBJECT_ID('tempdb..#temp_Tbl') IS NOT NULL
DROP TABLE #temp_TblA

IF OBJECT_ID('tempdb..#temp_Tbl') IS NOT NULL
DROP TABLE #temp_TblB

SELECT DISTINCT PROJECTID, 
        SUBSTRING(redirect, CHARINDEX('p', redirect, 6), CHARINDEX('.aspx', redirect) - CHARINDEX('p', redirect, 6)) Link,
        CAST(CASE WHEN redirect LIKE '%s-1.mytest.com%' THEN 2 ELSE 1 END AS tinyint) PROJECTS_DOMAINID,
        CAST(CASE WHEN redirect LIKE '%/test_p%' THEN 1 else 0 END AS BIT) test_project
INTO  #temp_Tbl  
FROM LOG_REDIRECT WITH(NOLOCK) 
WHERE (is_valid = 1) AND (INSERT_DATE > @date_from) -- AND (PROJECTID = 120515) 

SELECT DISTINCT  PROJECTID,
       SUBSTRING(ds.link, 0, CHARINDEX('?__', ds.link)) AS link
       NULL AS PROJECTS_DOMAINID, -- Or put your logic 
       NULL AS test_project, -- or put your logic to get value 
INTO   #temp_TblB
FROM   dbo.LINKS_PROJECTS  AS PROJECTS_DOMAINID
WHERE  ds.link > ''
       AND ds.link LIKE '%mytest.com%'
       AND (ds.INSERT_DATE > @date_from OR ds.UPDATE_DATE > @date_from)


-- Final Result 

SELECT * FROM #temp_TblA
UNION 
SELECT * FROM #temp_TblB

-- Cleanup 

IF OBJECT_ID('tempdb..#temp_Tbl') IS NOT NULL
DROP TABLE #temp_TblA

IF OBJECT_ID('tempdb..#temp_Tbl') IS NOT NULL
DROP TABLE #temp_TblB