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. MaxDate

MaxDate

Scheduled Pinned Locked Moved Database
question
4 Posts 2 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.
  • S Offline
    S Offline
    sardinka
    wrote on last edited by
    #1

    :((I have 3 tables: table1: DateMod (smalldatetime) StId Eid table2: DateMod (smalldatetime) StId Wid table3: stid I am selecting the max(Wid) and max(eid) based onstid: select max(Wid),date from table2 a,table 3 where a.stid=b.stid group by a.stid union select max(eid),date from table1 a,table 3 where a.stid=b.stid group by a.stid At this point I need to compare the dates between the results (if sid listed in both results) and select the most resent value from one of the tables for each stid. How do I do this?

    B 1 Reply Last reply
    0
    • S sardinka

      :((I have 3 tables: table1: DateMod (smalldatetime) StId Eid table2: DateMod (smalldatetime) StId Wid table3: stid I am selecting the max(Wid) and max(eid) based onstid: select max(Wid),date from table2 a,table 3 where a.stid=b.stid group by a.stid union select max(eid),date from table1 a,table 3 where a.stid=b.stid group by a.stid At this point I need to compare the dates between the results (if sid listed in both results) and select the most resent value from one of the tables for each stid. How do I do this?

      B Offline
      B Offline
      basementman
      wrote on last edited by
      #2

      If this is against SQL Server or Oracle, you could create a stored proc to do this in two passes or get real complex using subqueries in one pass. TWO PASS: create procedure GetLastModDate AS declare @tStidDates (stid int NULL, LastModDate smalldatetime NULL) set nocount on insert @tStidDates ( stid, LastModDate ) select A.stid, MAX(A.Wid) from table2 a, table 3 b where a.stid = b.stid group by a.stid insert @tStidDates ( stid, LastModDate ) select A.stid, MAX(A.eid) from table1 a, table 3 b where a.stid = b.stid group by a.stid select stid, MAX(LastModDate) from @tStidDates group by stid order by 1  onwards and upwards...

      S 1 Reply Last reply
      0
      • B basementman

        If this is against SQL Server or Oracle, you could create a stored proc to do this in two passes or get real complex using subqueries in one pass. TWO PASS: create procedure GetLastModDate AS declare @tStidDates (stid int NULL, LastModDate smalldatetime NULL) set nocount on insert @tStidDates ( stid, LastModDate ) select A.stid, MAX(A.Wid) from table2 a, table 3 b where a.stid = b.stid group by a.stid insert @tStidDates ( stid, LastModDate ) select A.stid, MAX(A.eid) from table1 a, table 3 b where a.stid = b.stid group by a.stid select stid, MAX(LastModDate) from @tStidDates group by stid order by 1  onwards and upwards...

        S Offline
        S Offline
        sardinka
        wrote on last edited by
        #3

        for some reason SQL is not taking this line: declare @tStidDates (stid int NULL, LastModDate smalldatetime NULL)

        B 1 Reply Last reply
        0
        • S sardinka

          for some reason SQL is not taking this line: declare @tStidDates (stid int NULL, LastModDate smalldatetime NULL)

          B Offline
          B Offline
          basementman
          wrote on last edited by
          #4

          Sorry, it should be declare @tStidDates table (stid int NULL, LastModDate smalldatetime NULL)  onwards and upwards...

          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