简体   繁体   中英

Postgresql query GROUP BY not possible

In my project I have this complex query:

SELECT FT."Riferimento1", FT."Riferimento2", "CodProdotto", "QuantitaFatturata", "PrezzoUnit", "DataFattura", "NumeroFattura", "CodCli"
FROM public.idocuments_as_fatturetestata FT
LEFT JOIN public.idocuments_as_fatturerighe FR ON FT."Riferimento1" = FR."Riferimento1" AND FT."Riferimento2" = FR."Riferimento2"
WHERE FT."CodCli" = '12192' GROUP BY "NumeroFattura";

If I don't use the GROUP BY option all was done but I have to grouping by NumeroFattura column.

When I add the Group BY sentence I get this error:

ERROR: column "ft.Riferimento1" must appear in the GROUP BY clause or be used in an aggregate function

if I add ft.Riferimento1 system ask me ft.Riferimento2 also and all columns, but I want to group just for NumeroFattura column.

"25"    "000006"    "191215002N"    1   1.800000    "2017-01-31 00:00:00+00"    589
"25"    "000009"    "112036402G"    100 0.970000    "2017-01-31 00:00:00+00"    318
"25"    "000009"    "213008200I"    200 1.660000    "2017-01-31 00:00:00+00"    318
"25"    "000009"    "213008200N"    150 1.660000    "2017-01-31 00:00:00+00"    318
"25"    "000009"    "213008500V1"   53.5    1.930000    "2017-01-31 00:00:00+00"    318
"25"    "000009"    "213008500E"    61  1.930000    "2017-01-31 00:00:00+00"    318
"25"    "000009"    "213008500R"    56  1.930000    "2017-01-31 00:00:00+00"    318
"25"    "000009"    "213008200G"    50  1.660000    "2017-01-31 00:00:00+00"    318
"25"    "000009"    "113066592N"    20  5.583000    "2017-01-31 00:00:00+00"    318
"25"    "000009"    "199900502N"    321 0.725000    "2017-01-31 00:00:00+00"    318
"25"    "000009"    "199900602N"    360 0.680000    "2017-01-31 00:00:00+00"    318
"25"    "000009"    "217001100F"    1200    2.036000    "2017-01-31 00:00:00+00"    318
"25"    "000009"    "112031102N"    1200    0.198000    "2017-01-31 00:00:00+00"    318
"25"    "000009"    "112044602N"    800 0.600000    "2017-01-31 00:00:00+00"    318
"25"    "000009"    "112036402N"    800 0.500000    "2017-01-31 00:00:00+00"    318
"25"    "000009"    "113066702N"    800 0.600000    "2017-01-31 00:00:00+00"    318
"25"    "000009"    "113066602N"    800 0.550000    "2017-01-31 00:00:00+00"    318
"25"    "000009"    "112530780N3"   5000    0.178000    "2017-01-31 00:00:00+00"    318

this is an example of output, in the last column i have the NumeroFattura row and i would to group for this value (in this example i should have two rows for results)

Someone can tell me why i can't group like i would?

So many thanks in advance

If you want one row per "NumeroFattura" , then use DISTINCT ON :

SELECT DISTINCT ON ("NumeroFattura") FT."Riferimento1", FT."Riferimento2",
       "CodProdotto", "QuantitaFatturata", "PrezzoUnit", "DataFattura",
       "NumeroFattura", "CodCli"
FROM public.idocuments_as_fatturetestata FT LEFT JOIN
     public.idocuments_as_fatturerighe FR
     ON FT."Riferimento1" = FR."Riferimento1" AND 
        FT."Riferimento2" = FR."Riferimento2"
WHERE FT."CodCli" = '12192' 
ORDER BY "NumeroFattura";

This returns an arbitrary matching row. It is unclear which row you want, but you can add an additional key to the ORDER BY to choose which of them.

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