Solved

rown number Delete

Posted on 2011-09-09
6
287 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
This video discusses moving either the default database or any database to a new volume.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

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

13 Experts available now in Live!

Get 1:1 Help Now