sql server - use outer apply to find for each row have a record based on first result from detail table -
i have 2 tables; problem
problem (as master) group of problem , problem_log
(as detail)
problem
problem_id problem pgroup 1 line down network 2 node down network 3 hardware error hardware 4 pm hardware 5 disk error hardware
problem_log
term problem down_date s1dip0314 service 15-03-26 s1dip0314 pm 15-01-01 s1dip0314 service 15-01-02 s1dip0314 disk error 15-01-06 s1dip0314 hardware error 15-01-28
at first find term
have problem = 'pm'
(in case use dateadd+7
days)
select term,problem,down_date [problem_log] problem = 'pm'
but problem when find problem = 'pm'
date have pm + 7
days have other pgroup = 'hardware'
or not example there more 1 problem within 7 days there 'disk error'
result should this
term problem down_date s1dip0314 pm 15-01-01 s1dip0314 disk error 15-01-06
and when found more problem find more problem within 30 days last problem found
term problem down_date s1dip0314 pm 15-01-01 s1dip0314 disk error 15-01-06 s1dip0314 hardware error 15-01-28
i must use outer apply make condition come true
i tried this
select pt.term, pts.problem, pts.down_date problem_log pt left outer join atm on pt.term = atm.term outer apply( select px.term, px.problem, px.down_date problem_log px left join problem pf on pf.problem = px.problem px.problem = pt.problem , pgroup = 'hardware' ) pts pt.problem = 'pm' , ('20' + pt.down_date + ' ' + down_time) <= dateadd( day,7,('20' + pt.down_date + ' ' + pt.down_time))) pts;
i think need logs of problems same group can use simple query this:
declare @term varchar(10) = 's1dip0314' declare @problem varchar(10) = 'pm' select pt.term, pt.problem, pt.down_date problem_log pt pt.term = @term , pt.problem in (select p.problem problem p p.pgroup = (select p1.pgroup problem p1 p1.problem = @problem))
Comments
Post a Comment