Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8383081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:04:31+00:00 2026-06-09T17:04:31+00:00

I have a payment table in a SQL Server 2008 database, this holds both

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T17:04:33+00:00Added an answer on June 9, 2026 at 5:04 pm

    You need a conditional sum:

    SELECT txt_web_name, SUM(mon_pay_amount) AS webTotal, AVG(mon_pay_amount) AS webAvg,
           sum(case when txt_pay_type <> 'Credit' then mon_pay_amount else 0 end
              ) as TotalNoCredit,
           avg(case when txt_pay_type <> 'Credit' then mon_pay_amount end
              ) as AvgNoCredit
    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
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using SQL Server 2005. I have a payments table with payment id's, user
Let us say that I have a table structured like this(using SQL server): empID
i have payment table fields update reason and amount & total field are change
I have a Billing table in my database that contains daily accruals of amounts
I have a table named PAYMENT . Within this table I have a user
This question applies to any database table design, where you would have system default
I have a payment_types table in which you can enter different payment types such
I have a MySQL table like this: Payments +----+---------------------+---------+ | id | date (sorted
I'm using SQL-Server 2005. I have two tables Users and Payments which has a
I have a Django-created tables in PostgreSQL 8.4 database, where one table extends another.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.