Visual FoxPro 查询添加总计

发布于 2024-12-12 07:40:51 字数 840 浏览 0 评论 0原文

我有以下简单查询,它只是对 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'

谢谢罗斯

I have the follwoing simple query which just SUMS the total of 1 or more rows. Based on a unique reference.

What I want to do is add a total column to the results of the jobcharge.nAccrInv grouped by JobRef

I.e

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'

Thanks Ross

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

硬不硬你别怂 2024-12-19 07:40:51

(太多了,无法放入评论字段)...稍后会回答。

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

我将处理查询,但您不需要 DISTINCT。

此外,您只有 jobCharge.nArrcInv、.nCostInv、.nSaleInv,但后来您使用 SUM( jobcharge.nAccrInv )...您的意图是获取应计成本总和、实际成本总和、每个工作的销售额总和描述???包括其他作业标题内容吗?

看起来您想要每种工作活动类型的各个子组总数以及整个工作中所有销售额的总计...也许您想要的是这个...

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;

实际上我已经给了您更多的列在每一行上显示每个职位描述的总计,以及与整个职位相比的总计(应计、实际和销售额)。您始终可以忽略您不关心的列,但这为您提供了我认为您想要的内容的方法。

此外,我假设最后一个连接到“JOB”表(因为未提供),其主键列只是“nID”而不是“nJob_ID”(作为 jobCharge 表的外键)。

如果您希望这些结果涵盖整个作业集,我实际上只需从 JobCharge 查询中删除相应的“WHERE”子句即可。

(Too much to put into a comment field)... will answer later.

What is the purpose of your nJobID = 3524, are you only interested in one job? If so, your query will give you the total only for job 3525 regardless of all the jobs returned from the JOB's WHERE clause.

I'll work on the query, but you do not want DISTINCT.

Additionally, you have just the jobCharge.nArrcInv, .nCostInv, .nSaleInv, but later you use SUM( jobcharge.nAccrInv )... Is your INTENT to get a sum of accrued costs, sum of actual costs, sum of sales per job description??? inclusive of other job header content?

It looks like you want both individual sub-group totals per type of job activity PLUS the grand total of all sales across the entire job... maybe what you want is this...

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;

I've actually given you a few more columns to show on an every row, its Totals per job description, AND, the TOTALS compared to the entire job (for accrued, actual and sales). You can always ignore the columns you don't care about, but this gives you a how to of what I think you want.

In addition, the last join to the "JOB" table I am assuming (since not provided), its primary key column is just "nID" instead of "nJob_ID" (as foreign key to the jobCharge table).

If you wanted these results spanning an entire set of jobs, I would actually just remove the respective "WHERE" clauses from the JobCharge queries.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文