SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Incorrect Syntax!?!
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Spica66
Starting Member

23 Posts

Posted - 07/26/2012 :  07:52:50  Show Profile  Reply with Quote
Can anyone tell me why this statement has "Incorrect syntax near the keyword 'WHERE'." ?

SELECT c.NFCUSTOMER, c.CLAIMID

FROM NFWARRANTYCLAIMS c,

(SELECT MAX(h.TRANSDATE) TRANSDATE

FROM NFWARRANTYCLAIMHISTORY h

WHERE NFCLAIMSTATUS=20

GROUP BY NFCLAIMID)

WHERE c.CLAIMID = h.NFCLAIMID;

SwePeso
Patron Saint of Lost Yaks

Sweden
29156 Posts

Posted - 07/26/2012 :  08:01:21  Show Profile  Visit SwePeso's Homepage  Reply with Quote
Your subquery needs an alias.

SELECT c.NFCUSTOMER, c.CLAIMID
FROM NFWARRANTYCLAIMS c,
(SELECT MAX(h.TRANSDATE) TRANSDATE
FROM NFWARRANTYCLAIMHISTORY h
WHERE NFCLAIMSTATUS=20
GROUP BY NFCLAIMID) AS h
WHERE c.CLAIMID = h.NFCLAIMID;


N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

Sweden
29156 Posts

Posted - 07/26/2012 :  08:03:33  Show Profile  Visit SwePeso's Homepage  Reply with Quote
But on the other hand, your query cannot run anyway since there are more syntax errors.
Try this one
SELECT		c.NFCUSTOMER,
		c.CLAIMID,
		h.TRANSDATE
FROM		dbo.NFWARRANTYCLAIMS AS c
INNER JOIN	(
			SELECT		NFCLAIMID,
					MAX(TRANSDATE) AS TRANSDATE 
			FROM		dbo.NFWARRANTYCLAIMHISTORY
			WHERE		NFCLAIMSTATUS = 20 
			GROUP BY	NFCLAIMID
		) AS h ON h.NFCLAIMID = c.CLAIMID;



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

Spica66
Starting Member

23 Posts

Posted - 07/26/2012 :  08:05:27  Show Profile  Reply with Quote
Thank you VERY much!!!
Go to Top of Page

Spica66
Starting Member

23 Posts

Posted - 07/26/2012 :  09:11:11  Show Profile  Reply with Quote
This worked perfectly:
SELECT c.NFCUSTOMER,
c.CLAIMID,
h.TRANSDATE
FROM dbo.NFWARRANTYCLAIMS AS c
INNER JOIN (
SELECT NFCLAIMID,
MAX(TRANSDATE) AS TRANSDATE
FROM dbo.NFWARRANTYCLAIMHISTORY
WHERE NFCLAIMSTATUS = 20
GROUP BY NFCLAIMID
) AS h ON h.NFCLAIMID = c.CLAIMID;

Now I need to get a few more columns from a third table WARRANTYCLAIMSTABLE t. I can use the c.CLAIMID as the key. How would I do a second join?
Go to Top of Page

Spica66
Starting Member

23 Posts

Posted - 07/26/2012 :  09:26:08  Show Profile  Reply with Quote
I tried this:

SELECT c.NFCUSTOMER,
c.CLAIMID,
h.TRANSDATE,
t.HRVYEARID,
t.HRVMODELID,
t.HRVBRANDID


FROM dbo.NFWARRANTYCLAIMS AS c

INNER JOIN (
SELECT NFCLAIMID,
MAX(TRANSDATE) AS TRANSDATE
FROM NFWARRANTYCLAIMHISTORY
WHERE NFCLAIMSTATUS = 20
GROUP BY NFCLAIMID
) AS h ON h.NFCLAIMID = c.CLAIMID

LEFT JOIN dbo.NFWARRANTYTABLE t
ON c.CLAIMID = t.INVENTSERIALID


but the three new values (columns) came up with all null values and I know that there are values that match.

Edited by - Spica66 on 07/26/2012 09:27:17
Go to Top of Page

webfred
Flowing Fount of Yak Knowledge

Germany
8529 Posts

Posted - 07/26/2012 :  09:32:25  Show Profile  Visit webfred's Homepage  Reply with Quote
Check the datatypes of c.CLAIMID and t.INVENTSERIALID.
Maybe the datatypes are different and then it is possible that they are not equal because of leading zeroes or whatever...


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

Sweden
29156 Posts

Posted - 07/26/2012 :  09:34:39  Show Profile  Visit SwePeso's Homepage  Reply with Quote
Don't forget that the derived table h filters out all nfclaimid NOT having a claimstatus of 20 (due to the inner join)!
SELECT		c.NFCUSTOMER,
		c.CLAIMID,
		h.TRANSDATE,
		t.HRVYEARID,
		t.HRVMODELID,
		t.HRVBRANDID
FROM		dbo.NFWARRANTYCLAIMS AS c
LEFT JOIN	(
			SELECT		NFCLAIMID,
					MAX(TRANSDATE) AS TRANSDATE 
			FROM		dbo.NFWARRANTYCLAIMHISTORY
			WHERE		NFCLAIMSTATUS = 20 
			GROUP BY	NFCLAIMID
		) AS h ON h.NFCLAIMID = c.CLAIMID
LEFT JOIN	dbo.NFWARRANTYTABLE AS t ON t.INVENTSERIALID = c.CLAIMID



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000