Subject Re: [firebird-support] Advice on following SQL string
Author Norman Dunbar
Evening Andy,

> I have the following table in my database
>
> SALEHISTORY
>
> TRADEDATE VARCHAR(20);
> BARCODENUMBER VARCHAR(13);
> POSDESCRIPTION VARCHAR(20);
> QTYSOLD VARCHAR(20);
> LINETOTAL VARCHAR(20);
> SALEGROUP VARCHAR(4);

First "problem" - you are storing a date in a varchar. This is bad.
Dates are dates, they are not strings. I know that in Oracle, when you
store the "wrong" data type in a table, the optimiser cannot always make
the best choice of execution plan. Firebird *may* be similar - I'mm no
doubt get told off if I'm wrong! ;-)

> What I am trying to do is get a weekly summary of all the sales for each
> item PER DAY
This may not be the (complete) answer but maybe it will point you along
the road. To get the report of all sales PER DAY:

select tradedate, barcodenumber, posdescription, sum(qtysold)
from salehistory
group by tradedate,barcodenumber, posdescription
order by <whatever you like>;

Unfortunately, you are also storing qtysold, a number, in a string! See
above! ;-)

<SNIP>

> I have NO IDEA how to write the SQL Statement to deal with this (at the
> moment i am looping through the database and adding each number at a
> Time and this still does not work correctly)
Possibly, just maybe, some of the "numbers" are null? If so, a number
plus a null usually gives a null, so you would be losing your total so far?

Again, that's what used to happen, but I think - I'm away from my notes
on nulls etc in Firebird - that a fairly recent version allows nulls to
be added to numbers and counts them as zeros. Again, I'll be corrected
if I'm talking nonsense again!

> Any advice or even some sample SQL statements would be most welcome
Advice - always always always use the correct data type for your data. A
string is a string, a number is a number, a date is a date. Don't mix
them up!

Good luck (sorry Ann - I've used your catch phrase!)

Cheers,
Norm.

PS. I know you may have inherited this database and it's [bad] design,
so be gently with me!

--
Norman Dunbar
Dunbar IT Consultants Ltd

Registered address:
Thorpe House
61 Richardshaw Lane
Pudsey
West Yorkshire
United Kingdom
LS28 7EL

Company Number: 05132767