SQL Max Value of Column to choose which row is selected

obizues

I'm having a problem with selecting a single row when there are multiple entries for one "callid"

In this case there are two rows being selected:

Assignee           maxTime
Jim Smith         11:31:05
James Smith       17:50:16

I want to only select a single row that has the greatest time.

Output I want:

 Assignee          maxTime
 James Smith      17:50:16

This is my code:

select Assignee, MAX(TimeResolv) as maxTime 
from heat8..asgnmnt 
where callid ='00539265' 
    and GroupName like '%cs%' 
Group by Assignee

Help would be appreciated.

sagi

You can use TOP :

SELECT TOP 1 * 
FROM heat8..asgnmntt t
ORDER BY t.timeResolv DESC

Or less efficient with NOT EXISTS():

SELECT * FROM heat8..asgnmntt t
WHERE NOT EXISTS(SELECT 1 FROM heat8..asgnmnt s
                 WHERE s.timeResolv > t.timeResolv)

Or with window function ROW_NUMBER() :

SELECT s.Assignee, s.TimeResolv
FROM (
    SELECT t.*,
           ROW_NUMBER() OVER(ORDER BY t.timeResolv) as rnk
    FROM heat8..asgnmntt t) s
WHERE s.rnk = 1

ROW_NUMBER() is also good to do it with one query for results per group.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

SQL select only rows with max value on a column

From Java

Fetch the row which has the Max value for a column

From Java

Python Pandas add column for row-wise max value of selected columns

From Dev

Get names of column with max value for each row

From Dev

Cgridview selected row column value in Yii

From Dev

Oracle: Is it possible to choose which table to join based on a column value?

From Dev

SQL max value in one column

From Dev

Select the row with the max value in a specific column, SQL Server

From Dev

SQL Select distinct rows with duplicate values in one column and choose one row for each duplicate based on value in primary key field

From Dev

Get MAX from row with column name (SQL)

From Dev

SQL: Increment row value when selected

From Dev

SQL - How to select a row having a column with max value (+ group by)

From Dev

SQL: Getting items which have a max value in another tables column, but also match some cross table clauses

From Dev

SQL and Jython - selecting MAX value in COLUMN

From Dev

Determine which source row is returned for SQL MAX() function

From Dev

SQL update column and use max value of a column for each row

From Dev

inner join to get row with max column value

From Dev

How to query data in Oracle SQL that gives the max value of a column and the corresponding columns for that row?

From Dev

Select the row with the max value in a specific column, SQL Server

From Dev

sql max partition by to get other column value

From Dev

Formula returning Column A value for row containing MAX value of a range

From Dev

update selected rows column value in datatable sql

From Dev

TSQL: Get a row which has a max value for a column

From Dev

Postgresql - Getting row with max value in column

From Dev

Select row with max value saving distinct column

From Dev

Using row value in dataframe to choose column in second dataframe

From Dev

Select row based on max value in column SQL Server

From Dev

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

From Dev

SQL find row value in other column of groups MAX() value - MySQL

Related Related

  1. 1

    SQL select only rows with max value on a column

  2. 2

    Fetch the row which has the Max value for a column

  3. 3

    Python Pandas add column for row-wise max value of selected columns

  4. 4

    Get names of column with max value for each row

  5. 5

    Cgridview selected row column value in Yii

  6. 6

    Oracle: Is it possible to choose which table to join based on a column value?

  7. 7

    SQL max value in one column

  8. 8

    Select the row with the max value in a specific column, SQL Server

  9. 9

    SQL Select distinct rows with duplicate values in one column and choose one row for each duplicate based on value in primary key field

  10. 10

    Get MAX from row with column name (SQL)

  11. 11

    SQL: Increment row value when selected

  12. 12

    SQL - How to select a row having a column with max value (+ group by)

  13. 13

    SQL: Getting items which have a max value in another tables column, but also match some cross table clauses

  14. 14

    SQL and Jython - selecting MAX value in COLUMN

  15. 15

    Determine which source row is returned for SQL MAX() function

  16. 16

    SQL update column and use max value of a column for each row

  17. 17

    inner join to get row with max column value

  18. 18

    How to query data in Oracle SQL that gives the max value of a column and the corresponding columns for that row?

  19. 19

    Select the row with the max value in a specific column, SQL Server

  20. 20

    sql max partition by to get other column value

  21. 21

    Formula returning Column A value for row containing MAX value of a range

  22. 22

    update selected rows column value in datatable sql

  23. 23

    TSQL: Get a row which has a max value for a column

  24. 24

    Postgresql - Getting row with max value in column

  25. 25

    Select row with max value saving distinct column

  26. 26

    Using row value in dataframe to choose column in second dataframe

  27. 27

    Select row based on max value in column SQL Server

  28. 28

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

  29. 29

    SQL find row value in other column of groups MAX() value - MySQL

HotTag

Archive