【问题标题】:Select all different records from one column that have the same value in other coumn从一列中选择在另一列中具有相同值的所有不同记录
【发布时间】:2016-09-21 08:22:09
【问题描述】:

我可以请一些好心人!

我在一个名为 x 的表中有三列:

ID | Barcode| Description  
1  | 554412 | Bar  
2  | 554412 | Stone  
3  | 554412 | Bar  

我需要选择所有条形码以及 ID,其中相同的条形码具有不同的描述,因此在这种情况下,它会显示 ID 1 和 2 以及 Bar 和 Stone 描述。

非常感谢您!

【问题讨论】:

  • 为什么 ID 是 1 和 2,而不是 2 和 3?
  • @SankarRaj 如果您使用Group by,您将无法获取ID
  • @JibinBalachandran 是的,你可以。你只需要在加入之外思考
  • 如果你想要不同描述的条形码,你不应该选择ID,因为它不相关。用于描述 Bar ID 可以是 1 和 3
  • 我基本上需要所有具有多个描述的条形码,但我还需要确定发生这种情况的 ID。

标签: sql sqlite visual-foxpro


【解决方案1】:

由于这同时被标记为 SQL 和 VFP,我将在这种情况下回答思考。这是您表的一些扩展示例数据:

Create Cursor myTable (Id I, Barcode c(10), Description c(10))

Insert Into myTable (Id, Barcode, Description) Values (1,'554412', 'Bar')
Insert Into myTable (Id, Barcode, Description) Values (2,'554412', 'Stone')
Insert Into myTable (Id, Barcode, Description) Values (3,'554412', 'Bar')

Insert Into myTable (Id, Barcode, Description) Values (4,'554413', 'Bar1')
Insert Into myTable (Id, Barcode, Description) Values (5,'554413', 'Bar2')
Insert Into myTable (Id, Barcode, Description) Values (6,'554413', 'Bar3')
Insert Into myTable (Id, Barcode, Description) Values (7,'554413', 'Bar3')
Insert Into myTable (Id, Barcode, Description) Values (8,'554413', 'Bar1')

使用 SQL 很容易得到你展示的内容:

Select * From myTable ;
    where Id In ;
    (Select Min(Id) From myTable Group By Barcode, Description)

为了 VFP,另一个不需要任何其他东西的简单技巧是创建唯一索引:

Index on Barcode + Description tag tmp unique
* Browse

【讨论】:

    【解决方案2】:
    Select x1.ID , x1.Barcode from x as x1 , x as x2 where 
    x1.Barcode = x2.Barcode and x2.Description != x1.Description
    

    这将在您的示例中返回第 1,2 和 3 行,因为它们都符合“相同条形码具有不同描述”的条件。

    将返回第 1 行和第 3 行,因为它们与第 2 行具有相同的条形码和不同的描述。

    【讨论】:

    • 好的,这听起来不错,但我认为我解释得不好。我基本上需要所有具有多个描述的条形码,但在结果中我需要查看所有三列以查看 ID 之间的“关系”是什么,如果这组 ID 具有统一的描述......或者会你知道另一种显示方式吗?
    【解决方案3】:

    请检查以下答案。

    CREATE TABLE test (
     id int DEFAULT NULL,
     Barcode  nvarchar(20) DEFAULT NULL,
     Description nvarchar(40) DEFAULT NULL
    ) --ENGINE=InnoDB DEFAULT CHARSET=latin1;
    
    -- ----------------------------
    -- Records of test
    -- ----------------------------
    INSERT INTO test VALUES ('1', '554412', 'Bar');
    INSERT INTO test VALUES ('2', '554412', 'Stone');
    INSERT INTO test VALUES ('3', '554412', 'Bar');
    
    
    --  If you want to show all ids then use the STUFF() code, so it will return 1,3 for Bar and 2 for Description
    SELECT Barcode, Description,
    (
    SELECT STUFF( (
    SELECT ', ' + convert(varchar(10),b.id )
    FROM test b
    WHERE b.barcode = t.Barcode AND b.Description= t.Description
    FOR XML PATH('') 
    ) , 1, 2, '')) AS Id
    FROM dbo.test t
    group by Barcode, Description
    
    
    -- If you just want to display  1 for Bar and 2 for Description then use below code
    SELECT Barcode, Description,
    (
    SELECT TOP 1 b.id 
    FROM test b
    WHERE b.barcode = t.Barcode AND b.Description= t.Description
    ) AS Id
    FROM dbo.test t
    group by Barcode, Description
    

    【讨论】:

      【解决方案4】:

      这显示了具有多个不同描述的条形码,以及相关的 ID

      select x.id, b.Barcode, b.Duplicates
      from
      (
      select Barcode, count(distinct Description) as Duplicates
      from x
      group by Barcode
      having count(distinct Description) >1
      ) b
      inner join x
      on x.Barcode = b.Barcode
      

      【讨论】:

      • 你好,这个看起来很酷,但是除了(或连同)计数之外,我还需要查看不同的描述本身。
      • 看来您应该努力修改他的代码以使其正常工作并发布更新的代码,而不是抱怨 JohnHC 没有为您做足够的工作。
      【解决方案5】:

      你能试试这个吗,希望它能满足你的要求:

      CREATE TABLE tmpTwo(ID INT, Barcode INT, DescriptionE VARCHAR(50))
      INSERT INTO tmpTwo VALUES 
      (1,554412, 'Bar'),
      (2,554412, 'Stone'),
      (3,554412, 'Bar'),
      (4,554413, 'b'),
      (5,554413, 's'),
      (6,554413, 'b'),
      (7,554414, 'z')
      
      SELECT s.id, s.DescriptionE
      FROM tmpTwo s
      INNER JOIN(SELECT MAX(u.id) it FROM tmpTwo u
             WHERE u.barcode IN (SELECT v.barcode FROM tmpTwo v 
                               GROUP BY v.barcode 
                               HAVING (COUNT(DISTINCT(v.descriptionE)))>1 )
      GROUP BY u.descriptionE) t ON s.id = t.it
      

      输出:

      id  DescriptionE
      2   Stone
      3   Bar
      5   s
      6   b
      

      仅供参考,如果您将 MAX 更改为 MIN,那么您将得到 id 为 1,2,4,5 的答案

      【讨论】:

        【解决方案6】:

        这个问题的答案可能很棘手,因为当您说 您需要选择所有条形码以及 ID,其中相同的条形码具有不同的描述, 然后结果集也可以是 id 2 和 3。所以我用最少的 id 来代表第一场比赛。见下文:

        with tabl (ID , Barcode, Description)
                  as ( select 1 , 554412 ,'Bar' from dual
                       UNION ALL
                        select 2 ,554412 ,'Stone' from dual
                       UNION ALL
                        select 3 ,554412 ,'Bar' from dual )
        select min(a.id) id,a.barcode,a.description 
        from tabl a
        inner join  tabl b
        on a.Barcode = b.Barcode
        group by a.barcode,a.description;
        

        【讨论】:

          【解决方案7】:

          你需要做一个自我加入

          http://www.mysqltutorial.org/mysql-self-join/

          第一个表将是 Select * from table

          第二张桌子会有所不同

          在 mysql 中我想出了....没有方便的 mssql 来尝试一下

          表 t1 记录数:3

          col1    col2    col3
          1   554412  Bar
          2   554412  Stone
          3   554412  Bar
          
          SELECT
          distinct
          pv1.col2,  pv1.col3
          
          from [t1] pv1
          join [t1] pv2 on pv1.col1=pv2.col1
          

          结果:

           Number of Records: 2
              col2    col3
              554412  Bar
              554412  Stone
          

          ack,没有 col1,抱歉,已经 18 小时了....尝试正确加入

          https://technet.microsoft.com/en-us/library/ms177490(v=sql.105).aspx

          【讨论】:

            猜你喜欢
            • 2017-02-14
            • 2019-05-31
            • 2012-05-25
            • 2022-01-01
            • 2015-02-15
            • 2017-12-24
            • 1970-01-01
            • 2013-11-10
            • 1970-01-01
            相关资源
            最近更新 更多