Oracle SQL Query用于查找具有最大日期的特定状态的记录

时间:2016-08-02 22:48:05

标签: sql oracle

我需要编写Oracle sql查询以获得如下输出。开始和结束日期都是DATE数据类型。我有一个这样的文件

ApName DbName Status StartDate EndDate
A1    A11   Success 8/3/2016 8/3/2016
A1    A11   Failed  8/3/2016 8/3/2016
A2    A22   Success 8/2/2016 8/2/2016
A3    A33   Success 8/2/2016 8/2/2016
A4    A44   Failed  8/2/2016 8/2/2016 
A4    A44   Failed  8/2/2016 8/2/2016
A4    A44   Success 8/3/2016 8/3/2016 
A5    A55   Failed  8/3/2016 8/3/2016
A5    A55   Failed  8/3/2016 8/3/2016
A2    A22   Success 8/3/2016 8/3/2016

我需要输出,其中apname和dbname成功并失败或只有失败状态。我不希望apname和dbname只有状态成功。所有这些条件都应适用于最大日期(例如,基于输入文件的2016年8月3日)

ApName DbName Status StartDate EndDate
  A1    A11   Success 8/3/2016 8/3/2016
  A1    A11   Failed  8/3/2016 8/3/2016
  A5    A55   Failed  8/3/2016 8/3/2016
  A5    A55   Failed  8/3/2016 8/3/2016

3 个答案:

答案 0 :(得分:0)

因此,如果我理解正确,如果在最长日期"Ariel": { "adult": false, "backdrop_path": "\/z2QUexmccqrvw1kDMw3R8TxAh5E.jpg", "belongs_to_collection": { "id": 8384, "name": "Proletariat Collection", "poster_path": null, "backdrop_path": "\/ibLeWo3X3dVo4rxvn4m3y90ms1I.jpg" }, "0": { "Varjoja paratiisissa": { "adult": false, "backdrop_path": "\/6YjUX87VtIEuDzanBE6obVxE9V3.jpg", "belongs_to_collection": { "id": 8384, "name": "Proletariat Collection", "poster_path": null, "backdrop_path": "\/ibLeWo3X3dVo4rxvn4m3y90ms1I.jpg" }, "1": { "Life in Loops (A Megacities RMX)": { "adult": false, "backdrop_path": "\/udvB86uyzJ6P9vmB83WfrCbnmnI.jpg", "belongs_to_collection": null, "budget": 42000, "genres": { "0": { "id": 99, "name": "Documentary" } } 至少有一个状态,您想要返回给定apname的所有行吗?

如果是这样,这应该会让您使用failed

朝着正确的方向前进
max over

使用select t.* from yourtable t join ( select apname, enddate, status, max(enddate) over () maxenddate from yourtable ) t2 on t2.apname = t.apname and t2.maxenddate = t.enddate and t2.status = 'Failed' 的替代方法:

exists

答案 1 :(得分:0)

假设状态为“成功”或“失败”,它应该完成工作:

SELECT * FROM 
(
    SELECT a.* , 
    count(distinct Status) over (partition by a.AppName, trunc(a.EndDate)) as  statusCnt, 
    max(endDate) over(partition by a.AppName) as lastDate
    FROM your_table a
)b
WHERE trunc(b.lastDate) = trunc(b.EndDate) AND (statusCnt > 1 OR Status = 'Failed');

我可能会误解有关“最长日期”的部分。我的查询比较了每个AppName的最大日期,而不是整体最大日期。

答案 2 :(得分:0)

这是一个使用分析函数但没有连接的解决方案。

<强>假设

日期可能包含时间元素(时间)。要求是返回至少有一行具有FAILED状态和EndDate的应用程序/数据库的行,这些行与表中所有enddates的最大值相同(但可能是较早的时间段)。

对于这些应用程序/数据库对,返回表中的所有行。 (如果要求只返回最大日期的行,则问题会更容易。)

<强>查询

with
     inputs ( ApName, DbName, Status, StartDate, EndDate ) as (
       select 'A1', 'A11', 'Success', to_date('8/3/2016', 'mm/dd/yyyy'), to_date('8/3/2016', 'mm/dd/yyyy') from dual union all
       select 'A1', 'A11', 'Failed' , to_date('8/3/2016', 'mm/dd/yyyy'), to_date('8/3/2016', 'mm/dd/yyyy') from dual union all
       select 'A2', 'A22', 'Success', to_date('8/2/2016', 'mm/dd/yyyy'), to_date('8/2/2016', 'mm/dd/yyyy') from dual union all
       select 'A3', 'A33', 'Success', to_date('8/2/2016', 'mm/dd/yyyy'), to_date('8/2/2016', 'mm/dd/yyyy') from dual union all
       select 'A4', 'A44', 'Failed' , to_date('8/2/2016', 'mm/dd/yyyy'), to_date('8/2/2016', 'mm/dd/yyyy') from dual union all
       select 'A4', 'A44', 'Failed' , to_date('8/2/2016', 'mm/dd/yyyy'), to_date('8/2/2016', 'mm/dd/yyyy') from dual union all
       select 'A4', 'A44', 'Success', to_date('8/3/2016', 'mm/dd/yyyy'), to_date('8/3/2016', 'mm/dd/yyyy') from dual union all 
       select 'A5', 'A55', 'Failed' , to_date('8/3/2016', 'mm/dd/yyyy'), to_date('8/3/2016', 'mm/dd/yyyy') from dual union all
       select 'A5', 'A55', 'Failed' , to_date('8/3/2016', 'mm/dd/yyyy'), to_date('8/3/2016', 'mm/dd/yyyy') from dual union all
       select 'A2', 'A22', 'Success', to_date('8/3/2016', 'mm/dd/yyyy'), to_date('8/3/2016', 'mm/dd/yyyy') from dual
     ),
     prep ( apname, dbname, status, startdate, enddate, fdate, mdate ) as (
       select apname, dbname, status, startdate, enddate,
              max(case when status = 'Failed' then enddate end) over   
                                                           (partition by apname, dbname),
              max(enddate) over ()
       from   inputs
     )
select apname, dbname, status, startdate, enddate
from   prep
where  fdate >= trunc(mdate)
;

<强>输出

APNAME DBNAME STATUS  STARTDATE  ENDDATE
------ ------ ------- ---------- ----------
A1     A11    Failed  2016-08-03 2016-08-03
A1     A11    Success 2016-08-03 2016-08-03
A5     A55    Failed  2016-08-03 2016-08-03
A5     A55    Failed  2016-08-03 2016-08-03

4 rows selected.
相关问题