x
Solved

# Firebird 2 Do While OR For Statement

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

Thank you

Henry

0
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
0

Author Comment

ID: 17065275
hi

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

Thanx

I hope yoy understand...

Henry
0

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
0

## Featured Post

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.