SQL Query getting syntax errors

Can someone review this for errors listed and see if you can tell what I am doing wrong?  I don't see why it will not run.OIG-TEST-ICN-subquery-with-error-mes.txt

Errors are

Msg 156, Level 15, State 1, Line 19
Incorrect syntax near the keyword 'on'.
Msg 102, Level 15, State 1, Line 33
Incorrect syntax near 'TST'.
OIG-TEST-ICN-subquery-with-error-mes.txt
Becky EdwardsEpic Clarity DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill PrewIT / Software Engineering ConsultantCommented:
First problem I see is that this line has an ON in it but there was no JOIN before it, that's not correct.

TTL on TTL.ICN_NO = TST.ICN_NO


»bp
Bill PrewIT / Software Engineering ConsultantCommented:
Did you perhaps mean to do:

SELECT 											
ICN_NO											
,TTL_PAID_CLM											
											
FROM											
(											
SELECT 											
cl.ICN_NO											
										
,ROW_NUMBER() OVER (PARTITION BY cl.ICN_NO ORDER BY cl.ICN_NO) RN											
--,COALESCE(cl.ICN_NO,rn)											
--,cl.IMAGE_ID											
,SUM(CLAIM_PAID_AMT) AS TTL_PAID_CLM											
FROM CL_RMT_CLM_INFO cl											
WHERE cl.INV_NO LIKE '2000030160%'											
GROUP BY cl.ICN_NO--, cl.IMAGE_ID											
HAVING SUM(CLAIM_PAID_AMT) <> 0											
)   TTL 
											
LEFT JOIN 											
(											
											
SELECT											
	cl.ICN_NO										
	,ROW_NUMBER() OVER (PARTITION BY cl.ICN_NO ORDER BY cl.ICN_NO) RN										
	,cl.IMAGE_ID										
   ,SUM(CLAIM_PAID_AMT)											  AS TTL_PAID_CLM
FROM CL_RMT_CLM_INFO cl											
WHERE cl.INV_NO LIKE '2000030160%'											
GROUP BY cl.ICN_NO, cl.IMAGE_ID											
HAVING SUM(CLAIM_PAID_AMT) <> 0											
)  TST	on TTL.ICN_NO = TST.ICN_NO 											

Open in new window


»bp

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Éric MoreauSenior .Net ConsultantCommented:
you need to move the ON clause to the JOIN statement (last line) instead of the FROM clause:

SELECT 											
	ICN_NO											
	,TTL_PAID_CLM											
												
	FROM											
	(											
	SELECT 											
	cl.ICN_NO											
												
	,ROW_NUMBER() OVER (PARTITION BY cl.ICN_NO ORDER BY cl.ICN_NO) RN											
	--,COALESCE(cl.ICN_NO,rn)											
	--,cl.IMAGE_ID											
	,SUM(CLAIM_PAID_AMT) AS TTL_PAID_CLM											
	FROM CL_RMT_CLM_INFO cl											
	WHERE cl.INV_NO LIKE '2000030160%'											
	GROUP BY cl.ICN_NO--, cl.IMAGE_ID											
	HAVING SUM(CLAIM_PAID_AMT) <> 0											
	)   TTL 										
												
	LEFT JOIN 											
	(											
												
	SELECT											
		cl.ICN_NO										
		,ROW_NUMBER() OVER (PARTITION BY cl.ICN_NO ORDER BY cl.ICN_NO) RN										
		,cl.IMAGE_ID										
	   ,SUM(CLAIM_PAID_AMT)											  AS TTL_PAID_CLM
	FROM CL_RMT_CLM_INFO cl											
	WHERE cl.INV_NO LIKE '2000030160%'											
	GROUP BY cl.ICN_NO, cl.IMAGE_ID											
	HAVING SUM(CLAIM_PAID_AMT) <> 0											
	)  TST		on TTL.ICN_NO = TST.ICN_NO 	

Open in new window

Becky EdwardsEpic Clarity DeveloperAuthor Commented:
Thank you.  That works perfectly Bill.  Thanks Eric also for the same solution that works!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
SQL

From novice to tech pro — start learning today.