1

I have a table called payhistory, and from this table I need to grab the Year, Month when a payment was made. I then need to get an average of payments grouped by Year and then month.

select AVG(totalpaid) from payhistory

This table has a column called datepaid that is where I need to grab the date from. It has a column called totalpaid which is where I need to grab the average from.

I then need a count of every payment within that month.

And finally a column displaying which Year/Month it was from.

Important columns in the table:

  • number (account number can use this to get a count)
  • datepaid (contains the date the payment was entered '2009-09-28 00:00:00.000')
  • totalpaid (contains the specific payment for that date)
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
James Wilson
  • 5,074
  • 16
  • 63
  • 122

1 Answers1

5

This should work:

select AVG(totalpaid),
   COUNT(number),
   MONTH(datepaid),
   YEAR(datepaid)
from payhistory
group by 
   MONTH(datepaid),
   YEAR(datepaid)
sgeddes
  • 62,311
  • 6
  • 61
  • 83