Converting complex query with inner join to tableau

484 views Asked by At

I have a query like this, which we use to generate data for our custom dashboard (A Rails app) -

SELECT AVG(wait_time) FROM (
     SELECT TIMESTAMPDIFF(MINUTE,a.finished_time,b.start_time) wait_time
           FROM (
                SELECT max(start_time + INTERVAL avg_time_spent SECOND) finished_time, branch
                  FROM mytable
                 WHERE name IN ('test_name')
                   AND status = 'SUCCESS'
                 GROUP by branch) a
          INNER JOIN
                (
                SELECT MIN(start_time) start_time, branch
                  FROM mytable
                 WHERE name IN ('test_name_specific')
                GROUP by branch) b
          ON a.branch = b.branch
         HAVING avg_time_spent between 0 and 1000)t
GROUP BY week

Now I am trying to port this to tableau, and I am not being able to find a way to represent this data in tableau. I am stuck at how to represent the inner group by in a calculated field. I can also try to just use a custom sql data source, but I am already using another data source.

columns in mytable -

  1. start_time
  2. avg_time_spent
  3. name
  4. branch
  5. status

I think this could be achieved new Level Of Details formulas, but unfortunately I am stuck at version 8.3

1

There are 1 answers

0
Alex Blakemore On

Save custom SQL for rare cases. This doesn't look like a rare case. Let Tableau generate the SQL for you.

If you simply connect to your table, then you can usually write calculated fields to get the information you want. I'm not exactly sure why you have test_name in one part of your query but test_name_specific in another, so ignoring that, here is a simplified example to a similar query.

If you define a calculated field called worst_case_test_time datediff(min(start_time), dateadd('second', max(start_time), avg_time_spent)), which seems close to what your original query says.

It would help if you explained what exactly you are trying to compute. It appears to be some sort of worst case bound for avg test time. There may be an even simpler formula, but its hard to know without a little context.

You could filter on status = "Success" and avg_time_spent < 1000, and place branch and WEEK(start_time) on say the row and column shelves.

P.S. Your query seems a little off. Don't you need an aggregation function like MAX or AVG after the HAVING keyword?