我试图以不同的方式聚合一些字段 - 使用分区 - 在同一个查询中但发现了AVG()的问题:
采用这个定义:
CREATE TABLE Result
(CheckListId int, CheckId int, AuditId int, CheckListResult FLOAT, CheckResult FLOAT)
INSERT INTO Result VALUES (1,1,1,1,1)
INSERT INTO Result VALUES (1,2,1,1,3)
INSERT INTO Result VALUES (1,2,2,3,1)
INSERT INTO Result VALUES (2,1,1,3,1)
+-------------+---------+---------+-----------------+-------------+
| CheckListId | CheckId | AuditId | CheckListResult | CheckResult |
+-------------+---------+---------+-----------------+-------------+
| 1 | 1 | 1 | 1 | 1 |
| 1 | 2 | 1 | 1 | 3 |
| 1 | 2 | 2 | 3 | 1 |
| 2 | 1 | 1 | 3 | 1 |
+-------------+---------+---------+-----------------+-------------+
感谢Format Text as Table格式化
这是我的选择
SELECT
CheckListId
, CheckId
, (dense_rank() over (PARTITION BY CheckListId order by [AuditId])
+ dense_rank() over (PARTITION BY CheckListId order by [AuditId] desc)
- 1) AS N_AuditForCheckList
, AVG(CheckListResult) OVER(PARTITION BY CheckListId) AS AvgCheckListResult
, COUNT(AuditId) OVER (PARTITION BY CheckListId, CheckId) AS N_AuditForCheck
, AVG(CheckResult) OVER(PARTITION BY CheckListId, CheckId) AS AvgCheckResult
FROM Result
我得到了这个结果
+-------------+---------+---------------------+--------------------+-----------------+----------------+
| CheckListId | CheckId | N_AuditForCheckList | AvgCheckListResult | N_AuditForCheck | AvgCheckResult |
+-------------+---------+---------------------+--------------------+-----------------+----------------+
| 1 | 1 | 2 | 1,67 | 1 | 1 |
| 1 | 2 | 2 | 1,67 | 2 | 2 |
| 1 | 2 | 2 | 1,67 | 2 | 2 |
| 2 | 1 | 1 | 3 | 1 | 1 |
+-------------+---------+---------------------+--------------------+-----------------+----------------+
而在AvgCheckListResult
我希望2
,因为在此核对清单中我有两个结果:第一次审核时为1次,第二次审核时为3次,而sql计算3行中的平均值
有没有办法在没有子查询或加入多个查询的情况下执行此操作?
P.S。
答案 0 :(得分:1)
我没有找到(目前)如何没有子查询(但我认为你已经探索了我的以下解决方案): 附:您编写问题的方式为+1(脚本,示例数据等)
SELECT *
, SUM(CheckListResult / C3 ) OVER (PARTITION BY CheckListId) / N_AuditForCheckList AS AvgChk3
FROM (
SELECT
CheckListId
, CheckId
, AuditId
, CheckListResult
, (dense_rank() over (PARTITION BY CheckListId order by [AuditId])
+ dense_rank() over (PARTITION BY CheckListId order by [AuditId] desc)
- 1) AS N_AuditForCheckList
, AVG(CheckListResult) OVER(PARTITION BY CheckListId) AS AvgCheckListResult
, AVG(CheckListResult) OVER(PARTITION BY CheckListId,AuditId) AS AvgCheckListResult2
, COUNT(*) OVER (PARTITION BY CheckListId, AuditId) AS C3
, COUNT(AuditId) OVER (PARTITION BY CheckListId, CheckId) AS N_AuditForCheck
, AVG(CheckResult) OVER(PARTITION BY CheckListId, CheckId) AS AvgCheckResult
FROM Result) A
输出:
+-------------+---------+---------+-----------------+---------------------+--------------------+---------------------+----+-----------------+----------------+---------+
| CheckListId | CheckId | AuditId | CheckListResult | N_AuditForCheckList | AvgCheckListResult | AvgCheckListResult2 | C3 | N_AuditForCheck | AvgCheckResult | AvgChk3 |
+-------------+---------+---------+-----------------+---------------------+--------------------+---------------------+----+-----------------+----------------+---------+
| 1 | 1 | 1 | 1 | 2 | 1,66666666666667 | 1 | 2 | 1 | 1 | 2 |
| 1 | 2 | 1 | 1 | 2 | 1,66666666666667 | 1 | 2 | 2 | 2 | 2 |
| 1 | 2 | 2 | 3 | 2 | 1,66666666666667 | 3 | 1 | 2 | 2 | 2 |
| 2 | 1 | 1 | 3 | 1 | 3 | 3 | 1 | 1 | 1 | 3 |
+-------------+---------+---------+-----------------+---------------------+--------------------+---------------------+----+-----------------+----------------+---------+
答案 1 :(得分:0)
也许我找到了一个嵌套查询的解决方案:
SELECT *
,(SELECT AVG(T.CheckListResult)
FROM
(SELECT DISTINCT AuditId, CheckListId, CheckListResult FROM Result) as T
WHERE T.CheckListId = Base.CheckListId
) AS AvgCheckListResult
,(SELECT COUNT(T.CheckListResult)
FROM
(SELECT DISTINCT AuditId, CheckListId, CheckListResult FROM Result) as T
WHERE T.CheckListId = Base.CheckListId
) AS N_AuditForCheckList
,(SELECT AVG(T.CheckResult)
FROM
(SELECT DISTINCT AuditId, CheckId, CheckResult FROM Result) as T
WHERE T.CheckId = Base.CheckId
) AS AvgCheckResult
FROM Result AS Base
结果是正确的但不确定性能