SQL如何加入和汇总

时间:2018-06-05 22:06:44

标签: sql sql-server

[编辑提供代码和解决方案的进展]

我有一些由SSIS工作构建的表,它从会计系统复制发票和从计费系统复制时间条目。由于我们是一家服务公司,我们按照四种简单的费率收费。手头的任务是构建一个查询,该查询将按费率开票的小时数与按时间段计算的结算系统小时数相匹配。

我通过SSIS作业派生了一个列,用于标识下一个星期一相对于时间条目记录的列。当我们在星期一发票时,我现在可以将给定周内的活动与下周创建的发票联系起来。我刚刚遇到的曲线球是会计想要在不同的非周期时段发票。工作仍然每周都会发生,但我不能在发票日期和派生的星期一之间进行简单的连接。

历史上,我会调整发票行和时间条目以按汇总进行汇总。我正在避免这种情况,因为它可能代表偏离答案的预设。这是两个表的简化视图

发票行

+----------+----------+-----------+-------+-------+
|Inv. Date | Inv. Num | Qty Hours | Rate  | Job ID|
+----------+----------+-----------+-------+-------+
| 5-7-18   | 12345    |  10       | Draft |  1    |
| 5-7-18   | 12345    |  5        | Eng   |  1    |
| 5-7-18   | 12345    |  20       | Design|  1    |
| 5-7-18   | 12345    |  2        | Staff |  1    |
| 5-14-18  | 12370    |  90       | Draft |  1    |
| 5-14-18  | 12370    |  10       | Eng   |  1    |
| 5-14-18  | 12370    |  8        | Design|  1    |
| 5-14-18  | 12370    |  1        | Staff |  1    |
| 5-28-18  | 12390    |  20       | Draft |  1    |
| 5-28-18  | 12390    |  10       | Eng   |  1    |
| 5-28-18  | 12390    |  80       | Design|  1    |
| 5-28-18  | 12390    |  1        | Staff |  1    |
+----------+----------+-----------+-------+-------+

时间条目

+----------+----------+-----------+-------+-------+
|Date Enter| Next Mon | Qty Hours | Rate  | Job ID|
+----------+----------+-----------+-------+-------+
| 5-1-18   | 5-7-18   |  10       | Draft |  1    |
| 5-3-18   | 5-7-18   |  5        | Eng   |  1    |
| 5-3-18   | 5-7-18   |  10       | Design|  1    |
| 5-4-18   | 5-7-18   |  10       | Design|  1    |
| 5-4-18   | 5-7-18   |  2        | Staff |  1    |
| 5-7-18   | 5-14-18  |  20       | Draft |  1    |
| 5-8-18   | 5-14-18  |  20       | Draft |  1    |
| 5-9-18   | 5-14-18  |  20       | Draft |  1    |
| 5-11-18  | 5-14-18  |  30       | Draft |  1    |
| 5-10-18  | 5-14-18  |  10       | Eng   |  1    |
| 5-11-18  | 5-14-18  |  8        | Design|  1    |
| 5-12-18  | 5-14-18  |  1        | Staff |  1    |
| 5-15-18  | 5-21-18  |  10       | Draft |  1    |
| 5-16-18  | 5-21-18  |  5        | Eng   |  1    |
| 5-17-18  | 5-21-18  |  40       | Design|  1    |
| 5-18-18  | 5-21-18  |  .5       | Staff |  1    |
| 5-21-18  | 5-28-18  |  10       | Draft |  1    |
| 5-22-18  | 5-28-18  |  5        | Eng   |  1    |
| 5-23-18  | 5-28-18  |  40       | Design|  1    |
| 5-25-18  | 5-28-18  |  .5       | Staff |  1    |
+----------+----------+-----------+-------+-------+

我正在寻找的输出如下:

+-------+------+--------+------+------+--------+-------+-----+-----+-------+
|InvDate|InvNum|InvDraft|InvEng|InvDes|InvStaff|TeDraft|TeEng|TeDes|TeStaff|
+-------+------+--------+------+------+--------+-------+-----+-----+-------+
|5-7-18 |12345 | 10     | 5    |  20  | 2      | 10    | 5   |  20 |   2   |
|5-14-18|12370 | 90     | 10   |  8   | 1      | 90    | 10  |  8  |   1   |
|5-28-18|12390 | 20     | 10   |  80  | 1      | 20    | 10  |  80 |   1   |
+-------+------+--------+------+------+--------+-------+-----+-----+-------+

我的诀窍是弄清楚如何从时间输入方面汇总两周的结算。我不知道发票之间会有多少个星期,因此我需要让现有发票的日期驱动它。

到目前为止,这是SQL:

USE FiniteAccounting
GO

WITH
Invoices AS
    (SELECT JrnlHdrs.TransactionDate
            , JrnlRows.InvNumForThisTrx
            , JrnlHdrs.ShipToName
            , JrnlHdrs.CustVendId
            , JrnlRows.JobID
            , EPProjects.Name AS ProjectName
            , EPProjects.EstimatedHours
            , EPProjects.QtHours
            , EPProjects.PONumber AS PurchOrder
            , EPProjects.QTPrice
            , JrnlHdrs.AmountPaid
            , Customer.CustomerRecordNumber
            , JrnlRows.Quantity
            , JrnlRows.Rate
            , InvSum.InvHours
      FROM  JrnlHdrs JrnlHdrs INNER JOIN
            JrnlRows JrnlRows ON JrnlRows.PostOrder = JrnlHdrs.PostOrder INNER JOIN
            EPProjects EPProjects ON EPProjects.ProjNumber = JrnlRows.JobID INNER JOIN
            Customer Customer ON Customer.CustomerRecordNumber = JrnlHdrs.CustVendId INNER JOIN
            (SELECT JrnlRows.InvNumForThisTrx, sum(jrnlRows.Quantity) AS InvHours 
                FROM JrnlRows
                Group By JrnlRows.InvNumForThisTrx) As InvSum on InvSum.InvNumForThisTrx = JrnlRows.InvNumForThisTrx
      Where JrnlRows.Quantity > 0 AND EPProjects.ProjectStatusID = 3),

InvoicePVT AS
    (SELECT ROW_NUMBER()
            OVER(PARTITION BY JobId ORDER BY TransactionDate ASC) AS RowID
            , TransactionDate
            , InvNumForThisTrx
            , ShipToName
            , CustVendId
            , JobID
            , ProjectName
            , IsNull([StaffEng], 0) AS StaffEng
            , IsNull(Engineer, 0) AS Engineer
            , IsNull(Designer, 0) AS Designer
            , IsNull(Draftsman, 0) AS Draftsman
            , InvHours
            , EstimatedHours
            , QtHours
            , PurchOrder AS PONumber
            , IsNull(QTPrice, 0) AS QTPrice
            , AmountPaid
            , CustomerRecordNumber
    FROM    Invoices PIVOT (SUM(Quantity) FOR Rate IN ([Draftsman], [Designer], [Engineer], [StaffEng])) AS RatesPerRow)

Select      i1.TransactionDate as LastInvDate 
            , i2.TransactionDate
            , i2.InvNumForThisTrx
            --, i2.ShipToName
            --, i2.CustVendId
            , i2.JobID
            --, i2.ProjectName
            , IsNull(i2.StaffEng, 0) AS StaffEng
            , IsNull(i2.Engineer, 0) AS Engineer
            , IsNull(i2.Designer, 0) AS Designer
            , IsNull(i2.Draftsman, 0) AS Draftsman
            , i2.InvHours

From InvoicePVT i1 INNER JOIN InvoicePVT i2 ON i1.RowID = i2.rowid -1
WHERE i1.JobID = i2.JobID;

结果是:

LastInvDate TransactionDate InvNumForThisTrx    JobID   StaffEng    Engineer    Designer    Draftsman   InvHours
2017-05-22  2017-05-29  11216   16-2256 0   0   29.75   0   30.25
2017-05-29  2017-06-05  11232   16-2256 0   0.25    34  0   34.25
2017-06-05  2017-06-12  11250   16-2256 0   0   41.75   0   41.75
2017-06-12  2017-06-19  11267   16-2256 0   0   17.5    0   17.5
2017-06-19  2017-06-26  11283   16-2256 0   1   9   0   10
2017-06-26  2017-07-10  11304   16-2256 0   0   1.25    0   1.25
2017-07-10  2017-11-06  11698   16-2256 0   1.25    1.25    0   2.5
2017-11-06  2018-04-09  12184   16-2256 0   0.75    33  0   33.75
2018-04-09  2018-04-23  12212   16-2256 0   0   12.25   0   12.25
2018-04-23  2018-05-14  12263   16-2256 0   0.5 5   0   5.5
2018-04-09  2018-04-23  12186   18-4080 0   1.25    1   8.5 10.75
2018-04-23  2018-04-30  12214   18-4080 0   0.5 0   7.5 8
2018-04-30  2018-05-14  12234   18-4080 0   1.75    1.25    56.5    59.5
2018-05-14  2018-05-28  12266   18-4080 0   0.5 1.25    34  35.75
2018-05-28  2018-05-28  12293   18-4080 0   1   0   44.75   45.75
2018-05-28  2018-05-28  12294   18-4080 0   0   0   17.25   17.25
2018-05-28  2018-05-28  12295   18-4080 0   0   0   9.75    9.75
2018-05-28  2018-05-28  12296   18-4080 0   0   0   5.75    5.75
2018-05-14  2018-05-28  12265   18-4088 0   0   0   0   10
2018-05-14  2018-05-28  12281   18-4089 0   0.75    18.5    0   19.25
2018-05-14  2018-05-28  12278   18-4102 0   0.75    15  0   15.75

提前致谢! 佩里

1 个答案:

答案 0 :(得分:0)

首先从不同的" Next Mon"创建一个表。柱。让它命名为" BillingMondays"。
然后创建一个附加列,用于存储" BillingMondays"的行号。让我们称之为" BillingMondayNo"。
现在加入" BillingMondays" BillingMondayNo = BillingMondayNo - 1.这将BillingMondays的记录连接到之前的记录,然后您可以计算账单的每周距离。
我认为这可以解决你的问题