Total of Non-Insurance Payments by Year

I need the yearly total of non-insurance payments for the years of 2010 and 2011.

Although this is a relatively easy query, it is a good demonstration of the aggregate SUM function, the handy YEAR function, and the GROUP BY clause. Note that the requirement that only non-insurance payments be included means that we have to JOIN our way all the way from the credit entry to the payors table, where the payortype value is stored. In this case we can make the query more compact by taking advantage of “natural” joins that are already present among the four tables in the query. Natural joins use existing primary and foreign keys so we don’t have to specify the ON table.column = table.column syntax you usually would include in such queries. It doesn’t hurt to include it, but in this case there are natural relationships that imply the ON specifications.

Select All Code:
1
2
3
4
5
6
7
SELECT YEAR(trandate) AS "yr",  SUM (amount) AS "Payments"
FROM journal a JOIN jcredits b JOIN ptpayors c JOIN payors d 
WHERE trantype ='P'
AND d.payortype <> 'I'
AND trandate BETWEEN '2010-01-01' AND '2011-12-31'
GROUP BY "yr"
ORDER BY "yr"

Leave a Reply

  

  

  

* Copy this password:

* Type or paste password here:

1,023 Spam Comments Blocked so far by Spam Free Wordpress

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">