有人可以帮忙解决问题,我还要加入两个CTE'
Msg 102,Level 15,State 1,Line 19 '('附近的语法错误。 消息156,第15级,状态1,第26行 关键字' left'附近的语法不正确。
查询:
with Cte_LatestPatInfo as
(select max(a.LogID) as LastPatRec, p.unitnum, p.PatNum, a.iplan from rrscsql2.arhighdollar.dbo.tblarinfoHistory a
inner join rrscsql2.arhighdollar.dbo.tblPatInfoHistory p
on a.patID=p.patID and a.LogID=p.LogID
where a.Active = 1
and a.ReasonCode is not null
group by p.unitnum, p.PatNum, a.iplan)
select *,rc.Description from rrscsql2.arhighdollar.dbo.tblPatInfoHistory p
inner join cte_LatestPatInfo li
on p.PatNum=li.PatNum and p.UnitNum=li.UnitNum and p.LogID = li.LastPatRec
inner join rrscsql2.arhighdollar.dbo.tblarinfoHistory ah
on ah.LogID=p.LogID and ah.iplan = li.iplan and p.PatID=ah.PatID
left join rrscsql2.ARHighDollar.dbo.tblReasonCodes rc
on ah.ReasonCode=rc.ReasonCode,
cte_EOMDenials as
(select d.* from rrscsql2.Denials.dbo.tblDenialMonthEnd d
Inner join rrscsql3.Facilities.dbo.vwFacilities f
on d.UnitNum = f.UnitNum and f.Owner like '%LifePoint%'
Inner join rrscsql2.Denials.dbo.tblDispositionDictionary t
on d.disposition=t.disposition and DispositionType like'O%'
Where datediff(mm, monthending, GETDATE()) = 1
and DATEDIFF(mm,DischDate,monthending)>2)
left join cte_EOMDenials d
on p.unitnum=d.unitnum and p.patnum = d.patnum and p.insplan=d.iplan
答案 0 :(得分:0)
您必须首先通过单个CTE
介绍WITH
的所有,然后您可以在最终查询中使用它们:
with Cte_LatestPatInfo as
(select max(a.LogID) as LastPatRec, p.unitnum, p.PatNum, a.iplan from
rrscsql2.arhighdollar.dbo.tblarinfoHistory a
inner join rrscsql2.arhighdollar.dbo.tblPatInfoHistory p
on a.patID=p.patID and a.LogID=p.LogID
where a.Active = 1
and a.ReasonCode is not null
group by p.unitnum, p.PatNum, a.iplan),
cte_EOMDenials as
(select d.* from rrscsql2.Denials.dbo.tblDenialMonthEnd d
Inner join rrscsql3.Facilities.dbo.vwFacilities f
on d.UnitNum = f.UnitNum and f.Owner like '%LifePoint%'
Inner join rrscsql2.Denials.dbo.tblDispositionDictionary t
on d.disposition=t.disposition and DispositionType like'O%'
Where datediff(mm, monthending, GETDATE()) = 1
and DATEDIFF(mm,DischDate,monthending)>2)
select *,rc.Description from rrscsql2.arhighdollar.dbo.tblPatInfoHistory p
inner join cte_LatestPatInfo li
on p.PatNum=li.PatNum and p.UnitNum=li.UnitNum and p.LogID = li.LastPatRec
inner join rrscsql2.arhighdollar.dbo.tblarinfoHistory ah
on ah.LogID=p.LogID and ah.iplan = li.iplan and p.PatID=ah.PatID
left join rrscsql2.ARHighDollar.dbo.tblReasonCodes rc
on ah.ReasonCode=rc.ReasonCode
left join cte_EOMDenials d
on p.unitnum=d.unitnum and p.patnum = d.patnum and p.insplan=d.iplan
答案 1 :(得分:0)
声明似乎没有加入两个CTE。如果你想加入两个ctes,你可以像下面的例子那样实现它
;WITH CTE_1
as
( select field1, field2... from sometable),
CTE_2
as
(select field1, field2... from sometable),
select a.field1, a.field2, b.field1, b.field2
from CTE_1 a left join CTE_2 b on a.field1 = b.field1;
您会注意到首先定义了两个ctes,用逗号(,)分隔,然后在select语句中使用。
请参阅http://msdn.microsoft.com/en-us/library/ms175972(v=sql.90).aspx以供参考。