所以我有以下内容:
SELECT bt.CompanyName,
AcceptedTable.Count AS AcceptedCount,
CompletedTable.Count AS CompletedCount,
SkippedTable.Count AS SkippedCount,
TotalTable.Count AS TotalCount
FROM (
SELECT uic.*, uic.Id AS UCId, ic.*, ic.Id AS CompanyId, ic.Name AS CompanyName
FROM UserChallenges iuc
JOIN Users iu ON iuc.UserId = iu.Id
JOIN Companies ic ON ic.Id = iu.CompanyId) bt
JOIN (
SELECT CompanyId AS CompanyId, COUNT(UCId) AS Count
FROM bt
GROUP BY CompanyId) TotalTable ON CompletedTable.CompanyId = bt.CompanyId
JOIN (
SELECT CompanyId AS CompanyId, COUNT(UCId) AS Count
FROM bt
WHERE AcceptedAt IS NOT NULL
GROUP BY CompanyId) AcceptedTable ON CompletedTable.CompanyId = bt.CompanyId
JOIN (
SELECT CompanyId AS CompanyId, COUNT(UCId) AS Count
FROM bt
WHERE DoneStatus IN (1,6)
GROUP BY CompanyId) CompletedTable ON CompletedTable.CompanyId = bt.CompanyId
JOIN (
SELECT CompanyId AS CompanyId, COUNT(UCId) AS Count
FROM bt
WHERE DoneStatus IN (4)
GROUP BY CompanyId) SkippedTable ON SkippedTable.CompanyId = bt.CompanyId
GROUP BY bt.CompanyName
目标是使用不同的WHERE子句对同一组数据计算计数。
我尝试了上述方法,因为它简化了查询,而不必在每个子查询中再次执行JOIN。 但是似乎不可能,它是Common table expression,在mysql 5.7中不可用。
有什么更好的方法来实现呢?
必须有比这种超长查询更干净的方法。
我也可以使用临时表,尽管我不确定在View中这是个好主意。
答案 0 :(得分:1)
您不能在其他派生表的<div class="mat-elevation-z8">
<table *ngIf="dataSource" mat-table class="full-width-table" [dataSource]="dataSource" matSort aria-label="Elements">
</table>
......
</div>
子句中引用另一个Derived Table别名,因为在该级别不能访问该别名。
此外,我觉得您可以使用条件聚合来解决此问题:
FROM