............................................... .............................
即如果是下表:
Name: MyTable1
CusID CusNume RegDate DocNr1 DC Amnt Cur Ddate Fexc ExcDif LY_TotA TY_TotA LY_ExcDif TY_ExcDif
1001 A Cust 05.01.2016 2000221 D 750,00 EUR 31.12.2018 V 50,00 750,00 0,00 50,00 0,00
1001 A Cust 06.01.2017 2000287 D 1.200,00 EUR 31.12.2018 V 70,00 1.200,00 0,00 70,00 0,00
1001 A Cust 21.05.2017 2000425 D 3.000,00 EUR 31.12.2018 V 320,00 3.000,00 0,00 320,00 0,00
1001 A Cust 31.05.2018 2000476 D 2.100,00 EUR 31.12.2018 V 35,00 0,00 2.100,00 0,00 35,00
1001 A Cust 10.09.2018 2000517 D 500,00 USD 31.12.2018 V 14,00 0,00 500,00 0,00 14,00
1002 B Cust 31.03.2018 2000309 D 3.200,00 EUR 31.12.2018 V 49,00 0,00 3.200,00 0,00 49,00
1002 B Cust 01.04.2018 2000401 D 1.900,00 USD 31.12.2018 V 32,00 0,00 1.900,00 0,00 32,00
Name: MyTable2
CusID CusNume RegDate Edate DocNr1 DocNr2 DC Dtype Dyear Amnt Cur Ddate Fexc ExcDif
1001 A Cust 06.01.2016 20.01.2018 2000141 1002213 D AD 2016 2.000,00 EUR 31.12.2016 C 100,00
1001 A Cust 06.01.2016 20.01.2018 2000141 1002213 D AD 2017 2.000,00 EUR 31.12.2017 C 120,00
1001 A Cust 20.01.2018 20.01.2018 1002213 1002213 C BD 2.000,00 EUR 0,00
1001 A Cust 21.05.2017 31.05.2018 2000320 1002315 D AD 2017 3.000,00 EUR 31.12.2017 C 300,00
1001 A Cust 21.03.2018 31.05.2018 2000381 1002315 D AD 1.500,00 EUR 0,00
1001 A Cust 31.05.2018 31.05.2018 1002315 1002315 C BD 4.500,00 EUR 0,00
1001 A Cust 10.02.2018 29.06.2018 2000521 1002716 D AD 500,00 EUR 0,00
1001 A Cust 29.06.2018 29.06.2018 1002716 1002716 C BD 500,00 EUR 0,00
1002 B Cust 03.11.2018 31.07.2018 2000777 1002819 D AD 4.300,00 EUR 0,00
1002 B Cust 03.12.2018 31.07.2018 1002819 1002819 C BD 4.300,00 EUR 0,00
1002 B Cust 31.03.2019 28.08.2016 2000875 1002934 D AD 2016 3.200,00 USD 31.12.2017 C 90,00
1002 B Cust 31.03.2019 28.08.2016 2000875 1002934 D AD 2017 3.200,00 USD 31.12.2017 C 115,00
1002 B Cust 01.04.2019 28.08.2018 1002934 1002934 C BD 3.200,00 USD 0,00
Name: MyTable3
DocNr CusID MAcc DC AmntNC Cur
1002213 1001 12001001 D 7856,00 EUR
1002213 1001 12001001 C 7736,00 EUR
1002213 60190001 C 250,00 EUR
1002315 1001 12001001 D 8204,00 EUR
1002315 1001 12001001 C 8104,00 EUR
1002315 60190001 C 435,00 EUR
1002716 1001 12001001 D 1025,00 EUR
1002716 1001 12001001 C 1005,00 EUR
1002716 60190001 C 20,00 EUR
1002819 1002 12001001 D 11150,00 EUR
1002819 1002 12001001 C 10800,00 EUR
1002819 60190001 C 350,00 EUR
1002934 1002 12001001 D 9400,00 EUR
1002934 1002 12001001 C 9180,00 EUR
1002934 60190001 C 220,00 EUR
我通过以下sql获得此结果。
Result:
CusID CusNume Cur LY_ExcDif TY_ExcDif LY_EvExcDif
1001 A Cust EUR 440,00 35,00 520,00
1002 B Cust EUR 0,00 49,00 0,00
1002 B Cust USD 0,00 32,00 205,00
"SELECT DISTINCT tb1.CusID, tb1.CusNume, tb1.Cur, SUM(tb1.LY_ExcDif) As LY_ExcDif, " & _
"SUM(tb1.TY_ExcDif) As TY_ExcDif, SUM(tb2.ExcDif) As LY_EvExcDif " & _
"FROM ( " & _
"(SELECT CusID, CusNume, Cur, " & _
"SUM(IIF(LY_ExcDif IS NULL, 0, IIF(DC = 'D', LY_ExcDif, -LY_ExcDif))) As LY_ExcDif, " & _
"SUM(IIF(TY_ExcDif IS NULL, 0, IIF(DC = 'D', TY_ExcDif, -TY_ExcDif))) As TY_ExcDif " & _
"FROM [MyTable1$] GROUP BY CusID, CusNume, Cur) tb1 " & _
"INNER JOIN ( " & _
"SELECT CusID, CusNume, Cur, " & _
"SUM(IIF(Fexc = 'C', IIF(ExcDif IS NULL, 0, IIF(DC = 'D', ExcDif, -ExcDif)))) As ExcDif " & _
"FROM [MyTable2$] " & _
"GROUP BY CusID, CusNume, Cur) tb2 " & _
"ON tb1.CusID = tb2.CusID And tb1.Cur = tb2.Cur) " & _
"GROUP BY tb1.CusID, tb1.CusNume, tb1.Cur; "
我想要这样的东西
"SELECT DISTINCT tb2.CusID, tb2.CusNume, tb3.Cur, Sum(tb3.LY_EvExcDif) As LY_EvExcDif, " & _
"Sum(tb3.TY_EvExcDif) As TY_EvExcDif, Sum(tb3.EvExcDif) As EvExcDif " & _
"FROM [MyTable2$] tb2 " & _
"INNER JOIN " & _
"(SELECT DISTINCT DocNr, Cur, " & _
"SUM(...............) As LY_EvExcDif, " & _
"SUM(...............) As TY_EvExcDif, " & _
"FROM [MyTable3$] " & _
"WHERE MAcc LIKE '601%' " & _
"GROUP BY DocNr, Cur) tb3 " & _
"ON tb2.DocNr2 = tb3.DocNr " & _
"GROUP BY tb2.CusID, tb2.CusNume, tb3.Cur "
1.Sum
SUM(IIF(DISTINCT tb2.Fexc = 'C', IIF(tb2.Amnt IS NULL, 0, IIF(tb2.DC = 'D', tb2.Amnt, -tb2.Amnt)))) /
SUM(IIF(tb2.Amnt IS NULL, 0, IIF(tb2.DC = 'D', tb2.Amnt, -tb2.Amnt))) *
SUM(IIF(AmntNC IS NULL, 0, IIF(DC = 'D', AmntNC, -AmntNC))))
2.Sum
SUM(IIF(DISTINCT tb2.Fexc IS NULL, IIF(tb2.Amnt IS NULL, 0, IIF(tb2.DC = 'D', tb2.Amnt, -tb2.Amnt)))) /
SUM(IIF(tb2.Amnt IS NULL, 0, IIF(tb2.DC = 'D', tb2.Amnt, -tb2.Amnt))) *
SUM(IIF(AmntNC IS NULL, 0, IIF(DC = 'D', AmntNC, -AmntNC))))
以这种方式编辑SQL之后,我想将其与第一个SQL合并并获得以下结果。 或另一个获得以下结果的sql:)
Result:
CusID CusNume Cur LY_ExcDif TY_ExcDif LY_EvExcDif TY_EvExcDif
1001 A Cust EUR 440,00 35,00 40,00 145,00
1002 B Cust EUR 0,00 49,00 0,00 0,00
1002 B Cust USD 0,00 32,00 205,00 0,00