sql server - Select Newest Record -


i have 2 tables (journal , incident). each incident may have more 1 journal entry. want select record , recent journal data.

the section @ bottom filters incidents want see. of those, want journal values associated recent journal entry.

this amalgam of code i've found on here, when run "query execution failed dataset 'dataset1'. unfortunately, don't have access log files see if there clues there.

any appreciated. think may have nested wrong.

select b.incidentnumber ,a.subject ,a.createddatetime ,b.subcategory ,b.effectiveduedate ,b.nextactiondate ,b.profilefullname   ( select   b.incidentnumber  ,a.subject  ,a.createddatetime  ,rn = row_number() on (partition b.incidentnumber order     a.createddatetime desc)  ,b.subcategory  ,b.effectivedate  ,b.nextactiondate  ,b.profilefullname    journal left join incident b on  a.parentrecordnumber = b.incidentnumber  a.category '%kanban%' , (b.status like' %waiting%' or b.status '%active%') , b.subcategory <> 'user termination' , b.subcategory <> 'res temp termination' , a.subject 'up |%' ) x rn = 1 

few things:

  • outer selected values should inline view aliased "x" no a. or b. alias in scope in inner query. (except when using coorlation that's 1 level believe)
  • you either need right join instead of left or change order of tables. believe want incidents , recent journal; not journals , related incident if 1 exists. changed order.
  • lastly when using outer joins, can put limits on records table of outer join. clause criteria outer joined tables cause null records generated outer join excluded. resolve must move limiting criteria join or use 'or' statement check null (it's cleaner move join). think of applying limit before join occurs null records incident kept. otherwise outer join simulates inner join excluding records not in both tables (or in case in incident not in journal)

.

select x.incidentnumber  --alias x not a/b aliased 'x'      , x.subject      , x.createddatetime      , x.subcategory      , x.effectiveduedate      , x.nextactiondate      , x.profilefullname (select b.incidentnumber            , a.subject            , a.createddatetime            , rn = row_number() on (partition b.incidentnumber                                       order a.createddatetime desc)            , b.subcategory            , b.effectivedate            , b.nextactiondate            , b.profilefullname         incident b --switched order think want incidents , if journal exists it's value.        left join journal            on a.parentrecordnumber = b.incidentnumber          -- since on if match found b, need move join or lose records created outer join.         , a.category '%kanban%'         , a.subject 'up |%'          --moved clause criteria join since b on "all records side" of outer join can leave b in clause.         (b.status like' %waiting%' or b.status '%active%')           , b.subcategory <> 'user termination'           , b.subcategory <> 'res temp termination') x rn = 1 

if not getting records here, i'd start removing of limiting criteria ensure query functioning desired , add in limits see what's causing no records found.


Comments

Popular posts from this blog

c# - Update a combobox from a presenter (MVP) -

How to understand 2 main() functions after using uftrace to profile the C++ program? -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -