简体   繁体   English

Postgresql 查询 GROUP BY 不可能

[英]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.如果我不使用 GROUP BY 选项,一切都已完成,但我必须按NumeroFattura列进行分组。

When I add the Group BY sentence I get this error:当我添加 Group BY 句子时,我收到此错误:

ERROR: column "ft.Riferimento1" must appear in the GROUP BY clause or be used in an aggregate function错误:列“ft.Riferimento1”必须出现在 GROUP BY 子句中或用于聚合函数中

if I add ft.Riferimento1 system ask me ft.Riferimento2 also and all columns, but I want to group just for NumeroFattura column.如果我添加 ft.Riferimento1 系统也询问我 ft.Riferimento2 和所有列,但我只想为 NumeroFattura 列分组。

"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)这是一个输出示例,在最后一列中,我有 NumeroFattura 行,我将为此值分组(在此示例中,我应该有两行结果)

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 :如果您想要每个"NumeroFattura"一行,则使用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.不清楚您想要哪一行,但您可以在ORDER BY添加一个额外的键来选择其中的哪一行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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