【问题标题】:Display only records which are not duplicated仅显示不重复的记录
【发布时间】:2020-04-03 01:25:27
【问题描述】:

我希望只显示没有重复的记录。

CREATE TABLE #TempTbl1 
(
    Circuit VARCHAR(55),
    Bandwidth DECIMAL(7,1),
    Unit BIT,
    CurrentID INT,
);

INSERT INTO #TempTbl1
VALUES
    ('28.L9XX.100277..COXC.', 5.0, 0, 3364),
    ('28/LEHZ/010421/121/CFOK', 100.0, 0, 3223),
    ('#2009-191:604', 10.0, 0, 3100),
    ('28.L9XX.100277..COXC.', 100.0, 0, 3364),
    ('#2009-191:604', 150.0, 0, 3100),
    ('00/GIGE/M3283961/395/303/395', 200.0, 1, 4500)

我有以下结果

 Circuit                       Bandwidth    Unit    CurrentID
 ------------------------------------------------------------
 28.L9XX.100277..COXC.          5.0         0       3364
 28/LEHZ/010421/121/CFOK        100.0       0       3223
 #2009-191:604                  10.0        0       3100
 28.L9XX.100277..COXC.          100.0       0       3364
 #2009-191:604                  150.0       0       3100
 00/GIGE/M3283961/395/303/395   200.0       1       4500

我希望我的结果如下所示。如您所见,我只想显示仅出现一次的电路。

 Circuit                       Bandwidth    Unit    CurrentID
 --------------------------------------------------------------
 28/LEHZ/010421/121/CFOK        100.0       0       3223
 00/GIGE/M3283961/395/303/395   200.0       1       4500

【问题讨论】:

  • #2009-191:604 发生了什么事?还有28.L9XX.100277..COXC.?
  • 请说明你要实现的逻辑。
  • 您的预期结果显示 2 个电路,您的数据显示 4 个独特电路。你能澄清一下吗?
  • @DaleK #2009-191:604?并且 28.L9XX.100277..COXC 以与原始表不同的带宽出现两次。我想查询这些结果,但只显示 28/LEHZ/010421/121/CFOK 和 00/GIGE/M3283961/395/303/395 因为它只显示一次。这有帮助吗?你们回答的很快。不幸的是,我在这里显示的编码示例仍然需要工作。我为我的困惑道歉。
  • 大多数人阅读“不显示重复项”,因为当有重复项时只返回 1 行而不是多行。在您的情况下,当有重复时,您似乎不需要该电路的任何行。

标签: sql sql-server tsql sql-server-2012 duplicates


【解决方案1】:

如果您只想要独特的电路,那么group by circuit 并在HAVING 子句中设置条件:

select circuit, 
  max(bandwidth) bandwidth, 
  max(cast(unit as integer)) unit, 
  max(cast(currentid as integer)) currentid
from #TempTbl1
group by circuit
having count(*) = 1

选择列表中带有MAX() 的聚合将返回列值,因为每个电路只有一个值。
请参阅demo
结果:

> circuit                      | bandwidth | unit | currentid
> :--------------------------- | :-------- | ---: | --------:
> 00/GIGE/M3283961/395/303/395 | 200.0     |    1 |      4500
> 28/LEHZ/010421/121/CFOK      | 100.0     |    0 |      3223

【讨论】:

    【解决方案2】:

    试试这个:

    SELECT *
    FROM yourtable t1
    WHERE 
       (SELECT count(1)
        FROM yourtable t2
        WHERE t2.circuit = t1.circuit
        AND t2.currentID = t1.currentID) = 1
    

    【讨论】:

      【解决方案3】:

      对于每个 CurrentID 和电路的唯一性?

      那么 COUNT OVER 它们应该是 1。

      ;WITH CTE_CIRCUITS AS
      (
          SELECT *
          , COUNT(*) OVER (PARTITION BY CurrentID, Circuit) AS Cnt
          FROM #TempTbl1
      )
      SELECT Circuit, Bandwidth, Unit, CurrentID
      FROM CTE_CIRCUITS
      WHERE Cnt = 1
      

      【讨论】:

        【解决方案4】:

        如果我们使用currentid 作为唯一字段,您可以使用以下代码。

        WITH CTE
         AS (SELECT circuit, 
                    FIRST_VALUE(bandwidth) OVER(PARTITION BY currentid
                    ORDER BY currentid) bandwidth, 
                    FIRST_VALUE(unit) OVER(PARTITION BY currentid
                    ORDER BY currentid) unit, 
                    currentid, 
                    COUNT(*) OVER(PARTITION BY currentid
                    ORDER BY currentid) CNT
             FROM #TempTbl1)
         SELECT circuit, 
                bandwidth, 
                unit, 
                currentid
         FROM CTE
         WHERE CNT = 1;
        

        或者:

        SELECT circuit, 
               bandwidth, 
               unit, 
               currentid
        FROM #TempTbl1
        WHERE currentid NOT IN
        (
            SELECT currentid
            FROM #TempTbl1
            GROUP BY currentid
            HAVING COUNT(currentid) > 1
        );
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-04-27
          • 2023-03-26
          • 2022-01-11
          • 2020-07-14
          • 1970-01-01
          • 2012-04-17
          • 2018-12-27
          • 1970-01-01
          相关资源
          最近更新 更多