« Ó Eating House: Not For Food | Main | Ruby Advocacy »

March 18, 2005

Pretty SQL Statement

I wrote this today, and I’m proud of it.


SELECT c.code, c.type, c.start_date, c.end_date,
            c.amount, c.minimum_purchase, 
            COUNT (o.order_id) AS num_orders,
            COALESCE( SUM (o.total), 0.00) AS 
                total_spent,
            COALESCE( SUM (o.discount), 0.00) AS 
                total_discount 
FROM coupons AS c
LEFT JOIN orders AS o ON o.coupon = c.code 
        AND o.order_status = 'COMPLETE'
GROUP BY c.code;

Posted by FusionGyro at March 18, 2005 10:12 PM

Trackback Pings

TrackBack URL for this entry:
http://www.clanspum.net/~fusion/blog/admin/mt-tb.cgi/102

Comments

Post a comment




Remember Me?

(you may use HTML tags for style)

Want HTML? Use Textile instead.