我有下表并使用oracle sql
TestName Status
ABC Passed_On_ReRun
123 Failed
EFG Passed
结果
Submit
问题:需要查询才能获取。我尝试过MAX但不能正常工作
答案 0 :(得分:1)
您可以使用窗口功能来做到这一点:
select
TestName,
case
when Status = 'Passed' and failed_once = 1
then 'Passed_On_Rerun'
else Status
end Status
from (
select
t.*,
rank() over(partition by TestName order by Timestamp desc) rn,
max(case when Status = 'Failed' then 1 else 0 end) over(partition by TestName) failed_once
from TestCaseStatus t
) t
where rn = 1
TESTNAME | STATUS :------- | :-------------- 123 | Failed ABC | Passed_On_Rerun EFG | Passed
答案 1 :(得分:0)
您可以通过以下查询实现所需的目标:
SELECT T1.TestName, T1.Status
FROM TestCaseStatus T1
WHERE T1.TimeStamp =
(SELECT MAX(T2.TimeStamp) FROM TestCaseStatus T2 WHERE T2.TestName = T1.TestName)
但是,您的示例数据有问题:
123 Failed 11.00 AM
123 Passed 11.00 AM
同一测试失败并通过了相同的时间戳记?!
编辑:为了满足“ Passed_On_ReRun”的需要,我们可以想象这样的解决方案:
SELECT T1.TestName, IF(T1.count > 0 AND T1.Status = 'Passed', 'Passed_On_ReRun', T1.Status)
FROM
(SELECT T2.TestName
, T2.Status
, SUM(T2.Status = 'Failed') AS count
, MAX(T2.TimeStamp) AS TimeStamp
FROM TestCaseStatus T2
GROUP BY T2.TestName) T1
答案 2 :(得分:0)
假设只有这三个条件,则可以使用条件聚合:
select testname,
(case when max(timestamp) = max(case when status = 'Failed' then timestamp end)
then 'Failed'
when max(timestamp) = max(case when status = 'Passed' then timestamp end) and
sum(case when status = 'Failed' then 1 else 0 end) > 0
then 'Passed_On_Rerun'
when max(timestamp) = max(case when status = 'Passed' then timestamp end)
then 'Passed'
else '???'
end)
from TestCaseStatus tcs
group by testname;
在Oracle中,您可以将其简化为:
select testname,
(case when max(status) keep (dense_rank first order by timestamp desc) = 'Passed' and
sum(case when status = 'Failed' then 1 else 0 end) > 0
then 'Passed_On_Rerun'
else max(status) keep (dense_rank first order by timestamp desc)
end)
from TestCaseStatus tcs
group by testname;
Here是db <>小提琴。
keep
语法正在根据时间戳获取状态的最后一个值。