Filter duplicate rows based on a field

后端 未结 4 818
余生分开走
余生分开走 2021-01-01 23:39

I have several records (bills) that are basically duplicates of each other, except for one field, which represents the language that bill is in.

For example:

<
相关标签:
4条回答
  • 2021-01-02 00:13

    Probably the easiest way would be to use ROW_NUMBER and PARTITION BY

    SELECT * FROM (
       SELECT b.*,
       ROW_NUMBER() OVER (PARTITION BY BillID ORDER BY Lang) as num
       FROM Bills b
       WHERE Account = 'abcd'
    ) tbl
    WHERE num = 1
    
    0 讨论(0)
  • 2021-01-02 00:23

    Use SELECT DISTINCT keyword to get different values SELECT DISTINCT ID,BillID,Account,Name,Amount,Lang FROM Bills; You will get different values.

    0 讨论(0)
  • 2021-01-02 00:24
     select
     BillID,Account,Name,Amount,max(Lang)
     FROM Bills 
     WHERE Account='abcd'
     group by BillID,Account,Name,Amount;
    

    Same as user2407394 except without the ID in the groupby, since that would return 3 too.

    0 讨论(0)
  • 2021-01-02 00:30
    select
    ID,BillID,Account,Name,Amount,max(Lang)
    FROM Bills 
    WHERE Account='abcd'
    group by BillID,Account,Name,Amount;
    

    Given that you are not giving priority to any specific language if there is same bill in multiple languages. The above query will work perfect.

    EDIT : Removed "ID" from group by. @Phil You are right..!!

    0 讨论(0)
提交回复
热议问题