我有這樣的數據
和類別重複的是
如果存在重複1.Duplicate基礎代碼,結果是最後一步
2.Duplicate基地NUMBER1或者如果存在重複的基礎數字2,結果是最後一步
如何獲取像這樣的重複數據?
我需要像導致
感謝,
我有這樣的數據
和類別重複的是
如果存在重複1.Duplicate基礎代碼,結果是最後一步
2.Duplicate基地NUMBER1或者如果存在重複的基礎數字2,結果是最後一步
如何獲取像這樣的重複數據?
我需要像導致
感謝,
如果我明白你的要求,我相信這會達到你想要的。
它至少會產生指定的結果。
declare @Data table
(
ID int not null,
CODE varchar(3) not null,
NUMBER1 varchar(3) not null,
NUMBER2 varchar(3) not null,
STEP int not null
)
insert into @Data values
(1, 'XXA', '001', '009', 1),
(2, 'XXB', '001', '008', 2),
(3, 'XXC', '002', '009', 3),
(4, 'XXA', '002', '008', 4)
-- this query returns the record with the highest STEP
-- excluding any other records that have the same CODE,
-- NUMBER1, or NUMBER2 and a lower STEP
select
d.ID,
d.CODE,
d.NUMBER1,
d.NUMBER2,
d.STEP
from @Data d
where
-- there does not exist any other record that has
-- the same CODE, NUMBER1, or NUMBER2 and a higher STEP
not exists (
select 1
from @Data duplicate
where
(
duplicate.CODE = d.CODE
or duplicate.NUMBER1 = d.NUMBER1
or duplicate.NUMBER2 = d.NUMBER2
)
and (
duplicate.STEP > d.STEP
or (
duplicate.STEP = d.STEP
and duplicate.ID > d.ID
)
)
)
-- this query returns the duplicate records with a lower STEP
select
d.ID,
d.CODE,
d.NUMBER1,
d.NUMBER2,
d.STEP,
HigherStep.ID as HigherStepID
from @Data d
inner join (
-- join to the duplicate record that has a higher STEP
select
ID,
CODE,
NUMBER1,
NUMBER2
from @Data HighestStep
where
-- there does not exist any other record that has
-- the same CODE, NUMBER1, or NUMBER2 and a higher STEP
not exists (
select 1
from @Data duplicate
where
(
duplicate.CODE = HighestStep.CODE
or duplicate.NUMBER1 = HighestStep.NUMBER1
or duplicate.NUMBER2 = HighestStep.NUMBER2
)
and (
duplicate.STEP > HighestStep.STEP
or (
duplicate.STEP = HighestStep.STEP
and duplicate.ID > HighestStep.ID
)
)
)
) HigherStep on
HigherStep.ID <> d.ID -- don't match this record to itself
and (
HigherStep.CODE = d.CODE
or HigherStep.NUMBER1 = d.NUMBER1
or HigherStep.NUMBER2 = d.NUMBER2
)
下面是一個下臺階的基礎上,玉山的評論檢索記錄修訂查詢。該查詢的一個問題是,它將無法捕獲那些記錄不是以最高的步驟立即重複記錄。
-- this query returns the duplicate records with a lower STEP
select
d.ID,
d.CODE,
d.NUMBER1,
d.NUMBER2,
d.STEP,
HigherStep.ID as HigherStepID
from @Data d
inner join (
-- join to the duplicate record that has a higher STEP
select
ID,
CODE,
NUMBER1,
NUMBER2,
STEP
from @Data
) HigherStep on
(
HigherStep.CODE = d.CODE
or HigherStep.NUMBER1 = d.NUMBER1
or HigherStep.NUMBER2 = d.NUMBER2
)
and (
HigherStep.STEP > d.STEP
or (
HigherStep.STEP = d.STEP
and HigherStep.ID > d.ID
)
)
如何獲得參考與id重複所以結果additonal值1,2,3,坦克之前醫生。 – 2012-01-13 20:38:52
@Yusan - 我不確定我是否理解,但我已更新我的答案,以包含第二個查詢,該查詢返回第一個查詢排除的重複記錄。唯一的區別是我從where子句中刪除了'not'。 – 2012-01-13 20:45:58
@Yusan - 我的歉意,我把我以前的編輯搞砸了。我還修改了第二個查詢,以便除了返回第一個查詢排除的記錄外,它還將這些記錄加入到具有更高步驟的重複記錄中。該記錄的ID作爲「HigherStepID」列提供。 – 2012-01-13 20:58:02
我希望我的理解正確:
DECLARE @T1 TABLE
(
ID INT,
CODE VARCHAR(50),
NUMBER1 VARCHAR(50),
NUMBER2 VARCHAR(50),
STEP INT
)
INSERT INTO @T1(ID, CODE, NUMBER1, NUMBER2, STEP)
VALUES(1, 'XXA', '001', '009', 1)
INSERT INTO @T1(ID, CODE, NUMBER1, NUMBER2, STEP)
VALUES(2, 'XXB', '001', '008', 2)
INSERT INTO @T1(ID, CODE, NUMBER1, NUMBER2, STEP)
VALUES(3, 'XXC', '002', '009', 3)
INSERT INTO @T1(ID, CODE, NUMBER1, NUMBER2, STEP)
VALUES(4, 'XXA', '002', '008', 4)
SELECT TAB.ID,
TAB.CODE,
TAB.NUMBER1,
TAB.NUMBER2,
TAB.STEP,
COALESCE(CAST(DUP_CODE_T.ID AS VARCHAR(50)) + ',', '') +
COALESCE(CAST(DUP_NUM1_T.ID AS VARCHAR(50)) + ',', '') +
COALESCE(CAST(DUP_NUM2_T.ID AS VARCHAR(50)), '') AS DUPLICATE_WITH
FROM @T1 AS TAB OUTER APPLY
(
SELECT TOP 1 DUP_CODE.ID
FROM @T1 AS DUP_CODE
WHERE DUP_CODE.CODE = TAB.CODE AND
DUP_CODE.ID <> TAB.ID
) AS DUP_CODE_T OUTER APPLY
(
SELECT TOP 1 DUP_NUM1.ID
FROM @T1 AS DUP_NUM1
WHERE DUP_NUM1.NUMBER1 = TAB.NUMBER1 AND
DUP_NUM1.ID <> TAB.ID
) AS DUP_NUM1_T OUTER APPLY
(
SELECT TOP 1 DUP_NUM2.ID
FROM @T1 AS DUP_NUM2
WHERE DUP_NUM2.NUMBER2 = TAB.NUMBER2 AND
DUP_NUM2.ID <> TAB.ID
) AS DUP_NUM2_T
您可以添加WHERE子句或TOP 1篩選結果。
好..但是如何僅僅由高一步造成只有一排。坦克這麼多pistipanko。 – 2012-01-13 19:51:28
添加TOP 1和ORDER BY TAB.ID DESC。 – pistipanko 2012-01-13 19:52:54
我的意思是在數據現實中,我有其他數據,這是獨一無二的,所以結果不只是1頂1,也許其他行沒有重複坦克。 – 2012-01-13 20:28:34
在查詢中要求的所需行爲需要澄清。 – 2012-01-13 19:54:15