How do I aggregate non-aggregate values in GROUP BY queries?

zuzak

I have a table full of duplicates. I'm trying to convert them so I can put a unique constraint across two fields (say, identifier1 and identifier2).

I would like to "collapse" those duplicates into single records, but of my records contain differing strings. I'd like to keep the last-touched in these circumstances (keeping the one from the highest ID and discarding the rest).

For example, I can aggregate the startDate below with MIN() -- but how do I only get the most recent location?

    id | identifier1 | identifier2 | location   | startDate
    1  | alice       | 0001        | ambridge   | 2016-01-01
    2  | bob         | 1312        | brigadoon  | 2017-01-01
    3  | alice       | 0001        | brigadoon  | 2017-05-01
    4  | bob         | 9999        | brigadoon  | 2015-01-01
    5  | celeste     | 1234        | cittegazze | 2011-01-01

    id | identifier1 | identifier2 | location   | startDate
    6  | alice       | 0001        | brigadoon  | 2016-01-01
    7  | bob         | 1312        | brigadoon  | 2017-01-01
    8  | bob         | 9999        | brigadoon  | 2015-01-01
    9  | celeste     | 1234        | cittegazze | 2011-01-01
Michał Turczyn

Try this:

select A.identifier1, A.identifier2, A.startDate, B.Location from (
    select identifier1,
           identifier2,
           MIN(startDate) AS startDate
    from TABLE_NAME
    group by identifier1, identifier2
) AS A JOIN TABLE_NAME AS B
ON (A.identifier1 = B.identifier1 and A.identifier2 = B.identifier2 and A.startDate = B.startDate)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I do this SQL query (aggregate group by and where in clause)?

From Dev

How do I aggregate by group and add the column to the dataframe?

From Dev

How do I aggregate text values into a single record

From Dev

How do I aggregate values respect to some conceptual classes with SQL?

From Dev

How to group and aggregate in pandas

From Dev

How to group and aggregate in pandas

From Dev

Subtract group aggregate values in MongoDB

From Dev

Aggregate within a group of unchanged values

From Dev

Elasticsearch group and aggregate nested values

From Dev

Subtract group aggregate values in MongoDB

From Dev

How can I extract results of aggregate queries in slick?

From Dev

How can I extract results of aggregate queries in slick?

From Dev

How do I perform an "OR" filter on an aggregate?

From Dev

Aggregate queries in Laravel Query Builder with group by

From Dev

Aggregate queries in Laravel Query Builder with group by

From Dev

How do we select non-aggregate columns in a query with a GROUP BY clause, which is not functionally dependent on columns in GROUP BY clause?

From Dev

How to display only non-null values using an aggregate query?

From Dev

Replace values of a group with group aggregate in numpy/pandas

From Dev

How do you select multiple aggregates and a non aggregate in LINQ

From Dev

How to do group in aggregate but also show other fields using Mongo?

From Dev

How do we group using aggregate for different match cases?

From Dev

An SQL query that combines aggregate and non-aggregate values in one row

From Dev

An SQL query that combines aggregate and non-aggregate values in one row

From Dev

How to aggregate with group by and sort correctly

From Dev

How to specify GROUP BY for PIVOT aggregate

From Dev

How to aggregate with group by and sort correctly

From Dev

How to use aggregate for group by in mongodb

From Dev

How can I aggregate collection and group by field count

From Dev

Non aggregate columns, SUM, MAX and GROUP BY

Related Related

  1. 1

    How can I do this SQL query (aggregate group by and where in clause)?

  2. 2

    How do I aggregate by group and add the column to the dataframe?

  3. 3

    How do I aggregate text values into a single record

  4. 4

    How do I aggregate values respect to some conceptual classes with SQL?

  5. 5

    How to group and aggregate in pandas

  6. 6

    How to group and aggregate in pandas

  7. 7

    Subtract group aggregate values in MongoDB

  8. 8

    Aggregate within a group of unchanged values

  9. 9

    Elasticsearch group and aggregate nested values

  10. 10

    Subtract group aggregate values in MongoDB

  11. 11

    How can I extract results of aggregate queries in slick?

  12. 12

    How can I extract results of aggregate queries in slick?

  13. 13

    How do I perform an "OR" filter on an aggregate?

  14. 14

    Aggregate queries in Laravel Query Builder with group by

  15. 15

    Aggregate queries in Laravel Query Builder with group by

  16. 16

    How do we select non-aggregate columns in a query with a GROUP BY clause, which is not functionally dependent on columns in GROUP BY clause?

  17. 17

    How to display only non-null values using an aggregate query?

  18. 18

    Replace values of a group with group aggregate in numpy/pandas

  19. 19

    How do you select multiple aggregates and a non aggregate in LINQ

  20. 20

    How to do group in aggregate but also show other fields using Mongo?

  21. 21

    How do we group using aggregate for different match cases?

  22. 22

    An SQL query that combines aggregate and non-aggregate values in one row

  23. 23

    An SQL query that combines aggregate and non-aggregate values in one row

  24. 24

    How to aggregate with group by and sort correctly

  25. 25

    How to specify GROUP BY for PIVOT aggregate

  26. 26

    How to aggregate with group by and sort correctly

  27. 27

    How to use aggregate for group by in mongodb

  28. 28

    How can I aggregate collection and group by field count

  29. 29

    Non aggregate columns, SUM, MAX and GROUP BY

HotTag

Archive