SQL 分组无效。
SQL Group by Not working.
我有一个很长的 SQL 项目列表,我需要做的是根据其中的两个项目进行分组 --
例如:
select distinct
a.customer_no ,
a.customer_prefix ,
a.customer_lname ,
a.customer_street1
... -- about 100 other rows
a.inv_mail_dt,
a.pmt_due_dt,
a.pmt_rcvd_dt,
a.pmt_rcvd_amt,
a.perf_date,
a.perf_desc,
a.sub_line_item_price_type_desc ,
a.Price_type_desc,
sum(a.sli_due_amt),
sum(a.sli_paid_amt)
from VIEW a
Where a.customer_no = @customer_no
Group by
a.customer_no,
a.order_no,
a.Price_type_desc
但我不断收到错误消息,要求我将其他 columns/fields 添加到分组依据。但是,如果它们是唯一的,则不应使用它们进行分组,如果它们相同,则它们应该分开
But those [columns] shouldn't be used to group by if they are unique then they should be separate if they are the same
那不是 GROUP BY
的工作方式。您在 select 中指定的每个表达式必须是以下两种之一:
- A
GROUP BY
列 - 即来自 GROUP BY
列表的列,或
- 聚合表达式 - 即 selects
MIN
, MAX
, SUM
, [=17 的函数=],等等
GROUP BY
查询将使用 "group by" 列将结果拆分为 "buckets",在每个单独的存储桶内执行聚合,并将结果 return 提供给您。
由于向 GROUP BY
列表中添加更多列可能会导致创建比您需要的更多的存储桶,解决此问题的另一种方法是完全删除未使用的列,运行 a GROUP BY
和 "flat" 分别查询,并在 RDBMS 或主机环境中加入结果:
select distinct
a.customer_no,
a.order_no,
a.Price_type_desc,
a.customer_prefix,
a.customer_lname,
a.customer_street1
... -- about 100 other rows
a.inv_mail_dt,
a.pmt_due_dt,
a.pmt_rcvd_dt,
a.pmt_rcvd_amt,
a.perf_date,
a.perf_desc,
a.sub_line_item_price_type_desc,
x.sum_sli_due_amt,
x.sum_sli_paid_amt
from VIEW a
join (
SELECT
b.customer_no,
b.order_no,
b.Price_type_desc,
sum(b.sli_due_amt) as sum_sli_due_amt,
sum(b.sli_paid_amt) as sum_sli_paid_amt
from VIEW b
Group by
b.customer_no,
b.order_no,
b.Price_type_desc
Where b.customer_no = @customer_no
) x ON a.customer_no=x.customer_no
AND a.order_no=x.order_no
AND a.Price_type_desc=x.Price_type_desc
Where a.customer_no = @customer_no
我有一个很长的 SQL 项目列表,我需要做的是根据其中的两个项目进行分组 --
例如:
select distinct
a.customer_no ,
a.customer_prefix ,
a.customer_lname ,
a.customer_street1
... -- about 100 other rows
a.inv_mail_dt,
a.pmt_due_dt,
a.pmt_rcvd_dt,
a.pmt_rcvd_amt,
a.perf_date,
a.perf_desc,
a.sub_line_item_price_type_desc ,
a.Price_type_desc,
sum(a.sli_due_amt),
sum(a.sli_paid_amt)
from VIEW a
Where a.customer_no = @customer_no
Group by
a.customer_no,
a.order_no,
a.Price_type_desc
但我不断收到错误消息,要求我将其他 columns/fields 添加到分组依据。但是,如果它们是唯一的,则不应使用它们进行分组,如果它们相同,则它们应该分开
But those [columns] shouldn't be used to group by if they are unique then they should be separate if they are the same
那不是 GROUP BY
的工作方式。您在 select 中指定的每个表达式必须是以下两种之一:
- A
GROUP BY
列 - 即来自GROUP BY
列表的列,或 - 聚合表达式 - 即 selects
MIN
,MAX
,SUM
, [=17 的函数=],等等
GROUP BY
查询将使用 "group by" 列将结果拆分为 "buckets",在每个单独的存储桶内执行聚合,并将结果 return 提供给您。
由于向 GROUP BY
列表中添加更多列可能会导致创建比您需要的更多的存储桶,解决此问题的另一种方法是完全删除未使用的列,运行 a GROUP BY
和 "flat" 分别查询,并在 RDBMS 或主机环境中加入结果:
select distinct
a.customer_no,
a.order_no,
a.Price_type_desc,
a.customer_prefix,
a.customer_lname,
a.customer_street1
... -- about 100 other rows
a.inv_mail_dt,
a.pmt_due_dt,
a.pmt_rcvd_dt,
a.pmt_rcvd_amt,
a.perf_date,
a.perf_desc,
a.sub_line_item_price_type_desc,
x.sum_sli_due_amt,
x.sum_sli_paid_amt
from VIEW a
join (
SELECT
b.customer_no,
b.order_no,
b.Price_type_desc,
sum(b.sli_due_amt) as sum_sli_due_amt,
sum(b.sli_paid_amt) as sum_sli_paid_amt
from VIEW b
Group by
b.customer_no,
b.order_no,
b.Price_type_desc
Where b.customer_no = @customer_no
) x ON a.customer_no=x.customer_no
AND a.order_no=x.order_no
AND a.Price_type_desc=x.Price_type_desc
Where a.customer_no = @customer_no