Solved

Incorrect syntax near the keyword 'ON'.

Posted on 2011-03-07
3
459 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
Comment Utility
                    ) 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
Comment Utility

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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 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

18 Experts available now in Live!

Get 1:1 Help Now