我有以下查询,我需要让它在CTREESQL中工作,但直到现在都没有成功:
delete c
from (select col1, col2,right(rtrim(col3),1) as col3
FROM table
GROUP BY col1, col2,right(rtrim(col3),1)
having COUNT(NULLIF(ltrim(col4), '')) =1 and COUNT(*)=2)d join table c on
c.col1 = d.col1 and c.col2= d.col2 and right(rtrim(c.col3),1) = d.col3
where c.col4 is not null and LTRIM(c.col4)<>''
以下查询适用于带连接的选择:
select c.*
from (select col1, col2,right(rtrim(col3),1) as col3
FROM table
GROUP BY col1, col2,right(rtrim(col3),1)
having COUNT(NULLIF(ltrim(col4), '')) =1 and COUNT(*)=2)d join table c on
c.col1 = d.col1 and c.col2= d.col2 and right(rtrim(c.col3),1) = d.col3
where c.col4 is not null and LTRIM(c.col4)<>''
答案 0 :(得分:0)
在周末经过长时间的斗争后,我能够让它发挥作用。
Create Table admin.dupes( col1, col2, col3 )
AS
select col1, col2,right(rtrim(col3),1) as col3
FROM table
GROUP BY col1, col2,right(rtrim(col3),1)
having COUNT(NULLIF(ltrim(col4), '')) =1 and COUNT(*)=2
delete from table
where
table.col4 is not null and LTRIM(table.col4)<>''
and exists (select 1 from dupes d where table.col1 = d.col1 and table.col2= d.col2 and right(rtrim(table.col3),1) = d.col3
)
Drop Table admin.dupes
我无法使用派生表/临时表,因为两者都无效。