简体   繁体   中英

Complex SQL QUERY in MS ACCESS across three tables with sum()

I'm Working on a large financial project, building queries in SQL on MS-ACCESS. These are my tables.

CE
anomes    cod_c     Name       NIP      Nip_Grupo
201706    1         ABC        10       50
201706    1         DDD        12       50
201706    2         CCC        11       50

O
anomes    cod_c    ID_O        Nip_1   val_1     val_2
201706    1        ACA_00      10      500       200
201706    1        ACB_01      12      100       300
201706    2        ACC_07      11      50        400

OS
anomes    cod_c    ID_O        Stage
201706    1        ACA_00      1      
201706    1        ACB_01      2         
201706    2        ACC_07      3      

What I need is a list like this

Name | Sum (val1 + val2) where stage =1 | Sum (val1 + val2) where stage =2 | 

ABC  |  x                               | x
DDD  |  x                               | x
CCC  |  x                               | x

This list should be accomplished by only entering Nip_Grupo (which connects the companies in table CE) And AnoMes which is a time code (yearmonth) reference. Then the second table (O) has operations with intervenients and I'm looking for the Nip_1 to be the same as nip on CE and then link each operation from that company with the stage in OS so that I can sum the total values of operation, per company(CE) from a group, per stage.

It seems pretty straight forward, but I don't always have records on the table OS that link a stage to an entry in table O, at that point, I needed the result to show zero.

This is my Query so far ( a simplified version to fit my example):

SELECT CE.Name, (Sum([O].[val1])+Sum(val2))
FROM CE 
    INNER JOIN O ON (CE.Cod_Contraparte = O.Cod_Contraparte) AND 
    (CE.AnoMes = O.AnoMes) AND (CE.Nip = O.Nip_1Titular)) 
     LEFT JOIN OPERACOES_STAGING_lnk AS OS ON (O.AnoMes = OS.AnoMes) AND 
    (O.ID_Operacao = OS.ID_Operacao)

WHERE (((CE.Nip_Grupo)=[enter nip:]) AND ((CE.anomes)=[enter anomes:]) AND 
      ((CE.Nip)=[O].[Nip_1])) AND ((OS.Stage)=[2])
GROUP BY CE.Nome
ORDER BY CE.Nome

And this query returns only the sum when the stage is 2, and only if I have records on the table OS, as I have many operations that are not connected through the stage I need it to show zero and to print a full list of companies based on the group_id (Nip_Grupo)

Conditional aggregate may help

SELECT CE.name, 
      SUM( IIF( OS.stage=1, O.val1+O.val2,0)) as stage1,
      SUM( IIF( OS.stage=2, O.val1+O.val2,0)) as stage2
FROM CE 
INNER JOIN O ON (CE.Cod_Contraparte = O.Cod_Contraparte) AND (CE.AnoMes = O.AnoMes) AND (CE.Nip = O.Nip_1Titular)) AND ((CE.Nip)=[O].[Nip_1])) 
INNER JOIN OPERACOES_STAGING_lnk AS OS ON (O.AnoMes = OS.AnoMes) AND (O.ID_Operacao = OS.ID_Operacao)
WHERE (((CE.Nip_Grupo)=[enter nip:]) AND ((CE.anomes)=[enter anomes:])
GROUP BY CE.Nome

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM