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 Just The Latest

Get Just The Latest

Scheduled Pinned Locked Moved Database
questiondatabasecomlearning
6 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.
  • G Offline
    G Offline
    GenJerDan
    wrote on last edited by
    #1

    There is a badly designed Caché database I have to grab data from via ODBC. Rather than updating a table, they decided to just append the new records. (Won't they be surprised, eventually. :p) Anyway, is there a simple-ish way to get just the latest row for each set of records? Some sort of cool groupy join or something? "ID" is an autoincrememnt int Col2 is a varchar that identifies the set of records Col3 is the data I'm ultimately interested in. (There are other columns, of course, but probably irrelevant for this question...I hope) Looks sorta like this:

    ID Col2 Col3
    1 A 23
    2 A 23
    3 A 23
    4 A 24
    5 B 17
    6 B 18
    7 B 19
    8 C 01
    9 C 01

    What I want to end up with is

    4 A 24
    7 B 19
    9 C 01

    No dogs or cats are in the classroom. My Mu[sic] My Films My Windows Programs, etc.

    C D 2 Replies Last reply
    0
    • G GenJerDan

      There is a badly designed Caché database I have to grab data from via ODBC. Rather than updating a table, they decided to just append the new records. (Won't they be surprised, eventually. :p) Anyway, is there a simple-ish way to get just the latest row for each set of records? Some sort of cool groupy join or something? "ID" is an autoincrememnt int Col2 is a varchar that identifies the set of records Col3 is the data I'm ultimately interested in. (There are other columns, of course, but probably irrelevant for this question...I hope) Looks sorta like this:

      ID Col2 Col3
      1 A 23
      2 A 23
      3 A 23
      4 A 24
      5 B 17
      6 B 18
      7 B 19
      8 C 01
      9 C 01

      What I want to end up with is

      4 A 24
      7 B 19
      9 C 01

      No dogs or cats are in the classroom. My Mu[sic] My Films My Windows Programs, etc.

      C Offline
      C Offline
      Chris Meech
      wrote on last edited by
      #2

      This view might work

      create or replace view grouped_table as
      select col2
      ,max(ID)
      from table
      group by col2
      ;

      Then you query the table and join on the view

      select t.ID
      ,t.col2
      ,t.col3
      from table t
      ,grouped_table gt
      where t.id = gt.id

      This is just off the top of my head, though. :)

      Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra] posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]

      G 1 Reply Last reply
      0
      • C Chris Meech

        This view might work

        create or replace view grouped_table as
        select col2
        ,max(ID)
        from table
        group by col2
        ;

        Then you query the table and join on the view

        select t.ID
        ,t.col2
        ,t.col3
        from table t
        ,grouped_table gt
        where t.id = gt.id

        This is just off the top of my head, though. :)

        Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra] posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]

        G Offline
        G Offline
        GenJerDan
        wrote on last edited by
        #3

        Ah... Probably should I said I don't own the database, can't create anything on it, etc. Not even a temporary table/view. :(

        No dogs or cats are in the classroom. My Mu[sic] My Films My Windows Programs, etc.

        C 1 Reply Last reply
        0
        • G GenJerDan

          Ah... Probably should I said I don't own the database, can't create anything on it, etc. Not even a temporary table/view. :(

          No dogs or cats are in the classroom. My Mu[sic] My Films My Windows Programs, etc.

          C Offline
          C Offline
          Chris Meech
          wrote on last edited by
          #4

          In that case try this then

          select t.ID
          ,t.col2
          ,t.col3
          from table t
          ,( select col2
          ,max(ID) id
          from table
          group by col2
          ) gt
          where t.id = gt.id

          Again, off the top of my head. :)

          Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra] posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]

          G 1 Reply Last reply
          0
          • G GenJerDan

            There is a badly designed Caché database I have to grab data from via ODBC. Rather than updating a table, they decided to just append the new records. (Won't they be surprised, eventually. :p) Anyway, is there a simple-ish way to get just the latest row for each set of records? Some sort of cool groupy join or something? "ID" is an autoincrememnt int Col2 is a varchar that identifies the set of records Col3 is the data I'm ultimately interested in. (There are other columns, of course, but probably irrelevant for this question...I hope) Looks sorta like this:

            ID Col2 Col3
            1 A 23
            2 A 23
            3 A 23
            4 A 24
            5 B 17
            6 B 18
            7 B 19
            8 C 01
            9 C 01

            What I want to end up with is

            4 A 24
            7 B 19
            9 C 01

            No dogs or cats are in the classroom. My Mu[sic] My Films My Windows Programs, etc.

            D Offline
            D Offline
            David Mujica
            wrote on last edited by
            #5

            Using this table: create table tt ( id int, Col2 varchar(1), Col3 Int) This seems to work for me ...

            select * from TT where id in (
            select max(id) from tt group by col2)

            1 Reply Last reply
            0
            • C Chris Meech

              In that case try this then

              select t.ID
              ,t.col2
              ,t.col3
              from table t
              ,( select col2
              ,max(ID) id
              from table
              group by col2
              ) gt
              where t.id = gt.id

              Again, off the top of my head. :)

              Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra] posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]

              G Offline
              G Offline
              GenJerDan
              wrote on last edited by
              #6

              Looks like it's working. Thanks. Won't know for sure, though, until it finishes...if it does. Started it a little over three hours ago and it began delivering the goods about two hours after that. Oh...did I forget to mention this $%^$@#! table has over 177 million rows in it? And I expect the result to be in the neighborhood of 3 million? Hopefully, it will finish before I go home this afternoon. Otherwise, pfft! "Night Watchman" will come along at 3 a.m. and shut off my computer and I'll have to start all over again in the morning. :laugh: EDIT: :laugh: :laugh: :laugh: It finished. And, as soon as I clicked on the result to try to copy it, BOOM! 'So sorry. We've run out of virtual memory and have to close. Hope you weren't working on anything too terribly important'. Or words to that effect.

              No dogs or cats are in the classroom. My Mu[sic] My Films My Windows Programs, etc.

              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