Article View: borland.public.interbase.sql
Article #150Re: need advice about sql for data mining
From: Maxim Shiryaev
Date: Tue, 10 Jun 2008 07:36
Date: Tue, 10 Jun 2008 07:36
82 lines
3324 bytes
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
Technical Details
Message-ID:
<484e2f00$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!feeder1-2.proxad.net!proxad.net!feeder1-1.proxad.net!ecngs!feeder2.ecngs.de!194.25.134.126.MISMATCH!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<484db5e2@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 114a0e,b4be59b23378bfae X-Google-Attributes: gid114a0e,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit From: "Maxim Shiryaev" <MaximShiryaev@gmail.com> Newsgroups: borland.public.interbase.sql References: <484db5e2@newsgroups.borland.com> Subject: Re: need advice about sql for data mining Date: Tue, 10 Jun 2008 11:36:31 +0400 Lines: 84 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1106 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106 NNTP-Posting-Host: 213.234.197.62 Message-ID: <484e2f00$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1213083392 213.234.197.62 (10 Jun 2008 00:36:32 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!feeder1-2.proxad.net!proxad.net!feeder1-1.proxad.net!ecngs!feeder2.ecngs.de!194.25.134.126.MISMATCH!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail