this is my query:
select NFREFC, nmff01, ciusmd, nmismd, sum(QRCEGP) as qtd
from swapfilio.gcffc join swapfilio.gcffp
on SOCEFC = SOCEFP
and NFREFC = FRNEFP
and NRFEFC = NRFEFP
join swapfilio.gcfgp
on SOCEGP = SOCEFP
and ARMEGP = ARMEFP
and AGREGP = AGREFP
and NGREGP = NGREFP
and NLNEGP = NLNEFP
and ARTEGP = ARTEFP
and MODEGP = MODEFP
and EMBEGP = EMBEFP
and GFMEGP = GFMEFP
join swapfilio.gesmd
on socsmd = SOCEGP
and niismd = ARTEGP
left join swapfilio.for01
on NFOF01 = NFREFC
and SOCF01 = SOCEFC
where SOCF01 = '1'
and SOCEFC = '1'
and socsmd = '1'
and (ciusmd = '5179627' or ciusmd = '6191569')
and DFCEFC >= 20180801
group by NFREFC, nmff01, ciusmd, nmismd
Result:
NFREFC NMFF01 CIUSMD NMISMD QTD
802 OCP PORTUGAL - PROD FARM, SA 6191569 ACCU-CHECK SAFE-T-PRO UNO LANCETAS 2.000
801 BOTELHO & RODRIGUES LDA 6191569 ACCU-CHECK SAFE-T-PRO UNO LANCETAS 2.000
803 COOPROFAR - COOP PROPRIET FARM CRL 6191569 ACCU-CHECK SAFE-T-PRO UNO LANCETAS 1.000
1825 GFPH-PHARMA,LDA. 5179627 ASACOL 800 MG COMP.GR X60 160.000
802 OCP PORTUGAL - PROD FARM, SA 5179627 ASACOL 800 MG COMP.GR X60 92.000
804 UDIFAR II DISTRI FARMACÊUTICA SA 5179627 ASACOL 800 MG COMP.GR X60 1.000
803 COOPROFAR - COOP PROPRIET FARM CRL 5179627 ASACOL 800 MG COMP.GR X60 11.000
What i want:
NFREFC NMFF01 CIUSMD NMISMD QTD
802 OCP PORTUGAL - PROD FARM, SA 6191569 ACCU-CHECK SAFE-T-PRO UNO LANCETAS 2.000 5
801 BOTELHO & RODRIGUES LDA 6191569 ACCU-CHECK SAFE-T-PRO UNO LANCETAS 2.000 5
803 COOPROFAR - COOP PROPRIET FARM CRL 6191569 ACCU-CHECK SAFE-T-PRO UNO LANCETAS 1.000 5
1825 GFPH-PHARMA,LDA. 5179627 ASACOL 800 MG COMP.GR X60 160.000 264
802 OCP PORTUGAL - PROD FARM, SA 5179627 ASACOL 800 MG COMP.GR X60 92.000 264
804 UDIFAR II DISTRI FARMACÊUTICA SA 5179627 ASACOL 800 MG COMP.GR X60 1.000 264
803 COOPROFAR - COOP PROPRIET FARM CRL 5179627 ASACOL 800 MG COMP.GR X60 11.000 264
I dont now what to get the expected result above, perhaps a subquery?
Please help me
Thank you
select NFREFC, nmff01, ciusmd, nmismd, sum(QRCEGP) as qtd
from swapfilio.gcffc join swapfilio.gcffp
on SOCEFC = SOCEFP
and NFREFC = FRNEFP
and NRFEFC = NRFEFP
join swapfilio.gcfgp
on SOCEGP = SOCEFP
and ARMEGP = ARMEFP
and AGREGP = AGREFP
and NGREGP = NGREFP
and NLNEGP = NLNEFP
and ARTEGP = ARTEFP
and MODEGP = MODEFP
and EMBEGP = EMBEFP
and GFMEGP = GFMEFP
join swapfilio.gesmd
on socsmd = SOCEGP
and niismd = ARTEGP
left join swapfilio.for01
on NFOF01 = NFREFC
and SOCF01 = SOCEFC
where SOCF01 = '1'
and SOCEFC = '1'
and socsmd = '1'
and (ciusmd = '5179627' or ciusmd = '6191569')
and DFCEFC >= 20180801
group by NFREFC, nmff01, ciusmd, nmismd
Result:
NFREFC NMFF01 CIUSMD NMISMD QTD
802 OCP PORTUGAL - PROD FARM, SA 6191569 ACCU-CHECK SAFE-T-PRO UNO LANCETAS 2.000
801 BOTELHO & RODRIGUES LDA 6191569 ACCU-CHECK SAFE-T-PRO UNO LANCETAS 2.000
803 COOPROFAR - COOP PROPRIET FARM CRL 6191569 ACCU-CHECK SAFE-T-PRO UNO LANCETAS 1.000
1825 GFPH-PHARMA,LDA. 5179627 ASACOL 800 MG COMP.GR X60 160.000
802 OCP PORTUGAL - PROD FARM, SA 5179627 ASACOL 800 MG COMP.GR X60 92.000
804 UDIFAR II DISTRI FARMACÊUTICA SA 5179627 ASACOL 800 MG COMP.GR X60 1.000
803 COOPROFAR - COOP PROPRIET FARM CRL 5179627 ASACOL 800 MG COMP.GR X60 11.000
What i want:
NFREFC NMFF01 CIUSMD NMISMD QTD
802 OCP PORTUGAL - PROD FARM, SA 6191569 ACCU-CHECK SAFE-T-PRO UNO LANCETAS 2.000 5
801 BOTELHO & RODRIGUES LDA 6191569 ACCU-CHECK SAFE-T-PRO UNO LANCETAS 2.000 5
803 COOPROFAR - COOP PROPRIET FARM CRL 6191569 ACCU-CHECK SAFE-T-PRO UNO LANCETAS 1.000 5
1825 GFPH-PHARMA,LDA. 5179627 ASACOL 800 MG COMP.GR X60 160.000 264
802 OCP PORTUGAL - PROD FARM, SA 5179627 ASACOL 800 MG COMP.GR X60 92.000 264
804 UDIFAR II DISTRI FARMACÊUTICA SA 5179627 ASACOL 800 MG COMP.GR X60 1.000 264
803 COOPROFAR - COOP PROPRIET FARM CRL 5179627 ASACOL 800 MG COMP.GR X60 11.000 264
I dont now what to get the expected result above, perhaps a subquery?
Please help me
Thank you