Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Row_Number With Partition keep min record of partition when count > 1 Expand / Collapse
Author
Message
Posted Monday, June 23, 2014 1:33 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, June 23, 2014 2:59 PM
Points: 9, Visits: 23
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
Post #1585262
Posted Monday, June 23, 2014 1:49 PM


SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Today @ 1:07 PM
Points: 240, Visits: 809
What have you tried?
Post #1585270
Posted Monday, June 23, 2014 1:53 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 3:06 PM
Points: 23,276, Visits: 31,996
Do you mean to delete from the result set or the table?



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)
Post #1585271
Posted Monday, June 23, 2014 2:02 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, June 23, 2014 2:59 PM
Points: 9, Visits: 23
the table the data is coming from. i will be updating the datedeleted field from null to getdate()
Post #1585280
Posted Monday, June 23, 2014 2:08 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 3:06 PM
Points: 23,276, Visits: 31,996
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





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)
Post #1585288
Posted Monday, June 23, 2014 2:12 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, June 23, 2014 2:59 PM
Points: 9, Visits: 23
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
Post #1585291
Posted Monday, June 23, 2014 2:42 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 3:06 PM
Points: 23,276, Visits: 31,996
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.



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)
Post #1585315
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse