我有如下的SQL架构:
SELECT * FROM Table where rowid in(1,2,3)
SELECT * FROM Table where rowid in(2,1,3)
SELECT * FROM Table where rowid in(3,2,1)
这里我想得到多行相等的记录。条件是ANo,Code,Type,CPS,Tax需要直接比较。并且,如果Ex_Date和Rd_Date彼此相等或相差10天,那么它将被视为重复。注意:我想忽略Date1列。
所以,上面的架构应该是这样的:
ANo | Code |Type | Date1 | EX_Date|Rd_Date |CPS | Tax
______|_______| _____|______ |_ ______|_______ |____|__________
B1 | AM | T |4/1/2014|3/2/2012|4/2/2010| 99 | 1
B1 | AM | T |5/2/2014|3/5/2012|4/2/2010| 99 | 1
p1 | 4 | X |5/8/2014|3/4/2012|5/9/2015| 22 | 2
p2 | 3 | Y |8/1/2015|5/6/2011|7/8/2010| 20 | 3
它会很棒,如果有人能帮我PLSQL.Sql服务器也行。
答案 0 :(得分:1)
如果你需要显示副本,它将类似于
select t1.* from Test t1
join Test t2 on 1=1
and t2.ANo = t1.ANo
and t2.Code = t1.Code
and t2."Type" = t1."Type"
and t2.CPS = t1.CPS
and t2.Tax = t1.Tax
and abs(t1.EX_Date - t2.EX_Date) <= 10
and abs(t1.Rd_Date - t2.Rd_Date) <= 10
group by t1.ANo, t1.Code,t1."Type", t1.Date1, t1.EX_Date,t1.Rd_Date,t1.CPS, t1.Tax
having count(t1.*) > 1;
答案 1 :(得分:1)
Oracle 11g R2架构设置:
CREATE TABLE Test ( ANo, Code,"Type", Date1, EX_Date,Rd_Date,CPS, Tax ) AS
SELECT 'B1', 'AM', 'T', TO_DATE( '2014-04-01', 'YYYY-MM-DD' ), TO_DATE( '2012-03-02', 'YYYY-MM-DD' ), TO_DATE( '2010-04-02', 'YYYY-MM-DD' ), 99, 1 FROM DUAL
UNION ALL SELECT 'B1', 'AM', 'T', TO_DATE( '2014-05-02', 'YYYY-MM-DD' ), TO_DATE( '2012-03-05', 'YYYY-MM-DD' ), TO_DATE( '2010-04-02', 'YYYY-MM-DD' ), 99, 1 FROM DUAL
UNION ALL SELECT 'p1', '4', 'X', TO_DATE( '2014-05-08', 'YYYY-MM-DD' ), TO_DATE( '2012-03-04', 'YYYY-MM-DD' ), TO_DATE( '2015-05-09', 'YYYY-MM-DD' ), 22, 2 FROM DUAL
UNION ALL SELECT 'p2', '3', 'Y', TO_DATE( '2015-08-01', 'YYYY-MM-DD' ), TO_DATE( '2011-05-06', 'YYYY-MM-DD' ), TO_DATE( '2010-07-08', 'YYYY-MM-DD' ), 20, 3 FROM DUAL;
查询1 :
SELECT *
FROM TEST t
WHERE EXISTS (
SELECT 'X'
FROM TEST x
WHERE x.ROWID <> t.ROWID
AND x.ANo = t.Ano
AND x.Code = t.Code
AND x."Type" = t."Type"
AND x.cps = t.cps
AND x.tax = t.tax
AND x.EX_DATE BETWEEN t.EX_DATE - INTERVAL '10' DAY AND t.EX_DATE + INTERVAL '10' DAY
AND x.RD_DATE BETWEEN t.RD_DATE - INTERVAL '10' DAY AND t.RD_DATE + INTERVAL '10' DAY
)
<强> Results 强>:
| ANO | CODE | Type | DATE1 | EX_DATE | RD_DATE | CPS | TAX |
|-----|------|------|-------------------------|-------------------------|-------------------------|-----|-----|
| B1 | AM | T | May, 02 2014 00:00:00 | March, 05 2012 00:00:00 | April, 02 2010 00:00:00 | 99 | 1 |
| B1 | AM | T | April, 01 2014 00:00:00 | March, 02 2012 00:00:00 | April, 02 2010 00:00:00 | 99 | 1 |