🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.interbase.sql
19 messages
19 total messages Started by Isaac Mon, 09 Jun 2008 23:02
need advice about sql for data mining
#146
Author: Isaac
Date: Mon, 09 Jun 2008 23:02
30 lines
1882 bytes
Hello,
I am looking for a logical and fast way to make the following

I have 5 tables:
1- inventory items (item code, name, etc...)

2- purchased invoices (date, invoice no, etc...)
3- detail table for table purchased invoices to store invoices items 
(item code, quantity, price, etc...)

4- sold invoices (date, invoice no, etc...)
5- detail table for table sold invoices to store invoices items (item 
code, quantity, price, etc...)

now I need to store all the items from table 1 into the following table 
ordered by date so I can calculate average cost

6- table average cost:

Date        purchased qty           cost price            sold qty
-----       --------------            ----------            ---------
5/6          50                             40.5                    0
7/6            40                           42                       
23      and so on

I am thinking to for using insert into with select expression for table 
2 and 3 but I need to also insert quantity sold for the same date how I 
can do that ?, again speed is critical

Thanks
Re: need advice about sql for data mining
#149
Author: Isaac
Date: Mon, 09 Jun 2008 23:36
8 lines
1277 bytes
Notes:
it did not have to be this way, if I can get only the items that have 
transactions (purchases and sales) ordered by date will be faster but I 
have to test for each row if this is a purchase or a sale so I can do 
the math correclty, however I dont know how it can be done, any ideas 
will be appreciated I only need pseudo code and the general idea

Thanks
Re: need advice about sql for data mining
#150
Author: Maxim Shiryaev
Date: Tue, 10 Jun 2008 07:36
82 lines
3324 bytes
Hi.

I think you need a SP here.

for
  select date
    from purchase

    union all

    select date
    from sales

    group by 1
  into :d
  /* now you have all the dates here */
do begin

  select sum(purchase_line.qty), sum(purchase_line.price *
purchase_line.qty),
  from purchase
    left join purchase_line on purchase_line.purchase_id =
purchase.purchase_id
  where
    purchase.date = :d
  into :purchase_qty, :purchase_amount;

  select sum(sales_line.qty), sum(sales_line.price * sales_line.qty)
  from sales
    left join sales_line on purchase_line.sales_id = sales.sales_id
  where
    sales.date = :d
  into :sales_qty, :sales_amount;

  /* be prepared to deal with NULLs in qty and amount here */

  /* calculate whatever needed */

end

And I would even combine both purchases and sales into single table with one
additional field like SIGN with +1 | -1 values to differentioate between two
types of operations. So you can create an index on date and get fast GROUP
BY speed. If so you could make the result in one pass ordering by dates and
summing appropiate variable depending on sign.


Maxim.


"Isaac" <no@no.com> ???????/???????? ? ???????? ?????????:
news:484db5e2@newsgroups.borland.com...
> Hello,
> I am looking for a logical and fast way to make the following
>
> I have 5 tables:
> 1- inventory items (item code, name, etc...)
>
> 2- purchased invoices (date, invoice no, etc...)
> 3- detail table for table purchased invoices to store invoices items
> (item code, quantity, price, etc...)
>
> 4- sold invoices (date, invoice no, etc...)
> 5- detail table for table sold invoices to store invoices items (item
> code, quantity, price, etc...)
>
> now I need to store all the items from table 1 into the following table
> ordered by date so I can calculate average cost
>
> 6- table average cost:
>
> Date        purchased qty           cost price            sold qty
> -----       --------------            ----------            ---------
> 5/6          50                             40.5                    0
> 7/6            40                           42
> 23      and so on
>
> I am thinking to for using insert into with select expression for table
> 2 and 3 but I need to also insert quantity sold for the same date how I
> can do that ?, again speed is critical
>
> Thanks
Re: need advice about sql for data mining
#152
Author: Craig Stuntz TeamB
Date: Tue, 10 Jun 2008 13:23
32 lines
1674 bytes
Maxim Shiryaev wrote:

>   select date
>     from purchase
> 
>     union all
> 
>     select date
>     from sales
> 
>     group by 1

	This isn't valid InterBase SQL.

Instead, do:

SELECT DISTINCT
  date
FROM
  purchase
UNION 
SELECT DISTINCT
  date
FROM
  sales
INTO ...

-- 
Craig Stuntz [TeamB] � Vertex Systems Corp. � Columbus, OH
  Delphi/InterBase Weblog : http://blogs.teamb.com/craigstuntz
Want to help make Delphi and InterBase better?  Use QC!
  http://qc.borland.com -- Vote for important issues
Re: need advice about sql for data mining
#206
Author: Isaac
Date: Tue, 10 Jun 2008 16:55
32 lines
1456 bytes
Craig Stuntz [TeamB] wrote:
> Maxim Shiryaev wrote:
>
>   
>>   select date
>>     from purchase
>>
>>     union all
>>
>>     select date
>>     from sales
>>
>>     group by 1
>>     
>
> 	This isn't valid InterBase SQL.
>
> Instead, do:
>
> SELECT DISTINCT
>   date
> FROM
>   purchase
> UNION 
> SELECT DISTINCT
>   date
> FROM
>   sales
> INTO ...
>
>   
Thanks
Re: need advice about sql for data mining
#207
Author: Isaac
Date: Tue, 10 Jun 2008 16:56
57 lines
2285 bytes
Maxim Shiryaev wrote:
> Hi.
>
> I think you need a SP here.
>
> for
>   select date
>     from purchase
>
>     union all
>
>     select date
>     from sales
>
>     group by 1
>   into :d
>   /* now you have all the dates here */
> do begin
>
>   select sum(purchase_line.qty), sum(purchase_line.price *
> purchase_line.qty),
>   from purchase
>     left join purchase_line on purchase_line.purchase_id =
> purchase.purchase_id
>   where
>     purchase.date = :d
>   into :purchase_qty, :purchase_amount;
>
>   select sum(sales_line.qty), sum(sales_line.price * sales_line.qty)
>   from sales
>     left join sales_line on purchase_line.sales_id = sales.sales_id
>   where
>     sales.date = :d
>   into :sales_qty, :sales_amount;
>
>   /* be prepared to deal with NULLs in qty and amount here */
>
>   /* calculate whatever needed */
>
> end
>
> And I would even combine both purchases and sales into single table with one
> additional field like SIGN with +1 | -1 values to differentioate between two
> types of operations. So you can create an index on date and get fast GROUP
> BY speed. If so you could make the result in one pass ordering by dates and
> summing appropiate variable depending on sign.
>
>
> Maxim.
>
>
>
>   
>
>
>   
Thanks , but I did not understand how I can make the sign field
Re: need advice about sql for data mining
#209
Author: Isaac
Date: Tue, 10 Jun 2008 19:40
3 lines
975 bytes
any function in InterBase to get the absolute value ? like abs() in delphi

Thanks
Re: need advice about sql for data mining
#210
Author: Bill Todd [TeamB]
Date: Tue, 10 Jun 2008 20:45
12 lines
1216 bytes
Isaac wrote:

> any function in InterBase to get the absolute value ? like abs() in
> delphi
> 
> Thanks

Have you checked the standard UDF library that comes with Delphi? IIRC
it contains an ABS function.

-- 
Bill Todd (TeamB)
Re: need advice about sql for data mining
#211
Author: Quinn Wildman (CodeGear Develope...
Date: Tue, 10 Jun 2008 22:19
7 lines
1207 bytes
I think Bill means InterBase not Delphi here. :-)

Bill Todd [TeamB] wrote:

> Have you checked the standard UDF library that comes with Delphi? IIRC
> it contains an ABS function.
> 
Re: need advice about sql for data mining
#212
Author: Bill Todd [TeamB]
Date: Tue, 10 Jun 2008 22:44
8 lines
1301 bytes
Quinn Wildman (CodeGear Developer Support) wrote:

> I think Bill means InterBase not Delphi here. :-)

Indeed I do.:)

-- 
Bill Todd (TeamB)
Re: need advice about sql for data mining
#213
Author: Isaac
Date: Wed, 11 Jun 2008 14:47
1 lines
1069 bytes
ok thanks
Re: need advice about sql for data mining
#214
Author: Isaac
Date: Wed, 11 Jun 2008 15:16
10 lines
1097 bytes
when I use for select with date field I automatically begin from the 
first of the table and the date in ascending ordered ?

for select <date field>
into :v_date
do  begin
.
.
.
end
Re: need advice about sql for data mining
#215
Author: Quinn Wildman (CodeGear Develope...
Date: Wed, 11 Jun 2008 15:18
14 lines
1343 bytes
If you are concerned about ordering you need to add an order by 
statement. Otherwise, ordering cannot be guaranteed.

Isaac wrote:
> when I use for select with date field I automatically begin from the 
> first of the table and the date in ascending ordered ?
> 
> for select <date field>
> into :v_date
> do  begin
> .
> .
> .
> end
Re: need advice about sql for data mining
#216
Author: Wayne Niddery (TeamB)
Date: Wed, 11 Jun 2008 16:32
22 lines
1794 bytes
"Isaac" <no@no.com> wrote in message news:484febae@newsgroups.borland.com...
> when I use for select with date field I automatically begin from the first 
> of the table and the date in ascending ordered ?
>
> for select <date field>
> into :v_date
> do  begin


When deal with SQL servers, always assume it is your responsibility to 
request the order you want. Just include an order by:

 for select <date field>
order by <date field>
into :v_date

Ascending is default, if you want descending, add "desc" following the field 
name.

-- 
Wayne Niddery - TeamB (www.teamb.com)
Winwright, Inc. (www.winwright.ca) 
Re: need advice about sql for data mining
#220
Author: Isaac
Date: Sat, 14 Jun 2008 13:17
3 lines
1138 bytes
OK Thanks alot Quinn Wildman,
by the way I hope InterBase to become fully embedded that can run 
without installing server, is there a schedule for that ?
Re: need advice about sql for data mining
#218
Author: Isaac
Date: Sat, 14 Jun 2008 13:17
12 lines
1465 bytes
Wayne Niddery (TeamB) wrote:
> When deal with SQL servers, always assume it is your responsibility to 
> request the order you want. Just include an order by:
>
> for select <date field>
> order by <date field>
> into :v_date
>
> Ascending is default, if you want descending, add "desc" following the 
> field name.
>
Many thanks I will add order by to ensure ordering
Re: need advice about sql for data mining
#223
Author: Isaac
Date: Sat, 14 Jun 2008 14:43
6 lines
1173 bytes
one last question if I have union selects can I add one order by like this
order by 1

or I have to add order by after every union select

Thanks
Re: need advice about sql for data mining
#225
Author: Bill Todd [TeamB]
Date: Sat, 14 Jun 2008 17:18
9 lines
1219 bytes
Isaac wrote:

> one last question if I have union selects can I add one order by like
> this order by 1

Use a single ORDER BY at the end.

-- 
Bill Todd (TeamB)
Re: need advice about sql for data mining
#228
Author: Isaac
Date: Sat, 14 Jun 2008 21:26
12 lines
1305 bytes
Bill Todd [TeamB] wrote:
> Isaac wrote:
>
>   
>> one last question if I have union selects can I add one order by like
>> this order by 1
>>     
>
> Use a single ORDER BY at the end.
>
>   
ok sorry stupid question <g>
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads