Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
skarumuru
Starting Member
2 Posts |
Posted - 2015-05-06 : 17:12:52
|
doc_id --- version --- activate_docd1 --- 1 --- Nd1 --- 2 --- Nd1 --- 3 --- Nd2 --- 1 ---Nd2 --- 3 ---Nd2 --- 4 ---Nd3 --- 1 ---Nd3 --- 2 ---NI want to update "activate_doc=Y" where the version having Max value based on the doc_id.expected results:d1 --- 3 --- Yd2 --- 4 ---Yd3 --- 2 ---YPlease post the query. Appreciate your help in advance. |
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2015-05-06 : 17:20:48
|
with version (doc_id, max_version)as (select doc_id, max(version) as max_version from yourtable group by doc_id)update yourtableset activate_doc = 'Y'from yourtablejoin version on yourtable.doc_id = version.doc_id and yourtable.version = version.max_versionwhere activate_doc <> 'N'Tara KizerSQL Server MVP since 2007http://weblogs.sqlteam.com/tarad/ |
|
|
skarumuru
Starting Member
2 Posts |
Posted - 2015-05-06 : 17:32:17
|
I am sorry. Is it a single query to execute on SQL Commander or its a Stored procedure?quote: Originally posted by tkizer with version (doc_id, max_version)as (select doc_id, max(version) as max_version from yourtable group by doc_id)update yourtableset activate_doc = 'Y'from yourtablejoin version on yourtable.doc_id = version.doc_id and yourtable.version = version.max_versionwhere activate_doc <> 'N'Tara KizerSQL Server MVP since 2007http://weblogs.sqlteam.com/tarad/
|
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2015-05-06 : 17:41:28
|
Yes it's a single query. But SQL Commander? SQLTeam.com is for Microsoft SQL Srever. Which dbms are you using?Tara KizerSQL Server MVP since 2007http://weblogs.sqlteam.com/tarad/ |
|
|
|
|
|