EXISTS 内部 CASE 内部 AGGREGATE FUNCTION
EXISTS inside CASE inside AGGREGATE FUNCTION
我正在尝试做类似的事情:
SELECT SUM(
CASE WHEN (<some_condition>
AND EXISTS(SELECT 1
FROM <tableA> as tA
WHERE tA.Id = tB.tAId and
<some_other_condition>
)
) THEN 1 ELSE 0 END
) as <column_name>
FROM <tableB> as tB
我需要避免使用连接来实现我的需要,因为在这种情况下,我不想 count/sum 通过连接子句获得的结果返回的重复项。当我尝试执行查询时,收到以下错误消息:
"cannot perform an aggregate function on an expression containing an aggregate or a subquery"
我不知道我做错了什么。
感谢任何帮助。
EDIT: I added a possible condition on the WHERE statement only to show I need to relate the columns of the outside table (tableB) with the inside table (tableA)
下面是 tableA 和 tableB 的示例,以及当我加入两者时会发生什么:
tableA: tableB: tableA JOIN tableB
Id Id tAId Time Id(A) Id(B) taId Time
1 1 1 5 1 1 1 5
2 2 2 6 2 2 2 6
3 3 2 10 2 3 2 10
4 4 3 11 3 4 3 11
5 5 4 14 4 5 4 14
6 4 13 4 6 4 13
7 4 12 4 7 4 12
8 5 10 5 8 5 10
因此,当我尝试使用 JOIN 操作的结果作为输入从 tableA count/sum 某些 属性(即 tableA.Id)时,我最终计算重复项。这就是为什么我不能使用 JOIN 除非我可以 select DISTINCT 元素。我喜欢显示此内容的答案,因此我正在尝试无误地实施它。
(注意:我非常感谢所有其他答案,因为它们显示了适合我情况的可能解决方案)
因为正如消息所说,您不能在聚合函数中使用子查询 (SUM()
),请考虑使用类似于以下的连接:
SELECT SUM(
CASE WHEN (<some_condition>
AND tA.<matching_column> IS NOT NULL
) THEN 1 ELSE 0 END
) as <column_name>
FROM <tableB> tB
LEFT JOIN <tableA> tA
ON tA.Id = tB.Id
WHERE tA.<some_other_condition>
SELECT SUM(
CASE WHEN (<some_condition>
AND tA.Id IS NOT NULL
) THEN 1 ELSE 0 END
) as <column_name>
FROM <tableB> as tB
JOIN (SELECT DISTINCT ta.Id
FROM <tableA>) as tA ON tA.Id = tB.Id AND <some_other_condition>
根据您最初提供的内容简单回答:
SELECT SUM(<column_name>) AS SumColumn
FROM (
SELECT CASE WHEN <some_condition> AND EXISTS(SELECT 1 FROM <tableA> WHERE <some_other_condition>) THEN 1 ELSE 0 END as <column_name>
FROM <tableB>
)AS SUB
您的查询是聚合查询,因此目的似乎是返回一行。因此,只需将所有条件移至 WHERE
子句即可。这是我对您的意图的最佳猜测:
SELECT COUNT(*) as <column_name>
FROM <tableB> tB
WHERE (<some_condition> AND
EXISTS (SELECT 1
FROM <tableA> as tA
WHERE tA.Id = tB.Id AND <some_other_condition>
);
我正在尝试做类似的事情:
SELECT SUM(
CASE WHEN (<some_condition>
AND EXISTS(SELECT 1
FROM <tableA> as tA
WHERE tA.Id = tB.tAId and
<some_other_condition>
)
) THEN 1 ELSE 0 END
) as <column_name>
FROM <tableB> as tB
我需要避免使用连接来实现我的需要,因为在这种情况下,我不想 count/sum 通过连接子句获得的结果返回的重复项。当我尝试执行查询时,收到以下错误消息:
"cannot perform an aggregate function on an expression containing an aggregate or a subquery"
我不知道我做错了什么。
感谢任何帮助。
EDIT: I added a possible condition on the WHERE statement only to show I need to relate the columns of the outside table (tableB) with the inside table (tableA)
下面是 tableA 和 tableB 的示例,以及当我加入两者时会发生什么:
tableA: tableB: tableA JOIN tableB
Id Id tAId Time Id(A) Id(B) taId Time
1 1 1 5 1 1 1 5
2 2 2 6 2 2 2 6
3 3 2 10 2 3 2 10
4 4 3 11 3 4 3 11
5 5 4 14 4 5 4 14
6 4 13 4 6 4 13
7 4 12 4 7 4 12
8 5 10 5 8 5 10
因此,当我尝试使用 JOIN 操作的结果作为输入从 tableA count/sum 某些 属性(即 tableA.Id)时,我最终计算重复项。这就是为什么我不能使用 JOIN 除非我可以 select DISTINCT 元素。我喜欢显示此内容的答案,因此我正在尝试无误地实施它。
(注意:我非常感谢所有其他答案,因为它们显示了适合我情况的可能解决方案)
因为正如消息所说,您不能在聚合函数中使用子查询 (SUM()
),请考虑使用类似于以下的连接:
SELECT SUM(
CASE WHEN (<some_condition>
AND tA.<matching_column> IS NOT NULL
) THEN 1 ELSE 0 END
) as <column_name>
FROM <tableB> tB
LEFT JOIN <tableA> tA
ON tA.Id = tB.Id
WHERE tA.<some_other_condition>
SELECT SUM(
CASE WHEN (<some_condition>
AND tA.Id IS NOT NULL
) THEN 1 ELSE 0 END
) as <column_name>
FROM <tableB> as tB
JOIN (SELECT DISTINCT ta.Id
FROM <tableA>) as tA ON tA.Id = tB.Id AND <some_other_condition>
根据您最初提供的内容简单回答:
SELECT SUM(<column_name>) AS SumColumn
FROM (
SELECT CASE WHEN <some_condition> AND EXISTS(SELECT 1 FROM <tableA> WHERE <some_other_condition>) THEN 1 ELSE 0 END as <column_name>
FROM <tableB>
)AS SUB
您的查询是聚合查询,因此目的似乎是返回一行。因此,只需将所有条件移至 WHERE
子句即可。这是我对您的意图的最佳猜测:
SELECT COUNT(*) as <column_name>
FROM <tableB> tB
WHERE (<some_condition> AND
EXISTS (SELECT 1
FROM <tableA> as tA
WHERE tA.Id = tB.Id AND <some_other_condition>
);