Solved

Incorrect syntax near the keyword 'ON'.

Posted on 2011-03-07
3
469 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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:Ephraim Wangoya
Ephraim Wangoya 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

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sql query help 15 52
Need return values from a stored procedure 8 42
partitioning database after decade growth 8 56
How can I find this data? 3 25
'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
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 …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

749 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