SQL select unique name based on max index

Robin Fay

I have the following table

-| id    | name        | index  | number| answered| time              | 
-|-------|-------------|--------|-------|---------|-------------------|
-| 043b  | callline2   | 1      |       | TRUE    |2020-05-26 11:07:25|
-| 043b  | Holdline1   | 2      |       | TRUE    |2020-05-26 11:07:25|
-| 043b  | Benny Russ  | 3      | 505   | TRUE    |2020-05-26 12:17:25|
-| 041b  | callline2   | 1      |       | FALSE   |2020-05-26 10:17:25|
-| 041b  | callline2   | 2      |       | FALSE   |2020-05-26 10:17:25|
-| 033b  | callline2   | 1      |       | FALSE   |2020-05-26 10:17:25|
-| 033b  | callline2   | 2      |       | FALSE   |2020-05-26 10:17:25|

I want to return

-| id    | name        | index  | number| answered| End   Time        |  Start   Time     |
-|-------|-------------|--------|-------|---------|-------------------|-------------------|
-| 043b  | Benny Russ  | 3      | 505   | TRUE    |2020-05-26 12:17:25|2020-05-26 11:07:25|
-| 033b  | callline2   | 2      |       | FALSE   |2020-05-26 10:17:25|2020-05-26 10:17:25|
-| 041b  | callline2   | 2      |       | FALSE   |2020-05-26 10:17:25|2020-05-26 10:17:25|

what I have come up with so far is, the following. but I can't figure out how to pull the name from the table when at the max index and where the number is not null?

SELECT distinct a.id , a.name, max.index_max, a.number, a.answered
    , max.end_time, min.start_time
FROM example_table a
    inner join (select Max(time) as end_time, max(index) as index_max, uuid 
                from example_table group by id) max on a.id = max.id 
    inner join (select min(time) as start_time, min(index) as index_min, uuid 
                from example_table group by id) min on a.id = min.id
Mikhail Berlyant

Below is for BigQuery Standard SQL

#standardSQL 
SELECT max_record.* EXCEPT(time), 
  max_record.time AS end_time, 
  start_time
FROM (
  SELECT ARRAY_AGG(t ORDER BY index DESC LIMIT 1)[OFFSET(0)] AS max_record, MIN(time) AS start_time
  FROM `project.dataset.table` t
  GROUP BY id 
)    

if to apply to sample data from your question - output is

enter image description here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

sql select min or max based on condition

From Dev

SQL Query to select the name of the branch with max number of employees working

From Dev

sql select min or max based on condition part 2

From Dev

sql select min or max based on condition part 2

From Dev

T-SQL: How to select rows based on the max date?

From Dev

Select row based on max value in column SQL Server

From Dev

Select unique barcode with max timestamp

From Dev

Select unique barcode with max timestamp

From Dev

mysql - Select unique column based on max value of another column in a different table

From Dev

Select max value of a row and column name of the max value as two columns in SQL Server

From Dev

Unique vs MAX in SQL statement

From Dev

SQL select MAX(COUNT)

From Dev

SQL select max value

From Dev

sql 'select max' query

From Dev

Select max date rows for unique key

From Dev

mysql select max value of unique items

From Dev

SQL Server Conditional Unique Index

From Dev

sql query a varchar(max) column to select an element based on the value of one of its children children's

From Dev

SQL: How to select a column-value based on an aggregate min/max value in a window frame (including preceding rows)

From Dev

sql query a varchar(max) column to select an element based on the value of one of its children children's

From Dev

SQL Select Based On Max Value of One Column and Specific Id from Another

From Dev

select image from name and index

From Dev

SQL Grouping based on Name

From Dev

SQL select MAX value or NULL

From Dev

SQL SELECT rows with max date

From Dev

SQL SERVER Select MAX Procedure

From Dev

SQL Server: INSERT INTO SELECT MAX

From Dev

select id by the min and max (sql)

From Dev

change the select box based on index

Related Related

  1. 1

    sql select min or max based on condition

  2. 2

    SQL Query to select the name of the branch with max number of employees working

  3. 3

    sql select min or max based on condition part 2

  4. 4

    sql select min or max based on condition part 2

  5. 5

    T-SQL: How to select rows based on the max date?

  6. 6

    Select row based on max value in column SQL Server

  7. 7

    Select unique barcode with max timestamp

  8. 8

    Select unique barcode with max timestamp

  9. 9

    mysql - Select unique column based on max value of another column in a different table

  10. 10

    Select max value of a row and column name of the max value as two columns in SQL Server

  11. 11

    Unique vs MAX in SQL statement

  12. 12

    SQL select MAX(COUNT)

  13. 13

    SQL select max value

  14. 14

    sql 'select max' query

  15. 15

    Select max date rows for unique key

  16. 16

    mysql select max value of unique items

  17. 17

    SQL Server Conditional Unique Index

  18. 18

    sql query a varchar(max) column to select an element based on the value of one of its children children's

  19. 19

    SQL: How to select a column-value based on an aggregate min/max value in a window frame (including preceding rows)

  20. 20

    sql query a varchar(max) column to select an element based on the value of one of its children children's

  21. 21

    SQL Select Based On Max Value of One Column and Specific Id from Another

  22. 22

    select image from name and index

  23. 23

    SQL Grouping based on Name

  24. 24

    SQL select MAX value or NULL

  25. 25

    SQL SELECT rows with max date

  26. 26

    SQL SERVER Select MAX Procedure

  27. 27

    SQL Server: INSERT INTO SELECT MAX

  28. 28

    select id by the min and max (sql)

  29. 29

    change the select box based on index

HotTag

Archive