Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Get the max elements in a join?

Get the max elements in a join?

Scheduled Pinned Locked Moved Database
databasequestionlearning
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Bastien Vandamme
    wrote on last edited by
    #1

    I have two tables 1. A table USER with a USER_ID, NAME, ... 2. A table STATUS with a STATUS_ID, PEOPLE_FK (foreign key to USER_ID), LABEL, DATE_START... A USER can only have one active status. I use the DATE_START field to keep the history of all status of the user. I never delete a status. If I create a join between USER and STATUS I can easily get all my users with all they status. BUT I would like to create a query to retrieve all my users with they last status only. I'm still learning SQL. What should I use? Is a select in a select recommended? I would like to avoid the solution of with subquesries. EDIT -- I searched for a duplicate of this question without success. If this question has been already answered just show me the link. thank you.

    Kornfeld Eliyahu PeterK J 2 Replies Last reply
    0
    • B Bastien Vandamme

      I have two tables 1. A table USER with a USER_ID, NAME, ... 2. A table STATUS with a STATUS_ID, PEOPLE_FK (foreign key to USER_ID), LABEL, DATE_START... A USER can only have one active status. I use the DATE_START field to keep the history of all status of the user. I never delete a status. If I create a join between USER and STATUS I can easily get all my users with all they status. BUT I would like to create a query to retrieve all my users with they last status only. I'm still learning SQL. What should I use? Is a select in a select recommended? I would like to avoid the solution of with subquesries. EDIT -- I searched for a duplicate of this question without success. If this question has been already answered just show me the link. thank you.

      Kornfeld Eliyahu PeterK Offline
      Kornfeld Eliyahu PeterK Offline
      Kornfeld Eliyahu Peter
      wrote on last edited by
      #2

      The probably best solution is to add an ACTIVE column to your STATUS table and make an unique index from ACTIVE and USER_ID. So you never ever will be able to set two rows to active state - or you will try and got an SQL error... It will also make your query simple as you always ask for WHERE ACTIVE = 1...

      I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)

      "It never ceases to amaze me that a spacecraft launched in 1977 can be fixed remotely from Earth." ― Brian Cox

      1 Reply Last reply
      0
      • B Bastien Vandamme

        I have two tables 1. A table USER with a USER_ID, NAME, ... 2. A table STATUS with a STATUS_ID, PEOPLE_FK (foreign key to USER_ID), LABEL, DATE_START... A USER can only have one active status. I use the DATE_START field to keep the history of all status of the user. I never delete a status. If I create a join between USER and STATUS I can easily get all my users with all they status. BUT I would like to create a query to retrieve all my users with they last status only. I'm still learning SQL. What should I use? Is a select in a select recommended? I would like to avoid the solution of with subquesries. EDIT -- I searched for a duplicate of this question without success. If this question has been already answered just show me the link. thank you.

        J Offline
        J Offline
        Jorgen Andersson
        wrote on last edited by
        #3

        CREATE TABLE statuser
        ([userid] int)
        ;

        INSERT INTO statuser
        ([userid])
        VALUES
        (1),
        (2)
        ;

        CREATE TABLE stat
        ([userid] int, [status] int, [startdate] date)
        ;

        INSERT INTO stat
        ([userid], [status], [startdate])
        VALUES
        (1, 1, '2012-05-30'),
        (1, 2, '2013-06-23')
        ;
        with maxstat as (
        select s.userid,max(startdate) startdate
        from statuser su
        left outer join stat s
        on su.userid = s.userid
        group by s.userid
        )
        select s.userid,s.status,s.startdate
        from maxstat m
        join stat s
        on m.userid = s.userid
        and m.startdate = s.startdate

        Oh, also make a habit of having the same name of the fields in all tables, so if it's called USER_ID in one table don't call it PEOPLE_FK in another. It's much easier to debug that way.

        Wrong is evil and must be defeated. - Jeff Ello[^]

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups