January 19, 2017 at 4:00 pm
I have the following data set.
Sequence CurrentStatusId PreviousStatusId
1 1 1
2 1 1
3 1 9
4 9 1
5 10 9
The previousStatusId of the sequence 3 is not same as the currentStatusID of the sequence 2. So I need to delete the Sequence 3 from this data set and I need to get the following result. There is a chance that multiple records can be like this and I need to delete all of them.
Sequence CurrentStatusId PreviousStatusId
1 1 1
2 1 1
4 9 1
5 10 9
I am using a cursor to this but the performance is really bad.
Can any one provide a query to do this?
Thanks in advance.
January 19, 2017 at 5:03 pm
sql_2005_fan - Thursday, January 19, 2017 4:00 PMI have the following data set.Sequence CurrentStatusId PreviousStatusId
1 1 1
2 1 1
3 1 9
4 9 1
5 10 9The previousStatusId of the sequence 3 is not same as the currentStatusID of the sequence 2. So I need to delete the Sequence 3 from this data set and I need to get the following result. There is a chance that multiple records can be like this and I need to delete all of them.
Sequence CurrentStatusId PreviousStatusId
1 1 1
2 1 1
4 9 1
5 10 9I am using a cursor to this but the performance is really bad.
Can any one provide a query to do this?Thanks in advance.
Here's one:
IF OBJECT_ID('tempdb..#tbl', 'U') IS NOT NULL
DROP TABLE #tbl;
CREATE TABLE #tbl
(
Seq INT PRIMARY KEY CLUSTERED,
CurrentStatusId INT,
PreviousStatusId INT
);
INSERT #tbl
(
Seq,
CurrentStatusId,
PreviousStatusId
)
VALUES
(1, 1, 1),
(2, 1, 1),
(3, 1, 9),
(4, 9, 1),
(5, 10, 9);
SELECT *
FROM #tbl t;
WITH LaggedData
AS (SELECT t.Seq,
t.PreviousStatusId,
TruePrevStatId = LAG(t.CurrentStatusId, 1, 1) OVER (ORDER BY t.Seq)
FROM #tbl t
)
DELETE t
FROM #tbl t
JOIN LaggedData ld
ON t.Seq = ld.Seq
WHERE ld.PreviousStatusId <> ld.TruePrevStatId;
SELECT *
FROM #tbl t;
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
January 20, 2017 at 8:31 am
I think that we need a better data set if we're going to give a good answer. Your current data set only has one record that will be deleted. We need a record set that is more complicated. Specifically, we need records that are affected by the deletion of previous records. Specifically, record 4 has the same results whether it uses record 3 (before the deletion) or record 2 (after the deletion), but we need examples where the results change.
When setting up the sample data, you should use a script to create the table followed by insert statements (like Phil did in his solution).
Drew
J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
January 20, 2017 at 9:48 am
Sure. I will do in the future.
Thanks for the help. It worked really well.
February 6, 2017 at 4:27 am
Can anyone help me to have the below query with not having the clause 'LaggedData'. I am not able to use this on SQL server 2000 instance.
Thanks.
IF OBJECT_ID('tempdb..#tbl', 'U') IS NOT NULL
DROP TABLE #tbl;
CREATE TABLE #tbl
(
Seq INT PRIMARY KEY CLUSTERED,
CurrentStatusId INT,
PreviousStatusId INT
);
INSERT #tbl
(
Seq,
CurrentStatusId,
PreviousStatusId
)
VALUES
(1, 1, 1),
(2, 1, 1),
(3, 1, 9),
(4, 9, 1),
(5, 10, 9);
SELECT *
FROM #tbl t;
WITH LaggedData
AS (SELECT t.Seq,
t.PreviousStatusId,
TruePrevStatId = LAG(t.CurrentStatusId, 1, 1) OVER (ORDER BY t.Seq)
FROM #tbl t
)
DELETE t
FROM #tbl t
JOIN LaggedData ld
ON t.Seq = ld.Seq
WHERE ld.PreviousStatusId <> ld.TruePrevStatId;
SELECT *
FROM #tbl t;
February 6, 2017 at 4:50 am
sql_2005_fan - Monday, February 6, 2017 4:27 AMCan anyone help me to have the below query with not having the clause 'LaggedData'. I am not able to use this on SQL server 2000 instance.
The LAG function first appeared in SQL Server 2012, and the Common Table Expression (CTE) in SQL Server 2005, so there's two things you have to change. It'll only work if you don't have any gaps in the values in the Seq column. If you do, it's much more complicated.
DELETE t
FROM #tbl t
JOIN ( -- subquery instead of CTE
SELECT t1.Seq,
t1.PreviousStatusId,
TruePrevStatId = COALESCE(t2.PreviousStatusId,1)
FROM #tbl t1
LEFT JOIN #tbl t2 ON t1.Seq = t2.Seq + 1 -- self-join instead of LAG function
) ld on t.Seq = ld.Seq
WHERE ld.PreviousStatusId <> ld.TruePrevStatId;
John
February 6, 2017 at 7:44 am
Now that you've got a couple of answers, I have to ask... who is fixing the original code that caused this problem to begin with?
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply