Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
SQL select to sum from two tables?
Message
De
04/06/2009 17:38:15
 
 
Information générale
Forum:
Visual FoxPro
Catégorie:
Base de données, Tables, Vues, Index et syntaxe SQL
Divers
Thread ID:
01403521
Message ID:
01403824
Vues:
36
Ok, I get what you are saying about Projections vs Derived Tables, which I hope is the right terminology, since I'm just repeating what you said.

I've built the SQL code both ways, and I did some testing on each way that I want to report. I'm hoping some others will jump in and critique both ways to say which one they think is better.

First, I want to show both ways to prove that I did it (it was a great excercise and really helped me think about SQL a lot more) and then I'll tell you the results after you reply.

First way (Projections)
Select job_item.job_num,
       job_info.job_start as date,
       job_info.p_o_num as po_num,
       job_item.qty,
       job_item.part_no,
       nvl(parts.altpartno,'') as Alt_Part_No,
       job_item.dwg_no,
       nvl(parts.desc, job_item.desc) as descrip,
       job_item.price,
       job_item.qty*job_item.price as total,
       job_info.quote_no,
       job_info.est_cost,
        (Select Cast(Sum(mat_qty*mat_cost*job_info.mat_mkup) as N(10,2)) From Material Where mat_code=Job_Info.job_num) as Material,
        (Select Cast(Sum(hrs_worked*mach_rate) as N(10,2)) From Actijobs Where job_num=Job_Info.job_num) as Labor,
        (Select Cast(Sum(hrs_worked) as N(10,2)) From Actijobs Where job_num=Job_Info.job_num) as Hours,
       (Select cast(NVL(LaborTotal,0) + nvl(MaterialTotal,0) as N(10,2)) 
         From (Select job_num as lbrkey, sum(hrs_worked*mach_rate) as LaborTotal from Actijobs  group by 1) Labor 
           Full Join (Select mat_code as mtlkey, sum(mat_qty*mat_cost*Job_Info.mat_mkup) as MaterialTotal from Material group by 1) Material on Labor.lbrkey = Material.mtlkey
         Where Labor.lbrkey=Job_Info.job_num) as TotalCost,
       job_info.job_invnum as Inv_Num,
        (Select Sum(amount) From Job_Invoice Where job_num=Job_Info.job_num) as Inv_Amt,
       job_info.cust_num,
       space(6) as Vendor
       From job_item
             left outer join Job_Info on job_item.job_num=job_info.job_num
             left outer join Parts on job_item.ipartkey=parts.ipkey
Second way (Derived Tables)
Select job_item.job_num,
       job_info.job_start as date,
       job_info.p_o_num as po_num,
       job_item.qty,
       job_item.part_no,
       nvl(parts.altpartno,'') as Alt_Part_No,
       job_item.dwg_no,
       nvl(parts.desc, job_item.desc) as descrip,
       job_item.price,
       job_item.qty*job_item.price as total,
       job_info.quote_no,
       job_info.est_cost,
       Mat.mtl_cost,
       Lbr.lbr_cost,
       Hrs.job_hrs,
       Nvl(Mat.mtl_cost,0)+Nvl(Lbr.lbr_cost,0)  as TotalCost,
       job_info.job_invnum as Inv_Num,
       Job_Invoice.amount,
       job_info.cust_num,
       space(6) as Vendor
       From job_item
             left outer join Job_Info on job_item.job_num=job_info.job_num
             left outer join Parts on job_item.ipartkey=parts.ipkey
             left join (Select mat_code, Cast(Sum(mat_qty*mat_cost*job_info.mat_mkup) as N(10,2)) as mtl_cost From Material group by 1) Mat on Job_info.Job_Num = Mat.mat_Code
             left join (Select job_num, Cast(Sum(hrs_worked*mach_rate) as N(10,2)) as lbr_cost From Actijobs group by 1) Lbr on  Job_Info.job_num=Lbr.job_num
             left join (Select job_num, Cast(Sum(hrs_worked) as N(10,2)) as job_hrs From Actijobs group by 1) Hrs on Job_Info.job_num=Hrs.job_num
             left join (Select job_num, Sum(amount) as amount From Job_Invoice group by 1) Job_invoice on Job_Info.job_num=Job_Invoice.job_num
So, which one do you think is fastest?

To me, the second one sure seems a lot easier to read and follow.

.
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform