Firebird 2 Do While OR For Statement

Posted on 2006-07-07
Last Modified: 2013-12-09
Good DAY

i AM struggeling with a simple statement, I know how to do it the normal GROUP BY WAY, but my employee wants IT AS FOLLOWS.
My table cosist of accounting entries. E.G
Account No : Week :  Year : Amount.
1                   01       2005   100
2                   01       2005   100
3                   02       2005   100
4                   04       2005   100
5                   06       2005   100

** The report must show for each week the total value, the USER INPUT THE END WEEK
e.g if the end week = 04 then the report must show the following:
Week : Amount
01       200
02       100
03       0.00
04       100

NOW I dont know how to loop throw all the records and IF there are NO entry for that week HOW TO PUT 0.00 IN. MY BOSS WANT TO SEE THE ZERO WEEKS...

If i do a normal select Week, sum(Amount) group by week then i get the following output

Week : Amount
01       200
02       100
04       100

The user input the end week e.g 52, and i must give him then a report with weeks 1 to 52 on a report with or without a value.

Please help me..

Thank you


Question by:henryreynolds
  • 2
LVL 19

Accepted Solution

NickUpson earned 500 total points
Comment Utility
the easist way is to create a table called eg weeknos with one column called weekno

then you can do something like this (you don't show your table structure so I have to guess)

select a.weekno, sum(b.amount) from weeknos a left outer join accnos b on a.weekno = b.weekno group by a.weekno

NB depending on the definition of a week, some years have 53 weeks.

the alternative is to do it with a stored procedure in order to do the FOR loop you described

Author Comment

Comment Utility

the table looks exactly likes this.. (the table stores entries captured for a specific week and the amount paid for that week by a client)
Account No : Week :  Year : Amount.
1                   01       2005   100
2                   01       2005   100
3                   02       2005   100
4                   04       2005   100
5                   06       2005   100

IGNORE the 53 or 52 weeks... I use a stored procedure and receive a Week Number as input paramater..
the week number that i receive from the user tell me that the user want a summary of all the weeks currently in the table
with their sum(amount)
...this table does not reference any other table...this table acts like a ledger....
e.g (the user ask to see a sum of total amount up to and including week 8) IF THEIR ARE NO ENTRY CUURENTLY IN THE TABLE THEN SHOW 0.00)
week 1 total 900
week 2 total 599
week 3 total 800
week 4 total 0.00 ******
week 5 total 900
week 6 total 800
week 7 total 0.00 ******
week 8 total 9.00

MY problem is as follows .. I dont know how to use a select statement to show 0.00 if their are no entry in the table, I can do a normal sum per
week, BUT then I miss week number, I need all the week number up to and including the week the user has ask for.

E.g user ask to see a sum upto and including week 45, then i must habe 45 entries one entry for each week.. if there are no entry in my table yet
for a specific week then i must still show the week but 0.00


I hope yoy understand...

Enjoy your day

LVL 19

Expert Comment

Comment Utility
the sql I showed above with the left outer join will do what you require with two small corrections

select a.weekno, sum(coalesce(b.amount, 0)) from weeknos a left outer join accnos b on a.weekno = b.weekno
where a.weekno <= :passedparam
group by a.weekno

the sql cannot generate the missing week numbers so has to get them from somewhere hence the weeknos table

alternatively your SP can do it by counting up each week in turn and returning zero if there is no data, but the select will be quicker

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Introduction: I have seen many questions on EE and elsewhere, asking about how to find either gaps in lists of numbers (id field, usually) ranges of values or dates overlapping date ranges combined date ranges I thought it would be a good …
SQL Command Tool comes with APEX under SQL Workshop. It helps us to make changes on the database directly using a graphical user interface. This helps us writing any SQL/ PLSQL queries and execute it on the database and we can create any database ob…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now