SQL的运行余额和runnig总计

时间:2019-06-11 14:58:48

标签: sql excel vba

I have the following excel sheet table [Data]:

 endPreyear = '31.12.2018'

  ID  cusID   Cus_Name    Reg_Date    DC  MyAmount    Exch    MyCur
   1   1001    A Cust      06.01.2018  D   2.000.00    180.00  EUR
   2   1001    A Cust      20.01.2018  C   1.900.00    160.00  EUR
   3   1001    A Cust      17.02.2018  D   1.200.00    100.00  EUR
   4   1001    A Cust      12.04.2018  D   1.300.00    110.00  EUR
   5   1001    A Cust      21.05.2018  D   1.400.00    120.00  EUR
   6   1001    A Cust      21.05.2019  D   3.000.00    450.00  EUR
   7   1001    A Cust      31.05.2019  C   5.000.00    300.00  EUR
   8   1001    A Cust      10.09.2019  D     500.00     10.00  EUR
   9   1001    A Cust      11.09.2019  C     400.00      5.00  EUR
   10  1002    B Cust      03.11.2018  D   4.300.00    450.00  EUR
   11  1002    B Cust      03.12.2018  C   3.900.00    405.00  EUR
   12  1002    B Cust      07.01.2019  D   9.000.00    740.00  EUR
   13  1002    B Cust      31.01.2019  C   4.000.00    200.00  EUR
   14  1002    B Cust      20.03.2019  D   5.200.00    140.00  EUR
   15  1002    B Cust      28.03.2019  C   4.200.00    120.00  EUR
   16  1002    B Cust      29.03.2019  D   1.805.00    120.00  USD
   17  1002    B Cust      30.03.2019  C   1.805.00    105.00  USD
   18  1002    B Cust      31.03.2019  D   3.204.00    210.00  USD
   19  1002    B Cust      01.04.2019  C   1.922.40    90.00   USD

OutPut:MyTable和4列:

LYear_Tot   TYear_Tot   LYear_ExcTot    TYear_ExcTot
2.000.00    0           180.00          0
  100.00    0            20.00          0
1.300.00    0           120.00          0
2.600.00    0           230.00          0
4.000.00    0           350.00          0
4.000.00    3.000.00    350.00          450.00
    0.00    2.000.00    110.00          390.00
    0.00    2.500.00    110.00          400.00
    0.00    2.100.00    110.00          395.00
4.300.00        0.00    450.00          0
  400.00        0.00     45.00          0
  400.00    9.000.00     45.00          740.00
    0.00    5.400.00     25.00          560.00
    0.00   10.600.00     25.00          700.00
    0.00    6.400.00     25.00          580.00
    0.00    1.805.00      0.00          120.00
    0.00        0.00      0.00           15.00
    0.00    3.204.00      0.00          225.00
    0.00    1.281.60      0.00          135.00

计算方法:

LYear_Tot  Explanation                                         TYear_Tot   
2.000.00   ID.1                                                0           
  100.00   pre.row + (-ID.2)                                   0           
1.300.00   pre.row + ID.3                                      0           
2.600.00   pre.row + ID.4                                      0
4.000.00   pre.row + ID.5                                      0           
4.000.00   pre.row (DC='D'And Year(Reg_Date)>Year(endPreyear)) 3.000.00 ID.6 
    0.00  (Because pre.row + (-ID.7)<0) dif.added to TYear_Tot 2.000.00               
    0.00                                    pre.row + ID.8     2.500.00    
    0.00                                    pre.row + (-ID.9)  2.100.00
4.300.00  ID.10 (Customer changed)                                 0.00      
.                                                              .
.                                                              .
Each customer and currency total will be reset

if DC='D' And Year(Reg_Date)<=Year(endPreyear) Then LYear_Tot = LYear_Tot + MyAmount

if DC='C' And Year(Reg_Date)<=Year(endPreyear) Then LYear_Tot = LYear_Tot + (-MyAmount)

if DC='C' And Year(Reg_Date)>Year(endPreyear) AND LYear_Tot + (-MyAmount)>0 Then LYear_Tot = LYear_Tot + (-MyAmount)
Elseif DC='C' And Year(Reg_Date)>Year(endPreyear) AND LYear_Tot + (-MyAmount) <=0 Then LYear_Tot = 0    'difference will be added to TYear_Tot field 

if DC='D' And Year(Reg_Date)>Year(endPreyear) Then TYear_Tot = TYear_Tot + MyAmount

if DC='C' And Year(Reg_Date)>Year(endPreyear) AND LYear_Tot <0 Then TYear_Tot = TYear_Tot + LYear_Tot
Elseif DC='C' And Year(Reg_Date)>Year(endPreyear) AND LYear_Tot >=0 Then TYear_Tot = TYear_Tot + (-MyAmount)

I hope I can explain you well.

How to write query in SQL for the above results? 
Thanks.   

LYear_Tot = LYear_Tot + IIf(DC =“ D”,MyAmount,-MyAmount) WHERE((Reg_Date <= endPreyear AND DC =“ D”)或DC =“ C”)AND a.cusID = b.cusID AND a.MyCur = b.MyCur

TYear_Tot = TYear_Tot + MyAmount 在哪里(Reg_Date> endPreyear AND DC =“ D”)AND a.cusID = b.cusID AND a.MyCur = b.MyCur +

TYear_Tot = TYear_Tot + MyAmount 在哪里(Reg_Date> endPreyear AND DC =“ C”)AND a.cusID = b.cusID AND a.MyCur = b.MyCur AND LYear_Tot = 0 +

TYear_Tot = TYear_Tot + LYear_Tot 在哪里(Reg_Date> endPreyear AND DC =“ C”)AND a.cusID = b.cusID AND a.MyCur = b.MyCur AND LYear_Tot <0。

0 个答案:

没有答案