Solved

Incorrect syntax near the keyword 'ON'.

Posted on 2011-03-07
3
464 Views
Last Modified: 2012-05-11
Msg 156, Level 15, State 1, Line 44
Incorrect syntax near the keyword 'ON'.

Not sure how to resolve this error. I am trying to join in the #Tmp.PatientProfileId. Any help is appreciated.
SET NOCOUNT ON

CREATE TABLE #Tmp ( PatientProfileId INT ) 
	
INSERT  INTO #Tmp
        SELECT DISTINCT
                PatientProfileID
        FROM    PatientProfile pp
                JOIN Orders o ON pp.PID = o.PID
                JOIN Orddx ON o.dxgroupid = orddx.dxgroupid
                JOIN DOCUMENT d ON o.SDID = d.SDID
        WHERE   ( orddx.dxCode LIKE ( 'icd-250.%' )
                  OR orddx.dxCode LIKE ( 'icd-251.%' )
                )
                AND o.XID = 1000000000000000000
                AND ( d.Change IS NULL
                      OR d.Change NOT IN ( 10, 11, 12 )
                    )
                    
SELECT  *
FROM    ( SELECT    *,
                    ROW_NUMBER() OVER ( PARTITION BY PatientId ORDER BY obsdate DESC ) rn
          FROM      ( SELECT    o.pid,
                                pp.PatientId,
                                pp.PatientProfileId,
                                dbo.FORMATNAME('', pp.FIRST, pp.Middle,
                                               pp.LAST, pp.Suffix) AS PatientName,
                                MAX(o.OBSDate) AS OBSDate,
                                o.OBSVALUE
                      FROM      OBS AS o
                                JOIN OBSHEAD AS oh ON o.HDID = oh.HDID
                                JOIN PatientProfile AS pp ON o.PID = pp.PId
                      WHERE     o.HDID = 28
                                AND ( o.State IS NULL
                                      OR o.STATE NOT IN ( 'D', 'R', 'I', 'P',
                                                          'S' )
                                    )
                      GROUP BY  o.PID,
                                pp.PatientId,
                                pp.PatientProfileId,
                                dbo.FORMATNAME('', pp.FIRST, pp.Middle,
                                               pp.LAST, pp.Suffix),
                                o.OBSVALUE
                    ) t1 ON #tmp.PatientProfileId = t1.PatientProfileId 
        ) t2 
WHERE   rn = 1
ORDER BY PatientName               
                    
SELECT  *
FROM    #Tmp                   
 
DROP TABLE #Tmp

Open in new window

0
Comment
Question by:Jeff S
3 Comments
 
LVL 11

Expert Comment

by:JoeNuvo
ID: 35063540
                    ) t1 ON #tmp.PatientProfileId = t1.PatientProfileId 

Open in new window


I guess you are forget to put #tmp to join with t1
so, keyword ON is error there.
0
 
LVL 32

Assisted Solution

by:ewangoya
ewangoya earned 250 total points
ID: 35063550

You need to join it
SET NOCOUNT ON

CREATE TABLE #Tmp ( PatientProfileId INT ) 
	
INSERT  INTO #Tmp
        SELECT DISTINCT
                PatientProfileID
        FROM    PatientProfile pp
                JOIN Orders o ON pp.PID = o.PID
                JOIN Orddx ON o.dxgroupid = orddx.dxgroupid
                JOIN DOCUMENT d ON o.SDID = d.SDID
        WHERE   ( orddx.dxCode LIKE ( 'icd-250.%' )
                  OR orddx.dxCode LIKE ( 'icd-251.%' )
                )
                AND o.XID = 1000000000000000000
                AND ( d.Change IS NULL
                      OR d.Change NOT IN ( 10, 11, 12 )
                    )
                    
SELECT  *
FROM    ( SELECT    *,
                    ROW_NUMBER() OVER ( PARTITION BY PatientId ORDER BY obsdate DESC ) rn
          FROM      ( SELECT    o.pid,
                                pp.PatientId,
                                pp.PatientProfileId,
                                dbo.FORMATNAME('', pp.FIRST, pp.Middle,
                                               pp.LAST, pp.Suffix) AS PatientName,
                                MAX(o.OBSDate) AS OBSDate,
                                o.OBSVALUE
                      FROM      OBS AS o
                                JOIN OBSHEAD AS oh ON o.HDID = oh.HDID
                                JOIN PatientProfile AS pp ON o.PID = pp.PId
                      WHERE     o.HDID = 28
                                AND ( o.State IS NULL
                                      OR o.STATE NOT IN ( 'D', 'R', 'I', 'P',
                                                          'S' )
                                    )
                      GROUP BY  o.PID,
                                pp.PatientId,
                                pp.PatientProfileId,
                                dbo.FORMATNAME('', pp.FIRST, pp.Middle,
                                               pp.LAST, pp.Suffix),
                                o.OBSVALUE
                    ) t2
               
        ) t1
inner join  #tmp t3 ON t3.PatientProfileId = t1.PatientProfileId          
WHERE   rn = 1
ORDER BY PatientName

Open in new window

0
 

Accepted Solution

by:
JoelDev earned 250 total points
ID: 35063564
Hi Jeff,

Looks like you're just missing the inner join on your second select statement, so you are never joining your sub query to the temp table before you use the ON statement. Something like this should fix the problem:

( SELECT    *,
                    ROW_NUMBER() OVER ( PARTITION BY PatientId ORDER BY obsdate DESC ) rn
          FROM      ( SELECT    o.pid,
                                pp.PatientId,
                                pp.PatientProfileId,
                                dbo.FORMATNAME('', pp.FIRST, pp.Middle,
                                               pp.LAST, pp.Suffix) AS PatientName,
                                MAX(o.OBSDate) AS OBSDate,
                                o.OBSVALUE
                      FROM      OBS AS o
                                JOIN OBSHEAD AS oh ON o.HDID = oh.HDID
                                JOIN PatientProfile AS pp ON o.PID = pp.PId
                      WHERE     o.HDID = 28
                                AND ( o.State IS NULL
                                      OR o.STATE NOT IN ( 'D', 'R', 'I', 'P',
                                                          'S' )
                                    )
                      GROUP BY  o.PID,
                                pp.PatientId,
                                pp.PatientProfileId,
                                dbo.FORMATNAME('', pp.FIRST, pp.Middle,
                                               pp.LAST, pp.Suffix),
                                o.OBSVALUE
                    ) t1 
                    INNER JOIN #tmp t ON t.PatientProfileId = t1.PatientProfileId 

Open in new window



Hope that helps,
Joel
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
A short film showing how OnPage and Connectwise integration works.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now