我有下表。
我想按小时计算小时和分区,然后每个小时花费Max
小时。以下是我到目前为止的情况。
表A
id custid projid hours
1 1010 Yellow 1
1 1011 Yellow 2
1 1012 Yellow 5
1 1010 Yellow 5
SQL:
select SUM(HOURS)OVER (PARTITION BY ID ORDER BY cust) AS TOTAL_HRS
from tablea
预期输出:上述SQL无法捕获MAX
小时
id custid projid hours
1 1010 Yellow 6
答案 0 :(得分:2)
您还需要一个partition
:
select *, sum(hours) over (partition by id, custid, projid order by cust) AS TOTAL_HRS
from tablea t
order by sum(hours) over (partition by id, custid, projid order by cust) desc
fetch first 1 row only;
答案 1 :(得分:2)
使用SUM
作为分析函数通常意味着您希望在保留所有原始记录的同时找到总和。但是您的预期输出似乎意味着汇总。因此,我建议使用GROUP BY
,然后查询以找到时间最长的行。
WITH cte AS (
SELECT id, custid, projid, SUM(hours) AS hours
FROM yourTable
GROUP BY id, custid, projid
)
SELECT *
FROM cte
ORDER BY hours DESC
WHERE rownum = 1
答案 2 :(得分:0)
我想知道这是否符合您的要求:
<li class="nav-item" data-toggle="tooltip" data-placement="right" title="Degree Pages">
<a class="nav-link nav-link-collapse collapsed" data-toggle="collapse" href="#collapseDegreePages" data-parent="#exampleAccordion">
<i class="fa fa-fw fa-graduation-cap"></i>
<span class="nav-link-text">Degree Management</span>
</a>
<ul class="sidenav-second-level collapse" id="collapseDegreePages">
<li>
<a href="addDegree.php">Add New Degree</a>
</li>
<li>
<a href="viewDegrees.php">View All Degrees</a>
</li>
</ul>
</li>
目前还不清楚为什么你需要行中的其他值。如果这样做,您可以将它们聚合成一行。