1

I'm entirely willing to admit that I am going about this incorrectly, but here's the situation. I was given a table like this:

qtr    cust_type      cust_cnt           

QTR1   new_cust       100
QTR1   return_cust    50 
QTR2   new_cust       150  
QTR2   return_cust    75

and I am trying to create a new cust_type row for each qtr that shows the combined amounts of the other cust_types, like this as an end result:

qtr    cust_type      cust_cnt           

QTR1   new_cust       100
QTR1   return_cust    60 
QTR1   total_cust     160
QTR2   new_cust       150  
QTR2   return_cust    75
QTR2   total_cust     225

Why on earth would I do this? The reason is not great- Tableau is very picky about how information is input in order to form specific graph types.

Is there some SQL syntax that can accomplish the second graph? I cannot seem to figure one out. I'm also very new to SQL :D

2
  • I think Tableau should be able to add those summary rows. Either way the union approach should be good too. Commented Aug 24, 2018 at 16:37
  • Why the 50 in the first quarter for returning customers becomes 60? Commented Aug 25, 2018 at 2:17

2 Answers 2

1

You could use:

SELECT qtr, cust_type, cust_cnt   
FROM tab
UNION ALL
SELECT qtr, 'total_cust' AS cust_type, SUM(cust_cnt) AS cust_cnt
FROM tab
GROUP BY qtr
ORDER BY qtr, cust_cnt;
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this is the logical structure I needed.
0

I encountered the same when visualize the data in Tableau. It supports total if you present data in a grid but sometimes we need show the total in a dropdown list. Unfortunately, Redshift doesn't support cube or rollup. I did use the same way what Lukasz suggested.

1 Comment

Definitely some things to have to get used to based on the database to visualization combo!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.