Solved

rown number Delete

Posted on 2011-09-09
6
290 Views
Last Modified: 2012-05-12

What is wrong with my delete

getting this error
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '('.
Msg 102, Level 15, State 1, Line 5
Incorrect syntax near 'sp'.

Delete from (
Select *,
Row_NUmber() over (partition by EmailAddress order by CreationTime Desc) rn 
from OpenClickThru where EmailAddress is Not NULL
) sp
Where sp.rn > 1 ;

Open in new window

0
Comment
Question by:Leo Torres
  • 2
  • 2
  • 2
6 Comments
 
LVL 42

Accepted Solution

by:
dqmq earned 250 total points
ID: 36514248
Try this:


;with cte as
(
Select *, Row_NUmber() over (partition by EmailAddress order by CreationTime Desc) rn
from OpenClickThru)
delete from cte
where EmailAddress is Not NULL and rn >1
0
 
LVL 22

Assisted Solution

by:Thomasian
Thomasian earned 250 total points
ID: 36515011
To correct the syntax of your query:
Delete sp
FROM (Select *,
			Row_NUmber() over (partition by EmailAddress order by CreationTime Desc) rn 
		from OpenClickThru where EmailAddress is Not NULL
	) sp
Where sp.rn > 1 

Open in new window

0
 
LVL 8

Author Comment

by:Leo Torres
ID: 36515532
dqmq: yours worked fine

But I am still curious because I would like to know why
Thomasian: did not work this was what I was tring to do there must be a way to do this with out CTE right?

Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'Select'.
Msg 102, Level 15, State 1, Line 5
Incorrect syntax near ')'.
0
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
LVL 22

Expert Comment

by:Thomasian
ID: 36515537
Did you run the query as is? Maybe you left out the word "FROM" since I get the same error without it.
0
 
LVL 8

Author Comment

by:Leo Torres
ID: 36515687
Ha that's what it was..

Thanks
0
 
LVL 42

Expert Comment

by:dqmq
ID: 36516472
Original query not missing "FROM", missing "SP" before the "FROM". :>)
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MS SQL Update query with connected table data 3 41
Sum particular field in database 2 16
sql server query 12 26
Current Month Filter in Visual Studio 10 24
In database programming, custom sort order seems to be necessary quite often, at least in my experience and time here at EE. Within the realm of custom sorting is the sorting of numbers and text independently (i.e., treating the numbers as number…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

820 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