Microsoft Query 中 DISTINCT 的 SUM
SUM of DISTINCT in Microsoft Query
我有一个查询,returns 数据有点像这样:
REF01 10 50 1
REF01 10 50 1
REF01 20 40 1
REF01 20 40 1
REF01 30 30 2
REF02 40 20 1
REF02 50 10 2
REF02 50 10 2
我需要它 return 像这样:
REF01 60 120
REF02 90 30
为此,我首先添加一个 DISTINCT
(GROUP BY
所有列似乎也做同样的事情)以删除重复项,return 是这样的:
REF01 10 50 1
REF01 20 40 1
REF01 30 30 2
REF02 40 20 1
REF02 50 10 2
然后我需要添加该结果的 SUM()
,而不计算删除的重复结果。
我已经尝试过像 this one but I get an error with the FROM (SELECT ...)
and this one 这样的解决方案,它确实有效,除了子查询中的一列是操作的结果 (column_1-column_2-column_3
) 导致错误 ORA-00972: identifier is too long
超过 30 个字符的最大限制并尝试在子查询中应用带有 AS
的别名(在 WITH SUBQUERY AS ( ... )
中在我尝试过的 MS Query 中不起作用。
支持大多数 SQL 函数,这在 Microsoft Query 上并不是最好的。
编辑 - 这是有效的解决方案:
这个问题似乎与 auto-generated 列名有关,这对 Oracle 来说太长了。为了避免这个问题,查询可以显式命名 CTE 列,如下所示:
with
x (col1, col2, col3, col4) as ( -- columns are named here
select distinct col1, col2, col3, col4 from t
)
select col1, sum(col2), sum(col3)
from x
group by col1
解决方案 #2(在该工具中不起作用):
如果您使用的工具不支持子查询,您仍然可以通过创建视图来欺骗它。例如:
create view view1 as select distinct col1, col2, col3, col4 from t
然后,运行一个使用它的查询:
select col1, sum(col2), sum(col3) from view1 group by col1
结果:
COL1 SUM(COL2) SUM(COL3)
----- --------- ---------
REF02 90 30
REF01 60 120
解决方案 #3(在该工具中也不起作用):
select col1, sum(col2), sum(col3) from (
select distinct col1, col2, col3, col4 from t
) x
group by col1
用于测试的数据:
供参考,我使用的数据脚本是:
create table t (
col1 varchar2(10),
col2 number(6),
col3 number(6),
col4 number(6)
);
insert into t (col1, col2, col3, col4) values ('REF01', 10, 50, 1);
insert into t (col1, col2, col3, col4) values ('REF01', 10, 50, 1);
insert into t (col1, col2, col3, col4) values ('REF01', 20, 40, 1);
insert into t (col1, col2, col3, col4) values ('REF01', 20, 40, 1);
insert into t (col1, col2, col3, col4) values ('REF01', 30, 30, 1);
insert into t (col1, col2, col3, col4) values ('REF02', 40, 20, 1);
insert into t (col1, col2, col3, col4) values ('REF02', 50, 10, 1);
insert into t (col1, col2, col3, col4) values ('REF02', 50, 10, 1);
看起来很明显,但这行不通吗(已修订)?
select col1, sum(col2) as col2, sum(col3) as col3 from
(Select col1, col2, sum(DISTINCT col3) as col3 from t
group by col1, col2) aa
group by col1
或使用 WITH:
WITH tt as (Select col1, col2, sum(DISTINCT col3) as col3 from t
group by col1, col2)
select col1, sum(col2) as col2, sum(col3) as col3 from tt
group by col1
我有一个查询,returns 数据有点像这样:
REF01 10 50 1
REF01 10 50 1
REF01 20 40 1
REF01 20 40 1
REF01 30 30 2
REF02 40 20 1
REF02 50 10 2
REF02 50 10 2
我需要它 return 像这样:
REF01 60 120
REF02 90 30
为此,我首先添加一个 DISTINCT
(GROUP BY
所有列似乎也做同样的事情)以删除重复项,return 是这样的:
REF01 10 50 1
REF01 20 40 1
REF01 30 30 2
REF02 40 20 1
REF02 50 10 2
然后我需要添加该结果的 SUM()
,而不计算删除的重复结果。
我已经尝试过像 this one but I get an error with the FROM (SELECT ...)
and this one 这样的解决方案,它确实有效,除了子查询中的一列是操作的结果 (column_1-column_2-column_3
) 导致错误 ORA-00972: identifier is too long
超过 30 个字符的最大限制并尝试在子查询中应用带有 AS
的别名(在 WITH SUBQUERY AS ( ... )
中在我尝试过的 MS Query 中不起作用。
支持大多数 SQL 函数,这在 Microsoft Query 上并不是最好的。
编辑 - 这是有效的解决方案:
这个问题似乎与 auto-generated 列名有关,这对 Oracle 来说太长了。为了避免这个问题,查询可以显式命名 CTE 列,如下所示:
with
x (col1, col2, col3, col4) as ( -- columns are named here
select distinct col1, col2, col3, col4 from t
)
select col1, sum(col2), sum(col3)
from x
group by col1
解决方案 #2(在该工具中不起作用):
如果您使用的工具不支持子查询,您仍然可以通过创建视图来欺骗它。例如:
create view view1 as select distinct col1, col2, col3, col4 from t
然后,运行一个使用它的查询:
select col1, sum(col2), sum(col3) from view1 group by col1
结果:
COL1 SUM(COL2) SUM(COL3)
----- --------- ---------
REF02 90 30
REF01 60 120
解决方案 #3(在该工具中也不起作用):
select col1, sum(col2), sum(col3) from (
select distinct col1, col2, col3, col4 from t
) x
group by col1
用于测试的数据:
供参考,我使用的数据脚本是:
create table t (
col1 varchar2(10),
col2 number(6),
col3 number(6),
col4 number(6)
);
insert into t (col1, col2, col3, col4) values ('REF01', 10, 50, 1);
insert into t (col1, col2, col3, col4) values ('REF01', 10, 50, 1);
insert into t (col1, col2, col3, col4) values ('REF01', 20, 40, 1);
insert into t (col1, col2, col3, col4) values ('REF01', 20, 40, 1);
insert into t (col1, col2, col3, col4) values ('REF01', 30, 30, 1);
insert into t (col1, col2, col3, col4) values ('REF02', 40, 20, 1);
insert into t (col1, col2, col3, col4) values ('REF02', 50, 10, 1);
insert into t (col1, col2, col3, col4) values ('REF02', 50, 10, 1);
看起来很明显,但这行不通吗(已修订)?
select col1, sum(col2) as col2, sum(col3) as col3 from
(Select col1, col2, sum(DISTINCT col3) as col3 from t
group by col1, col2) aa
group by col1
或使用 WITH:
WITH tt as (Select col1, col2, sum(DISTINCT col3) as col3 from t
group by col1, col2)
select col1, sum(col2) as col2, sum(col3) as col3 from tt
group by col1