Solved

Firebird 2 Do While OR For Statement

Posted on 2006-07-07
3
448 Views
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

Henry

0
Comment
Question by:henryreynolds
  • 2
3 Comments
 
LVL 19

Accepted Solution

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

Author Comment

by:henryreynolds
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...

Enjoy your day

Henry
0
 
LVL 19

Expert Comment

by:NickUpson
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
As technology users and professionals, we’re always learning. Our universal interest in advancing our knowledge of the trade is unmatched by most industries. It’s a curiosity that makes sense, given the climate of change. Within that, there lies a…
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…

813 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