2 Replies Latest reply on Feb 18, 2016 3:40 PM by Jared Maruskin

    Raw SQL using Tableau fields:  Show only latest update for each Group

    Jared Maruskin

      I am trying to use Raw SQL to get the most recent timestamp for each of several groups.  So far, I was successful in only pulling the global max(timestamp) from my RawSQL query. 

       

      Here is a simplified view... I have a dimension called GROUP and TIMESTAMP....

       

      GROUP
      TIMESTAMP
      Calculated Field
      Intended Result
      DATA
      A1/1/16  2:00:001/8/19   04:15:161/2/16  17:19:19...
      A1/2/16  3:45:441/8/19   04:15:161/2/16  17:19:19...
      A1/2/16  17:19:191/8/19   04:15:161/2/16  17:19:19...
      B1/1/16  15:09:441/8/19   04:15:161/1/16  19:19:19...
      B1/1/16  19:19:191/8/19   04:15:161/1/16  19:19:19...
      C1/5/16  12:12:551/8/19   04:15:161/8/19   04:15:16...
      C1/5/16   13:12:571/8/19   04:15:161/8/19   04:15:16...
      C1/8/19   04:15:161/8/19   04:15:161/8/19   04:15:16...

       

      I have a Calculated Field:  RAWSQL_DATETIME("select max(TIMESTAMP) from Database where GROUP = %1", [GROUP])

       

      It is returning the global maximum, as shown in my "Calculated Field" column. ....  I want the RAWSQL query to look at each item in Tableau, and do filter by  database.group = [GROUP], so that it returns only the maximum for the given group, as shown in my "Intended Result" column. 

       

      Finally, I will write a second calculated field:  IF [TIMESTAMP] = [INTENDED RESULT] THEN "LAST UPDATE"  ELSE  "OLD INFO" END, and filter by [2nd calculated field] = "LAST UPDATE"  to obtain my finalize intended view: 

       

      GROUP
      TIMESTAMP
      Intended Result
      DATA
      A1/2/16  17:19:191/2/16  17:19:19...
      B1/1/16  19:19:191/1/16  19:19:19....
      C1/8/19   04:15:161/8/19   04:15:16

       

      So that for each GROUP, I only see the data corresponding to my last update. 

       

       

      Please help!! 

       

      Thank you!