我有下表:
ID User Form Depth
1 A ABC 2001
1 A XYZ 1001
1 B XYZ 1003
1 B DEF 3001
1 C XYZ 1000
如果ID和Form相同,则需要从用户A识别出+ -2的行。使用上面的示例,脚本将返回:
ID User Form Depth
1 B XYZ 1003
1 C XYZ 1000
我已经有了一个脚本,该脚本可以标识具有相同ID和格式的行-我只需要另一部分,但是我在努力弄清楚逻辑。我希望可以使用某种DIFF函数,但是找不到适用于SQL Anywhere的函数。
有人有什么建议吗?
谢谢!
答案 0 :(得分:1)
如果您要寻找的深度恰好是A的深度的+/- 2:
select t1.*
from mytab t1,
mytab t2
where t1.id = t2.id
and t1.form = t2.form
and t1.user != 'A'
and t2.user = 'A'
and abs(t1.depth - t2.depth) = 2
go
ID User Form Depth
--- ----- ----- -----
1 B XYZ 1003
如果您要寻找的深度在A的深度的2倍以内(即diff <= 2):
select t1.*
from mytab t1,
mytab t2
where t1.id = t2.id
and t1.form = t2.form
and t1.user != 'A'
and t2.user = 'A'
and abs(t1.depth - t2.depth) <= 2
go
ID User Form Depth
--- ----- ----- -----
1 B XYZ 1003
1 C XYZ 1000
这是非常基本的SQL,因此虽然这是通过MySQL完成的,但您也应该在SQLAnywhere中发现查询工作:sql fiddle
答案 1 :(得分:0)
我认为您想要exists
:
select t.*
from t
where t.user <> 'A' and
exists (select 1
from t t2
where t2.form = t.form and t2.id = t.id and
t2.depth between t.depth - 2 and t.depth + 2
);
答案 2 :(得分:0)
一种快速而肮脏的通用方法。
用您想删除的人替换@User
。
DECLARE @table TABLE (
ID Int
,[User] VARCHAR(2)
,Form VARCHAR(3)
,Depth INT
)
DECLARE @User VARCHAR(2) = 'A'
INSERT INTO @table (ID , [User], Form, Depth)
VALUES
(1 , 'A' , 'ABC' , 2001),
(1 , 'A' , 'XYZ' , 1001),
(1 , 'B' , 'XYZ' , 1003),
(1 , 'B' , 'DEF' , 3001),
(1 , 'C' , 'XYZ' , 1000)
SELECT t1.ID, t1.[User], t1.Form, t1.Depth , ROW_NUMBER() OVER(ORDER BY t1.ID, t1.[User], t1.Form, t1.Depth) AS [row_number]
INTO #temp
FROM @table as t1
INNER JOIN (
SELECT t.ID, t.Form, COUNT('8') as [count]
FROM @table as t
GROUP BY ID, Form
HAVING COUNT('8') > 1
) as duplicates
ON duplicates.ID = t1.ID
AND duplicates. Form = t1.Form
ORDER BY ID, User, Form, Depth
-- SELECT * FROM #temp
SELECT [row_number] - 2 as value
INTO #range
FROM #temp as t
WHERE t.[User] = @User
--SELECT * FROM #range
INSERT INTO #range
SELECT [row_number] - 1
FROM #temp as t
WHERE t.[User] = @User
INSERT INTO #range
SELECT [row_number] + 1
FROM #temp as t
WHERE t.[User] = @User
INSERT INTO #range
SELECT [row_number] + 2
FROM #temp as t
WHERE t.[User] = @User
SELECT * FROM #temp
WHERE [row_number] IN (SELECT value FROM #range)
DROP TABLE #temp
DROP TABLE #range