Counting presence of discount ranges in SQL -
i have simple orders table product being sold has column discount per order. example:
order number discountprcnt 1234 0 1235 10 1236 41
what create output can join order table customer table , group discounts ranges email, follows:
email_address 0-20 20-50 50-100 joe@abc.com yes yes tom@abc.com yes yes
so idea determine if each customer (here designated email) has ever received discount in specified range, , if not, should return null range.
a simplified version of table structure is:
customer table: custid email 123 joe@abc.com 234 tom@abc.com 456 joe@abc.com
so emails can repeat across customers.
orders table: custid orderid amount discprcnt 123 1234 50.00 0 234 1235 75.00 10 456 1236 20.00 41
select c.email, count(o1.order_number), count(o2.order_number), count(o3.order_number) customer c, order o1, order o2, order o3 c.custid = o1.custid , c.custid = o2.custid , c.custid = o3.custid , o1.discountprcnt > 0 , o1.discountprcnt <= 20 , o2.discountprcnt > 2 , o2.discountprcnt <= 50 , o3.discountprcnt > 50 , o3.discountprcnt <= 100 group c.email
you'll not yes expected number of time user benefits discount. 0 if none.
Comments
Post a Comment