Visual FoxPro查询添加总计

时间:2011-10-25 09:59:42

标签: visual-foxpro

我有一个简单的简单查询,其中只有SUMS总共有1行或更多行。基于独特的参考。

我想要做的是在jobRef分组的jobcharge.nAccrInv的结果中添加一个总列

Job_Ref / Name / Sales / Total

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'

谢谢罗斯

1 个答案:

答案 0 :(得分:0)

(放入评论栏太多了)......稍后会回答。

你的nJobID = 3524的目的是什么,你只对一份工作感兴趣吗?如果是这样,无论从JOB的WHERE子句返回的所有作业如何,您的查询都将仅为作业3525提供总计。

我会处理查询,但你不想要DISTINCT。

此外,您只有jobCharge.nArrcInv,.nCostInv,.nSaleInv,但稍后您使用SUM(jobcharge.nAccrInv)...您的INTENT是否获得应计成本,实际成本之和,总和每个职位的销售说明???包含其他职位标题内容?

看起来您希望每种类型的工作活动都有单独的子组总数加上整个工作中所有销售的总计...也许你想要的就是这个......

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”子句。