Imagine a table :
CUST_PROMO (customer_id,PROMOTION) which is used as a mapping between every promotion that customer have received.
select promotion, count(customer_id) as promo_size
from CUST_PROMO
group by promotion
This gets us the total number of customers in each promotion.
Now, we’ve got CUSTOMER (customer_id, PROMO_RESPONDED,PROMO_PURCHASED), which lists the customer and which promotion got the customer to respond, and which got them to purchase.
select PROMO_RESPONDED, count(customer_id) as promo_responded
from CUSTOMER
group by PROMO_RESPONDED
select PROMO_PURCHASED,count(customer_id) as promo_responded
from CUSTOMER
group by PROMO_PURCHASED
This is all very self-explanatory; now I’ve got the number of people for whom each promo was successful.
But; what I’d like to end up with is [in CSV form]
PROMOTION,PROMO_SIZE,PROMO_RESPONDED,PROMO_PURCHASED,PROMO_RESPSUCCESSRATE,blah
1,100,12,5,12%,...
2,200,23,14,11.5%,...
I have no idea how to do this. I can UNION the three queries above; but that doesn’t actually result in what I want. I thought about creating an in-memory table, inserting in each promo value and then doing an update statement with a join against it to set the values each — but that’s pretty messy; and requires a new UPDATE statement for each table/select statement. I could also make a temp table per result set and then join them together; but really; who wants to do that?
I can’t think of any way of joining this data that makes any sense; since I’m dealing with aggregates.
So, at best, I need a function that, like UNION, will combine result sets, but will actually combine like columns on a key and ADD those columns rather than union which adds rows. The description makes it sound like a JOIN; but I can’t see that working.
Thanks for the help!
1 Answer