在通用 table 表达式的输入参数中使用查询结果
Using result of a query in input parameters for a common table expression
我有这样的疑问:
select distinct Project_Id,keyword,SE_Id from Table1 ;
它 returns 我将近 14.000 行。
我有另一个 SQL-查询,如下所示:
with DateWithValue as (
SELECT *
FROM DateTable as dt LEFT JOIN Table1 as PK on dt.Date_ID=PK.Load_Date and PK.Project_Id=? and Pk.keyword=? and SE_Id=?
LEFT JOIN Table2 as pr on PK.Project_ID=pr.Project_ID
where PK.Domain is not null and
dt.Date_ID>= (SELECT min(Load_Date) from Table1 where Project_Id=? and keyword=? and SE_Id=? ) and
dt.Date_ID<= (SELECT max(Load_Date) from Table1 where Project_Id=? and keyword=? and SE_Id=? )
--order by Date_ID
)
select * from DateTable as dt Left join DateWithValue on dt.Date_ID=DateWithValue.Date_ID
where DateWithValue.Date_ID is not null
order by dt.Date_ID
在此查询中 ?
应替换为第一个查询的第 i 个结果。
如何合并这两个查询?你对我有什么想法吗
更新:我已将查询更改如下(为了更好地分析,我举了一个例子):
SELECT
dt.Date_ID
,Pk.keyword
-- ,pr.Company_BK
-- ,pr.Project_URL
,t2.pos_position
,pk.Project_Id
,PK.SE_Id
FROM
DimDate as dt
JOIN
(
SELECT
t1.Project_Id,
t1.keyword,
t1.SE_Id,
Min_Load_Date = MIN(t1.Load_Date),
Max_Load_Date = MAX(t1.Load_Date)
FROM
[RL].[SearchMetrics_ProjectKeyword] t1
where keyword='aktueller gaspreis pro kwh' and Project_Id=383601 and SE_ID=1
GROUP BY
t1.Project_Id,
t1.keyword,
t1.SE_Id
)
as PK
ON dt.Date_ID >= PK.Min_Load_Date
AND dt.Date_ID < PK.Max_Load_Date
--AND PK.Project_Id=?
--AND Pk.keyword=?
--AND SE_Id=?
LEFT JOIN [RL].[SearchMetrics_ProjectKeyword] t2
on Pk.Project_Id=t2.Project_Id and PK.keyword=t2.keyword and PK.SE_Id=t2.SE_Id and t2.Load_Date >= dt.Date_ID and t2.Load_Date <= dt.Date_ID
-- LEFT JOIN MDM.SearchMetrics_Project as pr
-- ON PK.Project_ID=pr.Project_ID
通过这个查询我得到这样的结果:
结构正是我要找的,但新的挑战是列 pos_positin
,它有时有 NULL 值。但它不应该是这样的。
20180406 和 20180412 之间的这一列应该是 19,20180413 和 20180423 之间应该是 21
我怎样才能通过这个查询实现这一点?
更新 2:我在这里找到了解决方案:
with cte as
(
SELECT
dt.Date_ID
,Pk.keyword
-- ,pr.Company_BK
-- ,pr.Project_URL
,t2.pos_position
,pk.Project_Id
,PK.SE_Id
FROM
DimDate as dt
JOIN
(
SELECT
t1.Project_Id,
t1.keyword,
t1.SE_Id,
Min_Load_Date = MIN(t1.Load_Date),
Max_Load_Date = MAX(t1.Load_Date)
FROM
[RL].[SearchMetrics_ProjectKeyword] t1
where keyword='citire gaze' and Project_Id=1458724 and SE_ID=79
GROUP BY
t1.Project_Id,
t1.keyword,
t1.SE_Id
)
as PK
ON dt.Date_ID >= PK.Min_Load_Date
AND dt.Date_ID < PK.Max_Load_Date
--AND PK.Project_Id=?
--AND Pk.keyword=?
--AND SE_Id=?
LEFT JOIN [RL].[SearchMetrics_ProjectKeyword] t2
on Pk.Project_Id=t2.Project_Id and PK.keyword=t2.keyword and PK.SE_Id=t2.SE_Id and t2.Load_Date >= dt.Date_ID and t2.Load_Date <= dt.Date_ID
-- LEFT JOIN MDM.SearchMetrics_Project as pr
-- ON PK.Project_ID=pr.Project_ID
)
select a.Date_ID
,a.keyword
,a.Project_Id
,a.SE_Id
,ISNULL(a.pos_position,x.pos_position) pos_position
from cte a outer apply
(select top 1 pos_position
from cte b
where b.Date_ID<a.Date_ID and
b.Project_Id is not null and
b.pos_position is not null and
a.pos_position is null order by Date_ID desc)x
这应该可以满足您的需求并避免大量的性能问题...
将您的第一个查询更改为此...
-- #first_query...
SELECT
t1.Project_Id,
t1.keyword,
t1.SE_Id,
Min_Load_Date = CONVERT(DATE, MIN(t1.Load_Date)),
Max_Load_Date = CONVERT(DATE(MAX(t1.Load_Date))
FROM
dbo.Table1 t1
GROUP BY
t1.Project_Id,
t1.keyword,
t1.SE_Id;
那么您的第二个查询将如下所示...
SELECT
*
FROM
dbo.DateTable as dt
JOIN #First_Query as PK
ON dt.Date_ID >= PK.Min_Load_Date
AND dt.Date_ID < DATEADD(DAY, 1, Max_Load_Date)
--AND PK.Project_Id=?
--AND Pk.keyword=?
--AND SE_Id=?
LEFT JOIN Table2 as pr
ON PK.Project_ID=pr.Project_ID;
--where
-- PK.Domain is not null
-- AND dt.Date_ID>= (SELECT min(Load_Date) from Table1 where Project_Id=? and keyword=? and SE_Id=? )
-- AND dt.Date_ID<= (SELECT max(Load_Date) from Table1 where Project_Id=? and keyword=? and SE_Id=? )
--order by Date_ID
我有这样的疑问:
select distinct Project_Id,keyword,SE_Id from Table1 ;
它 returns 我将近 14.000 行。
我有另一个 SQL-查询,如下所示:
with DateWithValue as (
SELECT *
FROM DateTable as dt LEFT JOIN Table1 as PK on dt.Date_ID=PK.Load_Date and PK.Project_Id=? and Pk.keyword=? and SE_Id=?
LEFT JOIN Table2 as pr on PK.Project_ID=pr.Project_ID
where PK.Domain is not null and
dt.Date_ID>= (SELECT min(Load_Date) from Table1 where Project_Id=? and keyword=? and SE_Id=? ) and
dt.Date_ID<= (SELECT max(Load_Date) from Table1 where Project_Id=? and keyword=? and SE_Id=? )
--order by Date_ID
)
select * from DateTable as dt Left join DateWithValue on dt.Date_ID=DateWithValue.Date_ID
where DateWithValue.Date_ID is not null
order by dt.Date_ID
在此查询中 ?
应替换为第一个查询的第 i 个结果。
如何合并这两个查询?你对我有什么想法吗
更新:我已将查询更改如下(为了更好地分析,我举了一个例子):
SELECT
dt.Date_ID
,Pk.keyword
-- ,pr.Company_BK
-- ,pr.Project_URL
,t2.pos_position
,pk.Project_Id
,PK.SE_Id
FROM
DimDate as dt
JOIN
(
SELECT
t1.Project_Id,
t1.keyword,
t1.SE_Id,
Min_Load_Date = MIN(t1.Load_Date),
Max_Load_Date = MAX(t1.Load_Date)
FROM
[RL].[SearchMetrics_ProjectKeyword] t1
where keyword='aktueller gaspreis pro kwh' and Project_Id=383601 and SE_ID=1
GROUP BY
t1.Project_Id,
t1.keyword,
t1.SE_Id
)
as PK
ON dt.Date_ID >= PK.Min_Load_Date
AND dt.Date_ID < PK.Max_Load_Date
--AND PK.Project_Id=?
--AND Pk.keyword=?
--AND SE_Id=?
LEFT JOIN [RL].[SearchMetrics_ProjectKeyword] t2
on Pk.Project_Id=t2.Project_Id and PK.keyword=t2.keyword and PK.SE_Id=t2.SE_Id and t2.Load_Date >= dt.Date_ID and t2.Load_Date <= dt.Date_ID
-- LEFT JOIN MDM.SearchMetrics_Project as pr
-- ON PK.Project_ID=pr.Project_ID
通过这个查询我得到这样的结果:
结构正是我要找的,但新的挑战是列 pos_positin
,它有时有 NULL 值。但它不应该是这样的。
20180406 和 20180412 之间的这一列应该是 19,20180413 和 20180423 之间应该是 21
我怎样才能通过这个查询实现这一点?
更新 2:我在这里找到了解决方案:
with cte as
(
SELECT
dt.Date_ID
,Pk.keyword
-- ,pr.Company_BK
-- ,pr.Project_URL
,t2.pos_position
,pk.Project_Id
,PK.SE_Id
FROM
DimDate as dt
JOIN
(
SELECT
t1.Project_Id,
t1.keyword,
t1.SE_Id,
Min_Load_Date = MIN(t1.Load_Date),
Max_Load_Date = MAX(t1.Load_Date)
FROM
[RL].[SearchMetrics_ProjectKeyword] t1
where keyword='citire gaze' and Project_Id=1458724 and SE_ID=79
GROUP BY
t1.Project_Id,
t1.keyword,
t1.SE_Id
)
as PK
ON dt.Date_ID >= PK.Min_Load_Date
AND dt.Date_ID < PK.Max_Load_Date
--AND PK.Project_Id=?
--AND Pk.keyword=?
--AND SE_Id=?
LEFT JOIN [RL].[SearchMetrics_ProjectKeyword] t2
on Pk.Project_Id=t2.Project_Id and PK.keyword=t2.keyword and PK.SE_Id=t2.SE_Id and t2.Load_Date >= dt.Date_ID and t2.Load_Date <= dt.Date_ID
-- LEFT JOIN MDM.SearchMetrics_Project as pr
-- ON PK.Project_ID=pr.Project_ID
)
select a.Date_ID
,a.keyword
,a.Project_Id
,a.SE_Id
,ISNULL(a.pos_position,x.pos_position) pos_position
from cte a outer apply
(select top 1 pos_position
from cte b
where b.Date_ID<a.Date_ID and
b.Project_Id is not null and
b.pos_position is not null and
a.pos_position is null order by Date_ID desc)x
这应该可以满足您的需求并避免大量的性能问题...
将您的第一个查询更改为此...
-- #first_query...
SELECT
t1.Project_Id,
t1.keyword,
t1.SE_Id,
Min_Load_Date = CONVERT(DATE, MIN(t1.Load_Date)),
Max_Load_Date = CONVERT(DATE(MAX(t1.Load_Date))
FROM
dbo.Table1 t1
GROUP BY
t1.Project_Id,
t1.keyword,
t1.SE_Id;
那么您的第二个查询将如下所示...
SELECT
*
FROM
dbo.DateTable as dt
JOIN #First_Query as PK
ON dt.Date_ID >= PK.Min_Load_Date
AND dt.Date_ID < DATEADD(DAY, 1, Max_Load_Date)
--AND PK.Project_Id=?
--AND Pk.keyword=?
--AND SE_Id=?
LEFT JOIN Table2 as pr
ON PK.Project_ID=pr.Project_ID;
--where
-- PK.Domain is not null
-- AND dt.Date_ID>= (SELECT min(Load_Date) from Table1 where Project_Id=? and keyword=? and SE_Id=? )
-- AND dt.Date_ID<= (SELECT max(Load_Date) from Table1 where Project_Id=? and keyword=? and SE_Id=? )
--order by Date_ID