如何修复 Group By 中的聚合,缺少聚合值
How to fix Aggregation in Group By, missing aggregation values
我有 table 的销售信息,我对按客户分组并返回几列的总和、计数、最大值感兴趣。有什么想法请。
我检查了所有 Select 列都包含在 Group By 语句中,返回的是详细信息而不是分组和聚合值。
我尝试了一些明确的命名,但没有帮助。
SELECT
customerID AS CUST,
COUNT([InvoiceID]) AS Count_Invoice,
SUM([Income]) AS Total_Income,
SUM([inc2015]) AS Tot_2015_Income,
SUM([inc2016]) AS Tot_2016_Income,
MAX([prodA]) AS prod_A,
FROM [table_a]
GROUP BY
customerID, InvoiceID,Income,inc2015, inc2016, prodA
有多行 CUST,即 CUST 1、2 等应该有一行....它应该这样说...
---------------------------------------------
CUST Count_Invoice Total_Income Tot_2015_Income Tot_2016_Income prod_A
1 2 600 300 300 2
BUT IT IS RETURNING THIS
======================================
CUST Count_Invoice Total_Income Tot_2015_Income Tot_2016_Income prod_A
1 1 300 300 0 1
1 1 300 0 300 1
2 1 300 0 300 1
2 1 500 0 500 0
3 2 800 0 800 0
3 1 300 0 300 1
根据您对预期输出的描述,您应该仅按客户汇总:
SELECT
customerID A CUST,
COUNT([InvoiceID]) AS Count_Invoice,
SUM([Income]) AS Total_Income,
SUM([inc2015]) AS Tot_2015_Income,
SUM([inc2016]) AS Tot_2016_Income,
MAX([prodA]) AS prod_A
FROM [table_a]
GROUP BY
customerID;
您不需要 group by
其他列,因为它们已经按 count
、min
、max
或 sum
聚合。
所以你可以试试这个
SELECT customerID as CUST
,count([InvoiceID]) as Count_Invoice
,sum([Income]) as Total_Income
,sum([inc2015]) as Tot_2015_Income
,sum([inc2016]) as Tot_2016_Income
,max([prodA]) as prod_A --- here you are taking Max but in output it seems like sum
FROM [table_a]
Group By customerID
注意:对于 prod_A
列,您使用的 max
给出 1
但结果显示 2
实际上是 sum
或 count
。请检查。
有关详细信息,您可以找到 Group by 的 link。
我有 table 的销售信息,我对按客户分组并返回几列的总和、计数、最大值感兴趣。有什么想法请。
我检查了所有 Select 列都包含在 Group By 语句中,返回的是详细信息而不是分组和聚合值。
我尝试了一些明确的命名,但没有帮助。
SELECT
customerID AS CUST,
COUNT([InvoiceID]) AS Count_Invoice,
SUM([Income]) AS Total_Income,
SUM([inc2015]) AS Tot_2015_Income,
SUM([inc2016]) AS Tot_2016_Income,
MAX([prodA]) AS prod_A,
FROM [table_a]
GROUP BY
customerID, InvoiceID,Income,inc2015, inc2016, prodA
有多行 CUST,即 CUST 1、2 等应该有一行....它应该这样说...
---------------------------------------------
CUST Count_Invoice Total_Income Tot_2015_Income Tot_2016_Income prod_A
1 2 600 300 300 2
BUT IT IS RETURNING THIS
======================================
CUST Count_Invoice Total_Income Tot_2015_Income Tot_2016_Income prod_A
1 1 300 300 0 1
1 1 300 0 300 1
2 1 300 0 300 1
2 1 500 0 500 0
3 2 800 0 800 0
3 1 300 0 300 1
根据您对预期输出的描述,您应该仅按客户汇总:
SELECT
customerID A CUST,
COUNT([InvoiceID]) AS Count_Invoice,
SUM([Income]) AS Total_Income,
SUM([inc2015]) AS Tot_2015_Income,
SUM([inc2016]) AS Tot_2016_Income,
MAX([prodA]) AS prod_A
FROM [table_a]
GROUP BY
customerID;
您不需要 group by
其他列,因为它们已经按 count
、min
、max
或 sum
聚合。
所以你可以试试这个
SELECT customerID as CUST
,count([InvoiceID]) as Count_Invoice
,sum([Income]) as Total_Income
,sum([inc2015]) as Tot_2015_Income
,sum([inc2016]) as Tot_2016_Income
,max([prodA]) as prod_A --- here you are taking Max but in output it seems like sum
FROM [table_a]
Group By customerID
注意:对于 prod_A
列,您使用的 max
给出 1
但结果显示 2
实际上是 sum
或 count
。请检查。
有关详细信息,您可以找到 Group by 的 link。