How do I get the unique maxium or greatest (version ID) value to each (Record ID) with row data in sql server -


in sql server joining 2 tables , need way maximum or greatest (version number) value each (record id) exists in table including it's row data selected.

my query returning data follows returns record id's , versions, need return max version number value , row data:

select distinct     max(a.version_num), max(a.record_id),      b.person, b.status,       cast(a.summary_data nvarchar(4000))       table1 a, table2 b       a.record_id = b.record_id      , a.record_id not null      , a.summary_data not null  group      a.version_num, a.record_id, b.person, b.status,     cast(a.summary_data nvarchar(4000))  order      max(a.version_num), max(a.record_id) desc 

this query returns duplicate record id's version number , row data:

versionnum   record id           person    status    summary data      5       000000000000418     john      open      "specific summary data ...      4       000000000000418     jane      closed    "specific summary data ..."       3       000000000000418     sam       closed:   "specific summary data ..."        4       000000000000229     joe       pending   "specific summary data ..."       3       000000000000229     betty     closed    "specific summary data ..."      2       000000000000229     david     closed    "specific summary data ..."       7       000000000000318     karen     closed    "specific summary data ..."      6       000000000000318     sam       pending   "specific summary data ..."      5       000000000000318     betty     closed    "specific summary data ..."      4       000000000000318     david     closed    "specific summary data ..." 

i instead need return max (version number value) it's adjacent (id) , remainder of it's selected row data :

i.e.

versionnum   record id         person   status     summary data      5        000000000000418   john     open       "specific summary data ..."      4        000000000000229   joe      pending    "specific summary data ..."     7        000000000000318   karen    closed     "specific summary data ..."  

any appreciated!

i applied , tried got error - need on correct syntax return these fields

select *      (select          dt.version, dt.job_doc_set_request_id, dt.submitter,st.status, dt.summary,         row_number() on (partition dt.job_doc_set_request_id order dt.version desc) rn               ricpm_jd_jobdocdetails dt, ricpm_jd_jobdocset st) q        rn = 1      , dt.job_doc_set_request_id not '%deleted'     , st.status = 2      , dt.summary not 'obsolete%'      , dt.job_doc_set_request_id = st.request_id  

error:

java.sql.sqlexception: multi-part identifier "dt.job_doc_set_request_id" not bound.
@ net.sourceforge.jtds.jdbc.sqldiagnostic.adddiagnostic(sqldiagnostic.java:372) @ net.sourceforge.jtds.jdbc.tdscore.tdserrortoken(tdscore.java:2988)

select  *    (         select  *,                 row_number() on (partition recordid order versionnum desc) rn            mytable         ) q   rn = 1 

Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -