how to find out max of a field for two different conditions in sql

Anita Prasad

I have a table EMPDATA with the following data:

EntityId    MeetDate    SourceCode  Status
1           06.11.2017   AB          FNL
1           05.2.2018    AB          NO
1           09.3.2018    AB          FNL
3           07.12.2016   AB          FNL
3           09.2.2015    AB          FNL
3           07.8.2014    IU          FNL
3           08.7.2017    IU          FNL

Conditions :

1) MeetDate Column will be split in two columns in the output

1a) LastDate: This will be the latest Meetdate for SourceCode ‘AB’ and status ‘FNL’.

1b) InterimDate: This will be the latest Meetdate for SourceCode ‘IU’ which has occurred after the latest Meetdate for SourceCode ‘AB’ and Status ‘FNL’.

For records with no IU sourcecode interimdate will be null.

That is: Output will be as follows:

EntityId    LastDate    InterimDate 
1           09.3.2018      NULL          
3           07.12.2016    08.7.2017  
Giorgos Betsos

You can easily achieve the expected output using conditional aggregation:

SELECT EntityId,
       MAX(CASE 
              WHEN SourceCode = 'AB' AND status = 'FNL' THEN MeetDate 
           END) AS LastDate,
       MAX(CASE WHEN SourceCode = 'IU' THEN MeetDate END) AS InterimDate          
FROM mytable
GROUP BY EntityId

This query implements all logic described in the OP except for:

  • InterimDate ... which has occurred after the latest Meetdate for SourceCode ‘AB’ and Status ‘FNL’.

You can implement this using a CTE so that the code looks cleaner:

;WITH CTE AS (
SELECT EntityId,
       MAX(CASE 
              WHEN SourceCode = 'AB' AND status = 'FNL' THEN MeetDate 
           END) AS LastDate,
       MAX(CASE WHEN SourceCode = 'IU' THEN MeetDate END) AS InterimDate          
FROM mytable
GROUP BY EntityId
)
SELECT LastDate, 
       CASE 
          WHEN InterimDate > LastDate THEN InterimDate
       END AS InterimDate
FROM CTE 

Demo 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

How to find MIN and MAX of two columns from TWO different tables?

From Dev

How to Find out two Highest values from different Columns?

From Dev

How to find out different cells from two excelsheets with the same format

From Dev

How to find out max or latest date? How to create Calculate fields? How to delete a field? - Rethinkdb

From Dev

How to find MAX value from two tables in SQL Server?

From Dev

SQL: find duplicates, with a different field

From Dev

Find string in two field with SQL

From Dev

How to find average of two different sql query using php

From Dev

How to find out the max value for Int in Swift

From Dev

How to convert data from two different conditions?

From Dev

how to sort data based on two different (text and number) conditions in sql server?

From Dev

How can I Update two fields in same table with different conditions (SQL)?

From Dev

Find out max & min of two number without using If else?

From Dev

How to get data for different conditions in SQL Server?

From Dev

SQL return 2 different dates from one field using Max

From Dev

How to find out where different packages are installed?

From Dev

Under what conditions do these two SQL queries give different results?

From Dev

Perform two counts (with different conditions) within the same SQL request

From Dev

Under what conditions do these two SQL queries give different results?

From Dev

sql query / getting latest entry based on two different conditions

From Dev

Find tax bracket using SQL, without a min and max field

From Dev

How to query two fields (different) into one field

From Dev

How to query two fields (different) into one field

From Dev

How to find out if two numbers are relatively prime?

From Dev

How to find out if two Strings are the same in TypeScript?

From Dev

How merge two select with different WHERE and special conditions

From Dev

How to divide the sum of a column under two different conditions?

From Dev

MySQL - How to find specific field with comparing two field in select query

From Dev

How to find the difference between two date cells of different rows in sql server 2012?

Related Related

  1. 1

    How to find MIN and MAX of two columns from TWO different tables?

  2. 2

    How to Find out two Highest values from different Columns?

  3. 3

    How to find out different cells from two excelsheets with the same format

  4. 4

    How to find out max or latest date? How to create Calculate fields? How to delete a field? - Rethinkdb

  5. 5

    How to find MAX value from two tables in SQL Server?

  6. 6

    SQL: find duplicates, with a different field

  7. 7

    Find string in two field with SQL

  8. 8

    How to find average of two different sql query using php

  9. 9

    How to find out the max value for Int in Swift

  10. 10

    How to convert data from two different conditions?

  11. 11

    how to sort data based on two different (text and number) conditions in sql server?

  12. 12

    How can I Update two fields in same table with different conditions (SQL)?

  13. 13

    Find out max & min of two number without using If else?

  14. 14

    How to get data for different conditions in SQL Server?

  15. 15

    SQL return 2 different dates from one field using Max

  16. 16

    How to find out where different packages are installed?

  17. 17

    Under what conditions do these two SQL queries give different results?

  18. 18

    Perform two counts (with different conditions) within the same SQL request

  19. 19

    Under what conditions do these two SQL queries give different results?

  20. 20

    sql query / getting latest entry based on two different conditions

  21. 21

    Find tax bracket using SQL, without a min and max field

  22. 22

    How to query two fields (different) into one field

  23. 23

    How to query two fields (different) into one field

  24. 24

    How to find out if two numbers are relatively prime?

  25. 25

    How to find out if two Strings are the same in TypeScript?

  26. 26

    How merge two select with different WHERE and special conditions

  27. 27

    How to divide the sum of a column under two different conditions?

  28. 28

    MySQL - How to find specific field with comparing two field in select query

  29. 29

    How to find the difference between two date cells of different rows in sql server 2012?

HotTag

Archive