I have a payment table in a SQL Server 2008 database, this holds both payment amounts and credit amounts, credits are identified by the field txt_pay_type which would be equal to ‘credit’.
I am trying to output some reports, one being total revenue per website.
I am using this select statement to get the sum and average values per website, how can i get it to take away the credit amounts.
So the final webTotal is less and credits.
I.e do a sum of all the payments which are not credits (like below), do a sum for all the payments which are credits and take one from the other. Or i am better to do this on the page with cold fusion?
E.g Website 1 Total of 5000 in payments, 1000 in credits. Results would be;
Website Total Revenue Average Sale
Website 1 4000 200.00
SELECT
txt_web_name,
SUM(mon_pay_amount) AS webTotal,
AVG(mon_pay_amount) AS webAvg
FROM
tbl_payment
INNER JOIN dbo.tbl_orders ON (uid_pay_orderid = uid_orders)
INNER JOIN dbo.tbl_websites ON (uid_order_webid = uid_websites)
WHERE
dbo.tbl_payment.bit_pay_paid = <cfqueryparam cfsqltype="cf_sql_bit" value="yes">
AND txt_pay_status <> <cfqueryparam cfsqltype="cf_sql_varchar" value="Credit">
GROUP BY
txt_web_name
Any pointers would be appreciated.
You need a conditional sum:
Notice for the sum, I default to 0, so no values in a month will result in $0. For the average, I default to NULL, so the credit values do not get calculated in the average.