i trying to set a flag based on inner join condition, i need is use order by clause in query cause my condition depend on last record with particular string but that can’t be table last, what ever i tried its says its not allowed , what so ever i tried that seems not working for me.
first Tried:
UPDATE TESTINSTANCE SET NETWORKOUTAGE='1'
WHERE TESTINSTANCEID IN (SELECT DISTINCT A.TESTINSTANCEID FROM AUDITLOG A
INNER JOIN TESTINSTANCE TI ON TI.TESTINSTANCEID=A.TESTINSTANCEID AND TI.TIMETAKEN IS NULL
AND TI.NETWORKOUTAGE!='1'
AND TI.ISSENTTOEDUNXT IS NULL
WHERE REPLACE(A.MESSAGE, ' ', '')=REPLACE('PING RESPONSE CAME FROM SERVER',' ','')
AND DATEDIFF (MINUTE, A.REPORTEDTIME, GETDATE())>=5 ORDER BY A.AUDITID DESC);
Second Tried:
UPDATE TI SET NETWORKOUTAGE='1'
FROM TESTINSTANCE TI INNER JOIN (SELECT DISTINCT A.TESTINSTANCEID FROM AUDITLOG A
INNER JOIN TESTINSTANCE TI ON TI.TESTINSTANCEID=A.TESTINSTANCEID AND TI.TIMETAKEN IS NULL
AND TI.NETWORKOUTAGE!='1'
AND TI.ISSENTTOEDUNXT IS NULL
WHERE REPLACE(A.MESSAGE, ' ', '')=REPLACE('PING RESPONSE CAME FROM SERVER',' ','')
AND DATEDIFF (MINUTE, A.REPORTEDTIME, GETDATE())>=5 ORDER BY A.AUDITID DESC) RES ON RES.TESTINSTANCEID=TI.TESTINSTANCEID;
but both giving me the same error ,
The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specified.
What i missing here ?
It’s a little tricky to see what you’re trying to do, and obviously we don’t have your tables. But I think you want something like:
And remove the date comparison from inside the subquery. Basically, make the subquery number the rows appropriately, so that, in the join condition, you can select just the latest row and use that
REPORTEDTIMEvalue.