3

嗨,我想知道这在 T-SQL 中是否可行。

如果存在第 2 行和第 3 行/第 5 行和第 6 行的组合,则想法是“不”返回第 1 行和第 4 行之类的行。

declare @table table
(
 A int,
 B char(2),
 C char(2),
 D char(2)
)

insert into @table
select 2, 'A1', 'B1', NULL  -- row 1
union
select 2, 'A1', 'B1', 'C1'  -- row 2 and row 3 is a combination (values are equal) 
union                       
select 3, 'A1', 'B1', 'C1'   
union
select 2, 'A2', 'B2', NULL  -- row 4
union
select 2, 'A2', 'B2', 'C2'  -- row 5 and row 6 is a combination (values are equal) 
union                     
select 3, 'A2', 'B2', 'C2'   

有谁想试试这个?谢谢你最好的问候史蒂夫

4

4 回答 4

2

像这样的东西?

declare @table table
(
 ID INT ,
 A int,
 B char(2),
 C char(2),
 D char(2)
)

insert into @table
select 1, 2, 'A1', 'B1', NULL  -- row 1
union
select 2, 2, 'A1', 'B1', 'C1'  -- row 2 and row 3 is a combination (values are equal) 
union                       
select 3, 3, 'A1', 'B1', 'C1'   
union
select 4, 2, 'A2', 'B2', NULL  -- row 4
union
select 5, 2, 'A2', 'B2', 'C2'  -- row 5 and row 6 is a combination (values are equal) 
union                     
select 6, 3, 'A2', 'B2', 'C2'

SELECT *
FROM @table t
WHERE EXISTS(SELECT * FROM @table t1 WHERE t.id IN (t1.id + 1, t1.id + 2) 
      AND t.d IS NULL ) 
于 2013-07-04T09:51:52.427 回答
2

NOT EXISTS是要走的路:

SELECT A,B,C,D from @table t1
WHERE NOT EXISTS(
    SELECT 1 FROM @table t2
    WHERE t1.A < t2.A 
    AND COALESCE(t1.B,'')=COALESCE(t2.B,'') 
    AND COALESCE(t1.C,'')=COALESCE(t2.C,'') 
    AND COALESCE(t1.D,'')=COALESCE(t2.D,'') 
)

演示

-trickCOALESCE是比较两个空值。否则null=null将返回null并包含具有空值的相等行。

于 2013-07-04T09:52:10.337 回答
2

尝试这个

SELECT DISTINCT A,B,C,D FROM @table 
WHERE 
A IS NOT NULL AND 
B IS NOT NULL AND 
C IS NOT NULL AND 
D IS NOT NULL

或者

SELECT DISTINCT A,B,C,D FROM @table 
WHERE 
D IS NOT NULL
于 2013-07-04T09:42:20.320 回答
1

我认为我们可以通过使用表的聚合和排序分别过滤记录来实现这一点。我相信,你想消除那些后面有组合的行......如果是这样,下面可能会起作用......

declare @table table
(
 A int,
 B char(2),
 C char(2),
 D char(2)
)

insert into @table
select 2, 'A1', 'B1', NULL  -- row 1
union
select 2, 'A1', 'B1', 'C1'  -- row 2 and row 3 is a combination (values are equal) 
union                       
select 3, 'A1', 'B1', 'C1'   
union
select 2, 'A2', 'B2', NULL  -- row 4
union
select 2, 'A2', 'B2', 'C2'  -- row 5 and row 6 is a combination (values are equal) 
union                     
select 3, 'A2', 'B2', 'C2'   


SELECT 
T.* 
FROM 
@table T
INNER JOIN 
(
    SELECT 
    T1.* 
    FROM
    (
        SELECT 
        ROW_NUMBER() OVER(ORDER BY B,C,D) As RowNumber, 
        MIN(A) AS A,
        B,
        C,
        D,
        COUNT(1) Cnt 
        FROM @TABLE
        GROUP BY B,C,D
    ) T1
    LEFT JOIN
    (
        SELECT 
        ROW_NUMBER() OVER(ORDER BY B,C,D) As RowNumber, 
        MIN(A) AS A,
        B,
        C,
        D,
        COUNT(1) Cnt 
        FROM @TABLE
        GROUP BY B,C,D
    ) T2 ON T2.RowNumber = T1.RowNumber + 1
    WHERE ISNULL(T2.Cnt,0) <= 1
) NonPostDuplicateRows
ON T.B = NonPostDuplicateRows.B
AND T.C = NonPostDuplicateRows.C
AND T.D = NonPostDuplicateRows.D
于 2013-07-04T10:39:08.063 回答