July 23, 2018 at 3:07 am
I have a table full of episodes. So for one episode there might be 10 records. I want to delete all records apart from the original. Current I have a temp table where I have extracted the records I want to delete. Then I use the primary key.
But I was wondering if there is an alternative?
Example
Turn this
Primarykey, record , episode
1,1,1
2,2,1
3,3,1
4,1,2
5,2,2
6,3,2
7,1,3
8,2,3
9,3,3
To this
1,1,1
4,1,2
7,1,3
July 23, 2018 at 3:35 am
Delete table
Where record <> 1
Looks like it would do it.
The absence of evidence is not evidence of absence
- Martin Rees
The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
- Phil Parkin
July 23, 2018 at 5:57 am
Phil Parkin - Monday, July 23, 2018 3:35 AMDelete table
Where record <> 1
Looks like it would do it.
ahh yes , missed some details unfortunately the start number inst always '1' but the sequence is maintained what ever number you start at
July 23, 2018 at 6:33 am
Edward-445599 - Monday, July 23, 2018 3:07 AMI have a table full of episodes. So for one episode there might be 10 records. I want to delete all records apart from the original. Current I have a temp table where I have extracted the records I want to delete. Then I use the primary key.
But I was wondering if there is an alternative?Example
Turn this
Primarykey, record , episode
1,1,1
2,2,1
3,3,1
4,1,2
5,2,2
6,3,2
7,1,3
8,2,3
9,3,3
To this1,1,1
4,1,2
7,1,3
How about this?create table seq
(
Primarykey int, record int , episode int)insert into seq values(1,1,1);
insert into seq values(2,2,1);
insert into seq values(3,3,1);
insert into seq values(4,1,2);
insert into seq values(5,2,2);
insert into seq values(6,3,2);
insert into seq values(7,1,3);
insert into seq values(8,2,3);
insert into seq values(9,3,3);select Primarykey,record,episode from
(
select *,
row_number ()over(partition by episode order by record) rnk from seq
)sequence
where rnk=1
Primarykey record episode
1 1 1 4 1 2 7 1 3 3 rows (showing 1 to 3)
Saravanan
July 23, 2018 at 6:54 am
Here is the DELETE version of the query posted above:
DROP TABLE IF EXISTS #seq;
CREATE TABLE #seq
(
Primarykey INT NOT NULL PRIMARY KEY CLUSTERED,
record INT NOT NULL,
episode INT NOT NULL
);
INSERT #seq
(
Primarykey,
record,
episode
)
VALUES
(1, 1, 1),
(2, 2, 1),
(3, 3, 1),
(4, 1, 2),
(5, 2, 2),
(6, 3, 2),
(7, 1, 3),
(8, 2, 3),
(9, 3, 3);
WITH ordered
AS (SELECT rnk = ROW_NUMBER() OVER (PARTITION BY s.episode ORDER BY s.record)
FROM #seq s)
DELETE o
FROM ordered o
WHERE o.rnk > 1;
SELECT *
FROM #seq s;
The absence of evidence is not evidence of absence
- Martin Rees
The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
- Phil Parkin
July 23, 2018 at 7:01 am
Phil Parkin - Monday, July 23, 2018 6:54 AMHere is the DELETE version of the query posted above:
DROP TABLE IF EXISTS #seq;
CREATE TABLE #seq
(
Primarykey INT NOT NULL PRIMARY KEY CLUSTERED,
record INT NOT NULL,
episode INT NOT NULL
);INSERT #seq
(
Primarykey,
record,
episode
)
VALUES
(1, 1, 1),
(2, 2, 1),
(3, 3, 1),
(4, 1, 2),
(5, 2, 2),
(6, 3, 2),
(7, 1, 3),
(8, 2, 3),
(9, 3, 3);WITH ordered
AS (SELECT s.Primarykey,
rnk = ROW_NUMBER() OVER (PARTITION BY s.episode ORDER BY s.record)
FROM #seq s)
DELETE s
FROM #seq s
JOIN ordered o
ON s.Primarykey = o.Primarykey
WHERE o.rnk > 1;SELECT *
FROM #seq s;
I didn't read the problem statements .I just saw the expected output. Good one
Saravanan
July 24, 2018 at 7:26 am
wow ace thank you so much a very neat solution, I do not suppose, you can point me in the direct of a tutorial which explains whats going on here?
July 24, 2018 at 8:48 am
Edward-445599 - Tuesday, July 24, 2018 7:26 AMwow ace thank you so much a very neat solution, I do not suppose, you can point me in the direct of a tutorial which explains whats going on here?
There are a couple of things to get to grips with.
Post back if you'd like any further explanation.
The absence of evidence is not evidence of absence
- Martin Rees
The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
- Phil Parkin
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply