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

  • Home
  • SEARCH
  • 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 8125195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:44:07+00:00 2026-06-06T06:44:07+00:00

I’m using Firebird 2.1. I could use some help creating the following query or

  • 0

I’m using Firebird 2.1.

I could use some help creating the following query or stored procedure.

I need to list the monthly averages of daily sums of petty cash transactions.

Let me explain:

TABLE PettyCash(RowID, TDate, InOut, Amount)

  • 1 2012-01-01 0 5.000
  • 2 2012-01-01 1 3.000
  • 3 2012-01-05 0 4.000
  • 4 2012-01-23 1 2.000
  • 5 2012-02-20 1 5.000

InOut = 0 if it’s an incoming, InOut = 1 if it’s an outgoing transaction

What the query has to do is to calculate the balance for every day in the month, sum up the amounts and then divide it by the number of days in the current month.

If there is no transaction on a given day, the balance stays the same.

So in January it goes like this:

  • 2012-01-01 2.000
  • 2012-01-02 2.000
  • 2012-01-03 2.000
  • …
  • 2012-01-05 6.000

and so on.

To complicate the matter there is always a starting balance being carried from the last year (that adds up to the balances this year).

I calculate that like this:

SELECT SUM(IIF(INOUT = 0, AMOUNT, -AMOUNT))
FROM PETTYCASH
WHERE TDATE < '2012-01-01';

The resulting query or stored proc should be given a start date and an end date – the start date is always the first day of a year, the end date could be a given day of the year, ie.: StartDate = 2012-01-01, EndDate = 2012-06-23

If the EndDate is not the last day of a month, the average of the last months should be divided only by the last day, in the example June should be divided by 23 instead of 30.

The result should be like this:
Month | Average of daily sums for the month

Any help would be greatly appreciated!

Thanks

  • 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-06T06:44:08+00:00Added an answer on June 6, 2026 at 6:44 am

    What you need is a subquery. The inner subquery calculates the sum on each day, the outer query calculates the averages.

    I am not familiar with firebird’s date functions, but it does have the “extract” function. The following uses this to get what you want:

    select yr, mon, avg(amt)
    from (SELECT extract(year from tdate) as yr, extract(month from tdate) as mon,
                 extract(day from tdate) as day,
                 SUM(IIF(INOUT = 0, AMOUNT, -AMOUNT)) as amt
          FROM PETTYCASH
          WHERE TDATE < '2012-01-01'
          group by extract(year from tdate), extract(month from tdate),
                   extract(day from tdate)
         ) t
    group by yr, mon
    order by yr, mon
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I have thousands of HTML files to process using Groovy/Java and I need to
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

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.