SUMIFS针对公式中的另一个日期段评估BEGIN和END日期

时间:2018-12-17 03:31:36

标签: excel excel-formula

数据集如下:

工资汇总表(A)

enter image description here

来源工资表(B)

我已经在SQL Server中完成了逻辑,但是我没有运气将其转换为Excel。

要从SQL转换为Excel的逻辑是

A.SALARY是表B中的行的总和

  1. A.EMPLID = B.EMPLID
  2. A.EMPL_RCD = B.EMPL_RCD
  3. B.SLICE_BEGIN在A.START_DATE和A.END_DATE之间 B.SLICE_END在A.START_DATE和A.END_DATE之间

我已经做了SUMIFS的尝试,但是它不能满足上面(3)中概述的“ OR”条件。

只是想知道这里的解决方案是什么,是否有人可以指出我正确的方向。

数据:

+---------+----------+-------------+------------+----------+
| EMPLID  | EMPL_RCD | SLICE_BEGIN | SLICE_END  |  SALARY  |
+---------+----------+-------------+------------+----------+
| 1000280 |        0 | 28/12/2017  | 10/01/2018 | 2,634.03 |
| 1000280 |        0 | 11/01/2018  | 24/01/2018 | 2,634.02 |
| 1000280 |        0 | 25/01/2018  | 07/02/2018 | 2,634.05 |
| 1000280 |        0 | 08/02/2018  | 21/02/2018 | 2,634.02 |
| 1000280 |        0 | 22/02/2018  | 07/03/2018 | 2,634.03 |
| 1000280 |        0 | 08/03/2018  | 21/03/2018 | 2,634.02 |
| 1000280 |        0 | 22/03/2018  | 04/04/2018 | 2,634.02 |
| 1000280 |        0 | 05/04/2018  | 18/04/2018 | 2,634.02 |
| 1000280 |        0 | 19/04/2018  | 02/05/2018 | 2,634.02 |
| 1000280 |        0 | 03/05/2018  | 16/05/2018 | 2,634.03 |
| 1000280 |        0 | 17/05/2018  | 30/05/2018 | 2,634.02 |
| 1000280 |        0 | 31/05/2018  | 13/06/2018 | 2,634.02 |
| 1000280 |        0 | 14/06/2018  | 27/06/2018 | 2,634.02 |
| 1000280 |        0 | 02/07/2018  | 11/07/2018 | 2,590.56 |
| 1000280 |        0 | 12/07/2018  | 25/07/2018 | 2,631.30 |
| 1000280 |        0 | 26/07/2018  | 08/08/2018 | 2,631.30 |
| 1000280 |        0 | 09/08/2018  | 22/08/2018 | 2,631.30 |
| 1000280 |        0 | 23/08/2018  | 05/09/2018 | 2,631.30 |
| 1000280 |        0 | 06/09/2018  | 19/09/2018 | 2,631.30 |
| 1000280 |        0 | 20/09/2018  | 03/10/2018 | 2,631.30 |
| 1000280 |        0 | 04/10/2018  | 17/10/2018 | 2,631.30 |
| 1000280 |        0 | 18/10/2018  | 31/10/2018 | 2,631.30 |
| 1000280 |        0 | 01/11/2018  | 14/11/2018 | 2,631.30 |
| 1000280 |        0 | 15/11/2018  | 28/11/2018 | 2,631.30 |
| 1000280 |        0 | 01/12/2018  | 12/12/2018 | 2,675.62 |
| 1000280 |        0 | 13/12/2018  | 26/12/2018 | 2,686.70 |
+---------+----------+-------------+------------+----------+

3 个答案:

答案 0 :(得分:3)

您可以这样使用3个SUMIFS:

=SUMIFS(L$5:L$30,H$5:H$30,A5,I$5:I$30,B5,J$5:J$30,">="&C5,J$5:J$30,"<="&D5)+
SUMIFS(L$5:L$30,H$5:H$30,A5,I$5:I$30,B5,K$5:K$30,">="&C5,K$5:K$30,"<="&D5)-
SUMIFS(L$5:L$30,H$5:H$30,A5,I$5:I$30,B5,J$5:J$30,">="&C5,J$5:J$30,"<="&D5,K$5:K$30,">="&C5,K$5:K$30,"<="&D5)

enter image description here

答案 1 :(得分:3)

对此进行更复杂的处理,以供个人娱乐。但是,最简单是最好的; @Tom Sharpeanswer更加直观。

 =SUMPRODUCT($L$5:$L$30*($H$5:$H$30=A5)*($I$5:$I$30=B5)*
 ABS((($J$5:$J$30>=C5)*($J$5:$J$30<=D5))+(($K$5:$K$30>=C5)*($K$5:$K$30<=D5))
 *(($J$5:$J$30>=C5)*($J$5:$J$30<=D5))-(($K$5:$K$30>=C5)*($K$5:$K$30<=D5))))

enter image description here

答案 2 :(得分:1)

您可以尝试使用VBA代码:

Option Explicit

 Sub Test()

    Dim LastRow As Long, i As Long, j As Long
    Dim DateStart As Date, DateFinish As Date
    Dim Total As Double

    With ThisWorkbook.Worksheets("Sheet1")
        LastRow = .Cells(.Rows.Count, "H").End(xlUp).Row
            For j = 3 To 5

                DateStart = .Range("C" & j).Value - 1
                DateFinish = .Range("D" & j).Value + 1
                Total = 0

                For i = 5 To LastRow
                    If j = 3 And i = 5 Then
                        If .Range("J" & i).Value < DateStart And .Range("K" & i).Value < DateFinish Then
                            Total = Total + .Range("L" & i).Value
                        End If
                    Else
                        If .Range("J" & i).Value > DateStart And .Range("K" & i).Value < DateFinish Then
                            Total = Total + .Range("L" & i).Value
                        End If
                    End If

                Next i

                Range("E" & j).Value = Total

            Next j
    End With

 End Sub