我有一个下表(TBL_VIDEO),其中“ TIMESTAMP”中的列条目重复,并且我只想在“ CAMERA”数字匹配时将其删除。
之前:
ANALYSIS_ID | TIMESTAMP | EMOTION | CAMERA
-------------------------------------------
1 | 5 | HAPPY | 1
2 | 10 | SAD | 1
3 | 10 | SAD | 1
4 | 5 | HAPPY | 2
5 | 15 | ANGRY | 2
6 | 15 | HAPPY | 2
之后:
ANALYSIS_ID | TIMESTAMP | EMOTION | CAMERA
-------------------------------------------
1 | 5 | HAPPY | 1
2 | 10 | SAD | 1
4 | 5 | HAPPY | 2
5 | 15 | ANGRY | 2
我已经尝试过此语句,但是不会相应地删除列。感谢所有帮助您生成正确的SQL语句的帮助。预先感谢!
delete y
from TBL_VIDEO y
where exists (select 1 from TBL_VIDEO y2 where y.TIMESTAMP = y2.TIMESTAMP and y2.CAMERA < y.CAMERA);
答案 0 :(得分:1)
CREATE TABLE Table12
([ANALYSIS_ID] int, [TIMESTAMP] int, [EMOTION] varchar(5))
;
INSERT INTO Table12
([ANALYSIS_ID], [TIMESTAMP], [EMOTION])
VALUES
(1, 5, 'HAPPY'),
(2, 10, 'SAD'),
(3, 10, 'SAD'),
(4, 15, 'HAPPY'),
(5, 15, 'ANGRY')
;
with cte as (select *, row_number() over (partition by emotion order by [ANALYSIS_ID] ) as rn from Table12)
delete from cte
where rn>1
select * from Table12
输出
ANALYSIS_ID TIMESTAMP EMOTION
1 5 HAPPY
2 10 SAD
5 15 ANGRY
答案 1 :(得分:0)
您有两个问题:
第二个问题是重复。
关于第一个问题,请参阅https://docs.microsoft.com/en-us/sql/t-sql/statements/delete-transact-sql?view=sql-server-2017。 (按 上的F1键)。正确的语法是
delete y
from Table12 y
where exists (
答案 2 :(得分:0)
通用SQL命令如下。您可以输入列名/条件和表名。
从删除T ( SELECT ROW_NUMBER()over(按列1分区,按列2排序)a,* FROM TABLENAME T 其中a> 1
答案 3 :(得分:0)
删除 来自TBL_VIDEO y 其中y.CAMERA <(选择y2.CAMERA 从TBL_VIDEO y2那里 y.TIMESTAMP = y2.TIMESTAMP);