Selecting Latest Row From MAX Row of 3rd Table

blankabout

This is a related query to this, but it involves 3 tables instead of 2.

We have 3 tables:


People:
personid, name

PeopleEvents:
personid,eventid,eventdate

EventTypes:
eventid,eventname,eventsequence

The tables would have data like:


People:
1,John
2,Mary
3,Jane
4,Rob

PeopleEvents:
1,100,1/1/2013
2,100,2/2/2013
2,102,2/3/2013
2,104,3/3/2013
3,100,4/4/2013
3,102,4/4/2013
4.100,2/2/2013
4,103,4/4/2013
4,104,3/3/2013

EventTypes:
100,Joined company,10
101,Induction,20
102,Introduction,17
103,Second meeting,25
104,First meeting,15

We want to be able to pull out a list of people based on highest EventType sequence, NOT event ID.

Thus the report would be:


John,Joined Company
Mary,Introduction
Jane,Introduction
Rob,Second meeting

This to me seems inelegant but appears to work:


SELECT p.personid,et.eventname FROM people p 
INNER JOIN peopleevents pe ON p.personid = pe.personid
INNER JOIN eventtypes et ON et.eventid = pe.eventid
WHERE p.personid NOT IN
(SELECT p1.personid FROM people p1 LEFT JOIN peopleevents pe1 ON p1.personid = p.personid
LEFT JOIN eventtypes et1 ON et1.eventid = pe1.eventid
WHERE p1.personid = p.personid AND et1.eventseq > et.eventset) ORDER BY p.name;

The way I see it the sub-query is attempting to find a higher sequence than the main query and thus only allowing the main query to return the highest sequenced event.

I will enter the answers provided to see if one of them will provide a tidier solution.

Devart

Try this query -

SELECT * FROM (
  SELECT p.*, et.eventname, et.eventsequence FROM people p
    JOIN PeopleEvents pe
      ON p.personid = pe.personid
    JOIN EventTypes et
      ON et.eventid = pe.eventid
  ORDER BY eventsequence DESC
) t
GROUP BY personid

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

mySQL - Selecting the latest row from a table

From Dev

Selecting row from table in laravel

From Dev

selecting the single largest/smallest/first/latest row from a table where dates/amounts are not unique

From Dev

Selecting row from Table - Java Selenium

From Dev

MySQL - selecting random row from large table

From Dev

What is the best way for selecting 2 row from table in one row?

From Dev

Selecting a row in a html table

From Dev

table with first row - 1 cell, second row - 2 cells, 3rd row - 3 cells

From Dev

fetch 2nd row, 3rd row....200 row of a table in oracle by servlet

From Dev

selecting records from main table and count of each row in another table

From Dev

Selecting the latest row using groupby in Laravel

From Dev

Selecting the row with latest date in one column

From Dev

Find specific value in 2nd row and display value from 3rd row

From Dev

Find specific value in 2nd row and display value from 3rd row

From Dev

Count each value appearance in one row and get 3rd corresponding value from another row

From Dev

Selecting two rows from another table using one row

From Dev

Inserting Into a Row and Then Selecting from that Row in One Query

From Dev

mySQL is not selecting the values of the row with the MAX date

From Dev

Selecting largest number from row

From Dev

Selecting a row in table based on content and clicking link from selected row - Protractor

From Dev

Join row with MAX row in another table in Impala?

From Dev

SQL: Identifying latest max sequence per row

From Dev

Remove extra commas from only 2nd and 3rd row of CSV file

From Dev

Django get 2nd,3rd,4th column from row. for loop

From Dev

Creating a trigger on insert on a table, that creates new rows on another table, according to amount of row on another (3rd) table

From Dev

Return Text from Table Headers (Col/Row) Using MAX Function

From Dev

Select info from table where row has max date

From Dev

Retrieve Last Row from a table using Max or top or rowset

From Dev

Displaying entire row of max() value from nested table

Related Related

  1. 1

    mySQL - Selecting the latest row from a table

  2. 2

    Selecting row from table in laravel

  3. 3

    selecting the single largest/smallest/first/latest row from a table where dates/amounts are not unique

  4. 4

    Selecting row from Table - Java Selenium

  5. 5

    MySQL - selecting random row from large table

  6. 6

    What is the best way for selecting 2 row from table in one row?

  7. 7

    Selecting a row in a html table

  8. 8

    table with first row - 1 cell, second row - 2 cells, 3rd row - 3 cells

  9. 9

    fetch 2nd row, 3rd row....200 row of a table in oracle by servlet

  10. 10

    selecting records from main table and count of each row in another table

  11. 11

    Selecting the latest row using groupby in Laravel

  12. 12

    Selecting the row with latest date in one column

  13. 13

    Find specific value in 2nd row and display value from 3rd row

  14. 14

    Find specific value in 2nd row and display value from 3rd row

  15. 15

    Count each value appearance in one row and get 3rd corresponding value from another row

  16. 16

    Selecting two rows from another table using one row

  17. 17

    Inserting Into a Row and Then Selecting from that Row in One Query

  18. 18

    mySQL is not selecting the values of the row with the MAX date

  19. 19

    Selecting largest number from row

  20. 20

    Selecting a row in table based on content and clicking link from selected row - Protractor

  21. 21

    Join row with MAX row in another table in Impala?

  22. 22

    SQL: Identifying latest max sequence per row

  23. 23

    Remove extra commas from only 2nd and 3rd row of CSV file

  24. 24

    Django get 2nd,3rd,4th column from row. for loop

  25. 25

    Creating a trigger on insert on a table, that creates new rows on another table, according to amount of row on another (3rd) table

  26. 26

    Return Text from Table Headers (Col/Row) Using MAX Function

  27. 27

    Select info from table where row has max date

  28. 28

    Retrieve Last Row from a table using Max or top or rowset

  29. 29

    Displaying entire row of max() value from nested table

HotTag

Archive