Solved

Incorrect syntax near the keyword 'ON'.

Posted on 2011-03-07
3
467 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

773 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