EDIT : Because I'm using ExactTarget (Salesforce Marketing Cloud), I only have access to the SELECT statement. None of the good SQL that would make this easy.
I have a table of Customers and a table of Coupons. I'd like to assign unique codes to eligible Customers but I'm a little bit stuck on query #1. (Query #2 is an easy join.)
TABLE 1
+-------------+-------------+------------+ | Customer | CouponType | CouponCode | +-------------+-------------+------------+ | Customer1 | 20pctoff | | | Customer2 | 20pctoff | | | Customer3 | 10pctoff | | | Customer4 | NULL | | +-------------+-------------+------------+
TABLE 2
+-------------+-------------+------------+ | CouponType | CouponCode | AssignedTo | +-------------+-------------+------------+ | 10pctoff | Coupon1 | | | 20pctoff | Coupon2 | | | 30pctoff | Coupon3 | | +-------------+-------------+------------+
Query #1 would result in the following:
Customer1 = Coupon2
Customer2 = none (no more 20pctoff available)
Customer3 = Coupon1
Customer4 = none (not eligible)
The second SQL to update table 2 is a simple join (I don't need help with that).
Solved with a third table. Inelegant as heck, but that's the price to pay for using ExactTarget sometimes. Caveat, requires iterating through known CouponCodes 1 at a time.
New table:
+-----+------------------+--------------+ | ID | Customer | CouponCode | +-----+------------------+--------------+
Target table3, overwrite
SELECT row_number() OVER (order by Customer) as ID, Customer from Table1 where CouponType = '20pctoff'
Target table3, update
SELECT row_number() OVER (order by CouponCode) as ID, CouponCode from Table2 where CouponType = '20pctoff' and AssignedTo is NULL
Target table1, update
SELECT t3.Customer, t3.CouponCode FROM table3 t3 inner join table1 t1 on t1.Customer = t3.Customer where t3.Customer is not NULL and t3.CouponCode is not NULL
Target table2, update
SELECT t3.Customer as AssignedTo, t3.CouponCode FROM table3 t3 inner join table2 t2 on t2.CouponCode = t3.CouponCode where t3.Customer is not null and t3.CouponCode is not null
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.