Filter out duplicate row based on values in 2 columns

Peter S

I have the following table:

CREATE TABLE [TABLE_1] 
(
    ID decimal(28,6) NULL,
    S_DATE datetime  NULL,
    NR decimal(28,6) NULL,
);

INSERT INTO TABLE_1 (ID, S_DATE, NR)
VALUES (1, '2020-01-01', 1),
       (1, '2020-01-01', 2),
       (1, '2020-05-02', 3);

A simple query:

SELECT *  
FROM Table_1 t1

Returns:

+----+----------------------+----+
| ID |        S_DATE        | NR |
+----+----------------------+----+
|  1 | 2020-01-01T00:00:00Z |  1 |
|  1 | 2020-01-01T00:00:00Z |  2 |
|  1 | 2020-05-02T00:00:00Z |  3 |
+----+----------------------+----+

But I want to filter out duplicates that share the same ID and S_DATE, like this:

+----+----------------------+----+
| ID |        S_DATE        | NR |
+----+----------------------+----+
|  1 | 2020-01-01T00:00:00Z |  1 |
|  1 | 2020-05-02T00:00:00Z |  3 |
+----+----------------------+----+

SQL FIDDLE: LINK

Aaron Bertrand

You can use a common table expression which applies a row number to each subsequent row with the same ID/S_DATE values, then filter on that row number.

;WITH src AS 
(
  SELECT ID, S_DATE, NR, rn = ROW_NUMBER() OVER 
     (PARTITION BY ID, S_DATE ORDER BY NR)
  FROM dbo.TABLE_1
)
SELECT ID, S_DATE, NR
  FROM src
  WHERE rn = 1;

You could also do this, which is more straightforward, but it is much more complicated to do when you have many columns to group by (or if you want more complicated logic for which NR to include):

SELECT ID, S_DATE, NR = MIN(NR)
  FROM dbo.TABLE_1
  GROUP BY ID, S_DATE;

Fiddle

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Drop duplicate rows in dataframe based on multplie columns with list values

分類Dev

MySQL Query for selecting values based on 2 columns

分類Dev

Division of 2 row values based on group and condition

分類Dev

group by columns then filter based on a condition

分類Dev

dynamic Columns based on filter in angularjs

分類Dev

Filter Array of object based in duplicate keys

分類Dev

Convert SQL Row values into Columns

分類Dev

Delete duplicate records based on multiple columns

分類Dev

Removing duplicate values row-wise in R

分類Dev

Remove rows based on columns values

分類Dev

Marking Duplicate Values based on additional cell.

分類Dev

Filter out segments of decreasing values (with extra conditions)

分類Dev

Best way to lookup and return data for conditions based on 2 columns and 1 row

分類Dev

How to filter values of 2 columns in a dataframe from a second dataframe using pandas

分類Dev

In R: ordering values from 2 DF columns for use in ratio for each row

分類Dev

Script to hide columns based on the first row value

分類Dev

Turning row-based data into columns by header

分類Dev

pandas dataframe column based on row and multiple columns

分類Dev

Group rows by two columns and filter values by comparison

分類Dev

custom function to filter values in pandas dataframe columns

分類Dev

R - Identical values in columns of dataframe in one row

分類Dev

How to filter the first and the last row based on a condition in the last row in R

分類Dev

Extract values based on pattern out of a line/list

分類Dev

R data table unique record count based on all combination of a given list of values from 2 columns

分類Dev

Add a filter based on other column values in kdb

分類Dev

Django filter objects based on list values

分類Dev

Filter the pyspark dataframe based on values in list

分類Dev

LINQ to remove duplicate rows from a datatable based on the value of a specific row

分類Dev

Python: Filtering numpy values based on certain columns

Related 関連記事

  1. 1

    Drop duplicate rows in dataframe based on multplie columns with list values

  2. 2

    MySQL Query for selecting values based on 2 columns

  3. 3

    Division of 2 row values based on group and condition

  4. 4

    group by columns then filter based on a condition

  5. 5

    dynamic Columns based on filter in angularjs

  6. 6

    Filter Array of object based in duplicate keys

  7. 7

    Convert SQL Row values into Columns

  8. 8

    Delete duplicate records based on multiple columns

  9. 9

    Removing duplicate values row-wise in R

  10. 10

    Remove rows based on columns values

  11. 11

    Marking Duplicate Values based on additional cell.

  12. 12

    Filter out segments of decreasing values (with extra conditions)

  13. 13

    Best way to lookup and return data for conditions based on 2 columns and 1 row

  14. 14

    How to filter values of 2 columns in a dataframe from a second dataframe using pandas

  15. 15

    In R: ordering values from 2 DF columns for use in ratio for each row

  16. 16

    Script to hide columns based on the first row value

  17. 17

    Turning row-based data into columns by header

  18. 18

    pandas dataframe column based on row and multiple columns

  19. 19

    Group rows by two columns and filter values by comparison

  20. 20

    custom function to filter values in pandas dataframe columns

  21. 21

    R - Identical values in columns of dataframe in one row

  22. 22

    How to filter the first and the last row based on a condition in the last row in R

  23. 23

    Extract values based on pattern out of a line/list

  24. 24

    R data table unique record count based on all combination of a given list of values from 2 columns

  25. 25

    Add a filter based on other column values in kdb

  26. 26

    Django filter objects based on list values

  27. 27

    Filter the pyspark dataframe based on values in list

  28. 28

    LINQ to remove duplicate rows from a datatable based on the value of a specific row

  29. 29

    Python: Filtering numpy values based on certain columns

ホットタグ

アーカイブ