SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Row_Number With Partition keep min record of partition when count > 1


Row_Number With Partition keep min record of partition when count > 1

Author
Message
Lee Hopkins
Lee Hopkins
SSC-Enthusiastic
SSC-Enthusiastic (167 reputation)SSC-Enthusiastic (167 reputation)SSC-Enthusiastic (167 reputation)SSC-Enthusiastic (167 reputation)SSC-Enthusiastic (167 reputation)SSC-Enthusiastic (167 reputation)SSC-Enthusiastic (167 reputation)SSC-Enthusiastic (167 reputation)

Group: General Forum Members
Points: 167 Visits: 50
I have a query that is bringing back something like this.
if there is only one row for a given city they i want to delete it no mater what the row number is.
however if there is more then on row number for a given city i want to keep only the min row number and delete the others.

Row Recnum City Action
1 5 City A Delete
3 6 City B Delete
1 7 City C Delete
1 8 City E Keep
2 8 City E Delete
3 8 City E Delete
1 9 City F Keep
2 9 City F Delete
1 10 City G Keep
1 10 City G Delete
1 11 City H Keep
2 11 City H Delete
3 11 City H Delete
4 11 City H Delete
Dana
Dana
SSCertifiable
SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)

Group: General Forum Members
Points: 5786 Visits: 3450
What have you tried?
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (92K reputation)SSC Guru (92K reputation)SSC Guru (92K reputation)SSC Guru (92K reputation)SSC Guru (92K reputation)SSC Guru (92K reputation)SSC Guru (92K reputation)SSC Guru (92K reputation)

Group: General Forum Members
Points: 92119 Visits: 38954
Do you mean to delete from the result set or the table?

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Lee Hopkins
Lee Hopkins
SSC-Enthusiastic
SSC-Enthusiastic (167 reputation)SSC-Enthusiastic (167 reputation)SSC-Enthusiastic (167 reputation)SSC-Enthusiastic (167 reputation)SSC-Enthusiastic (167 reputation)SSC-Enthusiastic (167 reputation)SSC-Enthusiastic (167 reputation)SSC-Enthusiastic (167 reputation)

Group: General Forum Members
Points: 167 Visits: 50
the table the data is coming from. i will be updating the datedeleted field from null to getdate()
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (92K reputation)SSC Guru (92K reputation)SSC Guru (92K reputation)SSC Guru (92K reputation)SSC Guru (92K reputation)SSC Guru (92K reputation)SSC Guru (92K reputation)SSC Guru (92K reputation)

Group: General Forum Members
Points: 92119 Visits: 38954
So something like this:



/*
1 5 City A Delete
3 6 City B Delete
1 7 City C Delete
1 8 City E Keep
2 8 City E Delete
3 8 City E Delete
1 9 City F Keep
2 9 City F Delete
1 10 City G Keep
1 10 City G Delete
1 11 City H Keep
2 11 City H Delete
3 11 City H Delete
4 11 City H Delete
*/

create table dbo.City(
CityId int identity(1,1),
CityName varchar(10),
DateDeleted datetime null
);

insert into dbo.City(CityName)
values
('City A'),
('City B'),
('City C'),
('City E'),
('City E'),
('City E'),
('City F'),
('City F'),
('City G'),
('City G'),
('City H'),
('City H'),
('City H'),
('City H');

with BaseCity as (
select
CityId,
CityName,
rn = row_number() over (partition by CityName order by CityId),
cnt = count(*) over (partition by CityName)
from
dbo.City
)
select
*
from
BaseCity
where
rn > 1 or
cnt = 1;
go

with BaseCity as (
select
CityId,
CityName,
DateDeleted,
rn = row_number() over (partition by CityName order by CityId),
cnt = count(*) over (partition by CityName)
from
dbo.City
)
update BaseCity set
DateDeleted = getdate()
where
rn > 1 or
cnt = 1;
go

select * from dbo.City;
go

drop table dbo.City;
go




Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Lee Hopkins
Lee Hopkins
SSC-Enthusiastic
SSC-Enthusiastic (167 reputation)SSC-Enthusiastic (167 reputation)SSC-Enthusiastic (167 reputation)SSC-Enthusiastic (167 reputation)SSC-Enthusiastic (167 reputation)SSC-Enthusiastic (167 reputation)SSC-Enthusiastic (167 reputation)SSC-Enthusiastic (167 reputation)

Group: General Forum Members
Points: 167 Visits: 50
just the query to get my counts back by city.

my problem is i the record i need to keep could be say row 5 of a partition (of 7). so i would need delete 1,2,3,4,6,7 and keep row 5
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (92K reputation)SSC Guru (92K reputation)SSC Guru (92K reputation)SSC Guru (92K reputation)SSC Guru (92K reputation)SSC Guru (92K reputation)SSC Guru (92K reputation)SSC Guru (92K reputation)

Group: General Forum Members
Points: 92119 Visits: 38954
Lee Hopkins (6/23/2014)
just the query to get my counts back by city.

my problem is i the record i need to keep could be say row 5 of a partition (of 7). so i would need delete 1,2,3,4,6,7 and keep row 5


Well, since I don't have the data you have I had to make do with what I set up. Take what I did and see what you need to do to make it work with your data.

If you still have problems, post the DDL and sample data (look at how I did that in my post) and we will see what we can do.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search