我已经尝试过在阳光下进行的所有搜索来尝试解决此问题,或者是搜索不正确
我有一个带有子查询的查询,里面有2个联合查询
示例:-
SELECT name, status , SUM(TestCount) as ‘TestCount’
FROM(
select e1.reference as reference,
e1.name as name,
e1.status as status,
, SUM(CASE WHEN e1.date IS NOT NULL THEN 1 ELSE 0 END) as TestCount
FROM dbo.table1 as e1
WHERE e1.status IN('A','B','C')
GROUP BY e1.name, e1.status
UNION ALL
select e2.reference as reference,
e2.name as name,
e2.status as status,
SUM(CASE WHEN e2.date IS NOT NULL THEN 1 ELSE 0 END) as TestCount
FROM dbo.table2 as e2
WHERE e2.status IN('A','B','C')
GROUP BY e2.name, e2.status
) t
GROUP BY Name, Status
我想在子查询中添加一个SUM CASE WHEN列,然后将其从顶部拉出,仅按名称分组,而不按状态分组。 (因此,如果某人的名字出现5次,代表5种不同的状态,则只需将其姓名显示5次即可)。问题是,如果我使用WHERE子句(内部)或HAVING外部,则会过滤掉具有最后一列的SUM CASE WHEN条件的状态
例如
状态:-
A 乙 C d E-这是来自何处的总和案例
我想使用WHERE仅显示A,B,C,D,但是我想计算E。
希望这很有意义
根据建议进行了编辑:-
SUM(CASE WHEN MONTH(Field1Date) = MONTH(GETDATE()) AND YEAR(Field1Date) = YEAR(GETDATE()) AND TypeCode IN('A', 'B', 'C') THEN 1
ELSE 0 END) OVER (PARTITION BY Name) as full_count
这会导致:-
Msg 8120, Level 16, State 1, Line 1
Column 't.Field1Date' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Msg 8120, Level 16, State 1, Line 1
Column 't.Field1Date' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Msg 8120, Level 16, State 1, Line 1
Column 't.TypeCode' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Msg 8120, Level 16, State 1, Line 1
Column 't.TypeCode' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Msg 8120, Level 16, State 1, Line 1
Column 't.TypeCode' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
这样做可以将错误减少到2:-
SUM(CASE WHEN MONTH(Field1Date) = MONTH(GETDATE()) AND YEAR(Field1Date) = YEAR(GETDATE()) AND TypeCode IN('A', 'B', 'C') THEN 1
ELSE 0 END) OVER (PARTITION BY Name, Field1Date, TypeCode) as full_count
Msg 8120, Level 16, State 1, Line 2
Column 't.Field1Date' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Msg 8120, Level 16, State 1, Line 2
Column 't.TypeCode' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
我可以通过将最后两个错误添加到GROUP BY中来摆脱它们,但是我不想让它们以GROUP BY作为开始吗?
如果我将它们添加到GROUP BY以解决该错误,则SUM始终为0,因为内部查询中的WHERE语句会滤除计数所在的所有“状态”
对不起,我应该说这是SQL Server。 SSMS中的TSQL
答案 0 :(得分:1)
您可能正在寻找@Around("approveRejectPointcut()")
public Mono<Object> logAfterReturning(ProceedingJoinPoint pjp) {
...
Mono<Object> someObjectOrException = someService.updateApplicationStatus(appId, newStatus, comments);
someObjectOrException
.map(unused->pjp.proceed(pjp.getArgs())
.onErrorMap(RuntimeException.class, (s)->{
return new GenericBusinessException(s.getMessage());
});
}
来得出一组总数。您尚未告诉我们您使用的是哪个DBMS,但这是许多DBMS中可用的标准SQL函数。
我已将您的SUM OVER
简化为SUM(CASE WHEN date IS NOT NULL THEN 1 ELSE 0 END)
。我还从内部查询中删除了聚合,因为无论如何您都必须在主查询中进行聚合。
COUNT(date)