Improve company productivity with a Business Account.Sign Up


Firebird 2 Do While OR For Statement

Posted on 2006-07-07
Medium Priority
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

Nick Upson earned 1500 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

by:Nick Upson
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

Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

In today's business world, data is more important than ever for informing marketing campaigns. Accessing and using data, however, may not come naturally to some creative marketing professionals. Here are four tips for adapting to wield data for insi…
Blockchain technology enhances society similar to the Internet. Its effects are broad, disruptive, and will boost global productivity.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Stellar Phoenix SQL Database Repair software easily fixes the suspect mode issue of SQL Server database. It is a simple process to bring the database from suspect mode to normal mode. Check out the video and fix the SQL database suspect mode problem.

589 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