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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 19

Accepted Solution

NickUpson earned 500 total points
ID: 17061302
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

ID: 17065275

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

ID: 17065301
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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
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…

759 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