2011-10-25 80 views
0

我有以下簡單的查詢,它只是SUMS總數爲1或更多的行。基於獨特的參考。Visual FoxPro查詢添加總計

我想要做的是一個總的列添加到jobcharge.nAccrInv由JobRef

Job_Ref /姓名/銷售/總

123/VAT/10.0分組的結果

123/DUTY/10.0

123/GHC/10.0/30.0

SELECT DISTINCT;

Job.cJobRef AS JobRef,; 
Job.cName AS Customer_Name, ; 
Job.cJobType AS JobType, ; 
Job.cJobMode AS JobMode, ; 
Job.cOrigin AS Org,; 
Job.cDestination AS Dest,; 
Job.cOwner AS Owner,; 
jobcharge.cInvoiceDescr as [Invoice_Desc], ; 
jobcharge.nAccrInv as [Accrued_Costs], ; 
jobcharge.nCostInv as [Actual_Costs], ; 
jobcharge.nSaleInv as [Sales], ; 
(SELECT SUM(jobcharge.nAccrInv) AS SalesTotals FROM jobcharge WHERE NJOBID =3524); FROM job;INNER JOIN jobcharge ON job.nJob_Id = jobcharge.nJobId; WHERE job.cJobRef= "RSJC00001" AND job.cOwner = 'DBQ' 

感謝羅斯

回答

0

(太多投入註釋字段)......將在稍後回答。

你的nJobID = 3524的目的是什麼,你只對一份工作感興趣嗎?如果是這樣,無論從JOB的WHERE子句返回的所有作業是什麼,您的查詢都會爲您提供僅用於作業3525的總數。

我將處理查詢,但您不希望DISTINCT。

此外,您只有jobCharge.nArrcInv,.nCostInv,.nSaleInv,但稍後使用SUM(jobcharge.nAccrInv)...您的意圖是獲得應計成本,實際成本總和,總和每個職位描述的銷售額?包括其他工作標題內容?

它看起來像你想要的每類作業活動PLUS總計整個作業的所有銷售的兩個獨立的子組總計......也許你想,這是什麼...

SELECT 
     Job.cJobRef AS JobRef,; 
     Job.cName AS Customer_Name, ; 
     Job.cJobType AS JobType, ; 
     Job.cJobMode AS JobMode, ; 
     Job.cOrigin AS Org,; 
     Job.cDestination AS Dest,; 
     Job.cOwner AS Owner,; 
     JCSubTotals.Invoice_Desc,; 
     JCSubTotals.Accrued_PerDesc,; 
     JCSubTotals.Actual_PerDesc,; 
     JCSubTotals.Sales_PerDesc,; 
     JCFinalTotals.Total_Accrued,; 
     JCFinalTotals.Total_Actual,; 
     JCFinalTotals.Total_Sales; 
    from ; 
     (select jc.nJobID,; 
       jc.cInvoiceDescr as Invoice_Desc, ; 
       sum(jc.nAccrInv) as Accrued_PerDesc, ; 
       sum(jc.nCostInv) as Actual_PerDesc, ; 
       sum(jc.nSaleInv) as Sales_PerDesc ; 
      from ; 
       JobCharge jc; 
      where ; 
       jc.nJobID = 3524 ; 
      group by ; 
       jc.nJobID,; 
       jc.cInvoiceDescr) JCSubtotals ; 
     JOIN ; 
     (select jc.nJobID,; 
        sum(jc.nAccrInv) as Total_Accrued, ; 
        sum(jc.nCostInv) as Total_Actual, ; 
        sum(jc.nSaleInv) as Total_Sales ; 
       from ; 
        JobCharge jc; 
       where ; 
        jc.nJobID = 3524 ; 
       group by ; 
        jc.nJobID) JCFinalTotals ; 
      ON JCSubtotals.nJobID = JCFinalTotals.nJobID ; 
     JOIN Job ; 
     on JCSubtotals.nJobID = Job.nID; 

實際上,我已經爲您提供了更多的專欄,每行顯示其總數,每個職位描述的總額,以及與整個工作相比(總計,實際和銷售額)的TOTALS。你總是可以忽略你不關心的列,但是這會給你一個我認爲你想要的東西的方法。另外,我假設(因爲沒有提供)的最後一次連接到「JOB」表,其主鍵列只是「nID」而不是「nJob_ID」(作爲jobCharge表的外鍵)。

如果您希望這些結果跨越整個作業集,我實際上只是從JobCharge查詢中刪除相應的「WHERE」子句。

+0

@Ross,修改後的答案包括來自感知的想要的結果的查詢 – DRapp