我将从下面的查询中获取值。然后,我将创建一个循环。
inline
使用上面的循环,我需要从下面的查询中提取值(F.a = a)
select a FROM [tbl_outer] where col=1
我该怎么做?你能帮我吗?
答案 0 :(得分:0)
假设所有表都在同一个数据库中,则可以尝试一下(很遗憾,我现在无法对其进行测试,但是我很确定它会起作用):
select T.cv, T.fa, a
FROM [tbl_outer]
left outer join (
select Distinct C.v as cv, F.a as fa
from tbl_main as F
join tbl_inner1 as C on C.ID = F.ID and c.to = 0
join tbl_inner2 as E on E.a = F.a
join tbl_inner3 as G on G.Id = E.Sales
where C.v NOT IN(select v from table_4
where Month = DATEPART(month, GETDATE())
AND Year = DATEPART(yyyy, GETDATE()))
) T on a = T.fa
where col=1
说明
left outer join
来保留您的第一个查询返回的所有值。T.cv
和T.fa
)将为NULL
可以肯定地优化此查询,但让我们先确认正确性。
答案 1 :(得分:0)
我只需添加一个附加的join
条件:
select Distinct C.v, F.a
from tbl_outer o join
tbl_main f
on f.a = o.a join
tbl_inner1 C
on C.ID = F.ID and c.to = 0 join
tbl_inner2 E
on E.a = F.a join
tbl_inner3 G
on G.Id = E.Sales
where o.col = 1 and
C.v not in (select v
from table_4 t4
where t4.Month = month(getdate()) and t4.year = year(getdate())
);
注意:
datepart()
替换为更易于理解的month()
和year()
函数。left join
,甚至没有其他匹配值的o.a
,则可能需要le = LinkExtractor()
le.extract_links(response)
。