Basically, I have a query like this:
SELECT dc.email, SUM(bft.amount_usd) AS TOTALSPENT,
SUM(CASE WHEN doct.Product_name = 'Books' THEN bft.amount_usd ELSE 0 END) TOTALSPENT_BOOKS,
SUM(CASE WHEN doct.Product_name = 'Shoes' THEN bft.amount_usd ELSE 0 END) TOTALSPENT_SHOES,
COUNT(DISTINCT CASE WHEN doct.CATEGORY_A = 1 THEN doct.order_guid ELSE NULL END) CATEGORY_A_count,
COUNT(DISTINCT CASE WHEN doct.CATEGORY_B = 1 THEN doct.order_guid ELSE NULL END) CATEGORY_B_count,
COUNT(DISTINCT CASE WHEN doct.CATEGORY_C = 1 THEN doct.order_guid ELSE NULL END) CATEGORY_C_count,
COUNT(DISTINCT CASE WHEN doct.CATEGORY_D = 1 THEN doct.order_guid ELSE NULL END) CATEGORY_D_count
FROM ProductTypeTable doct
INNER JOIN customerTable dc ON dc.customerID = doct.customerID
INNER JOIN transactionsTable bft ON bft.transactionID = doct.transactionID
GROUP BY dc.email
CATEGORY_A - is a Boolean field (1/0)
CATEGORY_B - is a Boolean field (1/0)
CATEGORY_C - is a Boolean field (1/0)
CATEGORY_D - is a Boolean field (1/0)
My output and desired output is:
How my query should look like to get the desired output?
I think the code below will give you what you need. The key is using "WITH ROLLUP" after the GROUP BY, which generates a grand total with a NULL key value(s).
SELECT
Total_eamil_count,
TOTALSPENT, TOTALSPENT_BOOKS, TOTALSPENT_SHOES,
CATEGORY_A_count, CATEGORY_B_count, CATEGORY_C_count, CATEGORY_D_count
FROM (
SELECT dc.email, SUM(1) AS Total_eamil_count, SUM(bft.amount_usd) AS TOTALSPENT,
SUM(CASE WHEN doct.Product_name = 'Books' THEN bft.amount_usd ELSE 0 END) TOTALSPENT_BOOKS,
SUM(CASE WHEN doct.Product_name = 'Shoes' THEN bft.amount_usd ELSE 0 END) TOTALSPENT_SHOES,
COUNT(DISTINCT CASE WHEN doct.CATEGORY_A = 1 THEN doct.order_guid ELSE NULL END) CATEGORY_A_count,
COUNT(DISTINCT CASE WHEN doct.CATEGORY_B = 1 THEN doct.order_guid ELSE NULL END) CATEGORY_B_count,
COUNT(DISTINCT CASE WHEN doct.CATEGORY_C = 1 THEN doct.order_guid ELSE NULL END) CATEGORY_C_count,
COUNT(DISTINCT CASE WHEN doct.CATEGORY_D = 1 THEN doct.order_guid ELSE NULL END) CATEGORY_D_count
FROM ProductTypeTable doct
INNER JOIN customerTable dc ON dc.customerID = doct.customerID
INNER JOIN transactionsTable bft ON bft.transactionID = doct.transactionID
GROUP BY dc.email WITH ROLLUP
) AS derived
WHERE email IS NULL
Thank you for your response.
I have a few questions so I understand this correctly.
- In my original question, what exactly "group by dc.email" is doing?
- why are we doing "where dc.email IS NULL"? Shouldn't it be NOT NULL?
- If I want to keep the complete query as-is (like you gave me) and then supply a list of email addresses to add where clause......where dc.email in (list here or table name), how it will change our new query that you have provided?
Thank you Scott for your help.
Query worked 
Hi Scott,
I need some help with the query. Can you please give me another example of how to break this query with the help of temp tables - so grabbing the fields that we need and put it into a temp table and then run the aggregates on top of the temp table?
The query that we worked on is taking a really long time to execute. Almost 1min