我希望合并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
到同一个临时表。
答案 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