Question

Can someone please help resolve an issue, I am left joining two CTE's?

Msg 102, Level 15, State 1, Line 19 Incorrect syntax near '('. Msg 156, Level 15, State 1, Line 26 Incorrect syntax near the keyword 'left'.

Query:

   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
Was it helpful?

Solution

You have to introduce all of your CTEs first via a single WITH, and then you can use them in a final query:

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

OTHER TIPS

The statement does not seem to join two CTEs. If you want to join two ctes you can achieve it like the following example

;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;

You will notice that the two ctes were defined first, separated by comma(,) then use in the select statement.

Please see http://msdn.microsoft.com/en-us/library/ms175972(v=sql.90).aspx for reference.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top