Best query to get the latest version

Hi,
I have a table as below:
col1 col2 col3.... col_ver col7
I have a combination od values for col1 to col6. I have values in col7 which is different for each value in col_ver. col_ver is a version column.
For a particular combination (col1 to col6) I want to get the rows with the maximum version. Can anyone suggest the best sql for this ?
I have 2 options as below, please suggest from performance point of view which would be better (i don't have enough rows to test) and if there is a any other better way
option 1:
SELECT col1, col2, col3, col4, col5, col6, col_ver, col7
FROM
(SELECT col1, col2, col3, col4, col5, col6, col_ver, col7, MAX(col_ver) OVER (PARTITION BY col1, col2, col3, col4,
col5, col6) MVER
FROM tabA )
WHERE col_ver = MVER;
option2:
SELECT col1, col2, col3, col4, col5, col6, col_ver, col7
FROM tabA A
WHERE col_ver = (SELECT MAX(col_ver) FROM tabA B
WHERE
B.col1 = A.col1
AND B.col2 = A.col2
AND B.col3 = A.col3
AND B.col4 = A.col4
AND B.col5 = A.col5
AND B.col6 = A.col6);
Thanks a lot in adavnce

SQL> with t as
  2  (select 'a1' col1, 'b1' col2, 'c1' col3, 'd1' col4, 1 col_ver,  100 col7
  3     from dual
  4   union all
  5   select 'a1' col1, 'b2' col2, 'c1' col3, 'd1' col4, 1 col_ver,  101 col7
  6     from dual
  7   union all
  8   select 'a1' col1, 'b2' col2, 'c2' col3, 'd1' col4, 1 col_ver,  102 col7
  9     from dual
10   union all
11   select 'a1' col1, 'b2' col2, 'c2' col3, 'd2' col4, 1 col_ver,  103 col7
12     from dual
13   union all
14   select 'a2' col1, 'b1' col2, 'c1' col3, 'd1' col4, 1 col_ver,  104 col7
15     from dual
16   union all
17   select 'a2' col1, 'b2' col2, 'c1' col3, 'd1' col4, 1 col_ver,  105 col7
18     from dual
19   union all
20   select 'a2' col1, 'b2' col2, 'c2' col3, 'd1' col4, 1 col_ver,  106 col7
21     from dual
22   union all
23   select 'a2' col1, 'b2' col2, 'c2' col3, 'd2' col4, 1 col_ver,  107 col7
24     from dual
25   union all
26   select 'a1' col1, 'b1' col2, 'c1' col3, 'd1' col4, 2 col_ver,  108 col7
27     from dual
28   union all
29   select 'a1' col1, 'b2' col2, 'c2' col3, 'd1' col4, 2 col_ver,  109 col7
30     from dual
31   union all
32   select 'a1' col1, 'b2' col2, 'c2' col3, 'd2' col4, 2 col_ver,  110 col7
33     from dual
34   union all
35   select 'a2' col1, 'b2' col2, 'c1' col3, 'd2' col4, 2 col_ver,  111 col7
36     from dual
37   union all
38   select 'a2' col1, 'b2' col2, 'c2' col3, 'd2' col4, 2 col_ver,  112 col7
39     from dual
40   union all
41   select 'a1' col1, 'b2' col2, 'c2' col3, 'd2' col4, 3 col_ver,  113 col7
42     from dual
43   union all
44   select 'a2' col1, 'b1' col2, 'c1' col3, 'd1' col4, 3 col_ver,  114 col7
45     from dual)
46  select * from t;
CO CO CO CO    COL_VER       COL7
a1 b1 c1 d1          1        100
a1 b2 c1 d1          1        101
a1 b2 c2 d1          1        102
a1 b2 c2 d2          1        103
a2 b1 c1 d1          1        104
a2 b2 c1 d1          1        105
a2 b2 c2 d1          1        106
a2 b2 c2 d2          1        107
a1 b1 c1 d1          2        108
a1 b2 c2 d1          2        109
a1 b2 c2 d2          2        110
a2 b2 c1 d2          2        111
a2 b2 c2 d2          2        112
a1 b2 c2 d2          3        113
a2 b1 c1 d1          3        114
15 rows selected.
SQL> -- from here we only need to get those with the lastest value for the col_ver
SQL> with t as
  2  (select 'a1' col1, 'b1' col2, 'c1' col3, 'd1' col4, 1 col_ver,  100 col7
  3     from dual
  4   union all
  5   select 'a1' col1, 'b2' col2, 'c1' col3, 'd1' col4, 1 col_ver,  101 col7
  6     from dual
  7   union all
  8   select 'a1' col1, 'b2' col2, 'c2' col3, 'd1' col4, 1 col_ver,  102 col7
  9     from dual
10   union all
11   select 'a1' col1, 'b2' col2, 'c2' col3, 'd2' col4, 1 col_ver,  103 col7
12     from dual
13   union all
14   select 'a2' col1, 'b1' col2, 'c1' col3, 'd1' col4, 1 col_ver,  104 col7
15     from dual
16   union all
17   select 'a2' col1, 'b2' col2, 'c1' col3, 'd1' col4, 1 col_ver,  105 col7
18     from dual
19   union all
20   select 'a2' col1, 'b2' col2, 'c2' col3, 'd1' col4, 1 col_ver,  106 col7
21     from dual
22   union all
23   select 'a2' col1, 'b2' col2, 'c2' col3, 'd2' col4, 1 col_ver,  107 col7
24     from dual
25   union all
26   select 'a1' col1, 'b1' col2, 'c1' col3, 'd1' col4, 2 col_ver,  108 col7
27     from dual
28   union all
29   select 'a1' col1, 'b2' col2, 'c2' col3, 'd1' col4, 2 col_ver,  109 col7
30     from dual
31   union all
32   select 'a1' col1, 'b2' col2, 'c2' col3, 'd2' col4, 2 col_ver,  110 col7
33     from dual
34   union all
35   select 'a2' col1, 'b2' col2, 'c1' col3, 'd2' col4, 2 col_ver,  111 col7
36     from dual
37   union all
38   select 'a2' col1, 'b2' col2, 'c2' col3, 'd2' col4, 2 col_ver,  112 col7
39     from dual
40   union all
41   select 'a1' col1, 'b2' col2, 'c2' col3, 'd2' col4, 3 col_ver,  113 col7
42     from dual
43   union all
44   select 'a2' col1, 'b1' col2, 'c1' col3, 'd1' col4, 3 col_ver,  114 col7
45     from dual)
46  select a.col1, a.col2, a.col3, a.col4, a.col_ver, a.col7
47    from (select t.col1, t.col2, t.col3, t.col4, t.col_ver, t.col7,
48                 row_number() over (partition by t.col1, t.col2, t.col3, t.col4
49                                    order by t.col_ver desc) rn
50            from t) a
51   where a.rn = 1;
CO CO CO CO    COL_VER       COL7
a1 b1 c1 d1          2        108
a1 b2 c1 d1          1        101
a1 b2 c2 d1          2        109
a1 b2 c2 d2          3        113
a2 b1 c1 d1          3        114
a2 b2 c1 d1          1        105
a2 b2 c1 d2          2        111
a2 b2 c2 d1          1        106
a2 b2 c2 d2          2        112
9 rows selected.
SQL>

Similar Messages

  • I have a mac  book pro that i bought in late 2009. My current software is snow leopard version 10.5.8. I would like to get the new lion operating system but I cant without first getting the latest version of snow leopard. it doesnt show on software updat

    I have a mac book pro that i bought in late 2009. My current software is snow leopard version 10.5.8. I would like to get the new lion operating system but I cant without first getting the latest version of snow leopard. however when I go to update my software it doesnt show that any update is available.

    10.5.8 is Leopard, not Snow Leopard. You need the Snow Leopard DVD;
    You can get it only by phone now from Apple.
    In the US, call 1-800-MY-APPLE and ask for a sales assistant. Last quoted price was $19.99 for the single-user and $29.99 for the family licence.
    For other countries, check here; http://support.apple.com/kb/HE57
    Don't forget to ask for Sales; the tech support guys can't help.
    If you want Lion, rather than Mountain Lion, the same Sales team can give you a download code to use at the Mac AppStore.
    To get the App Store, you need to update your new Snow Leopard from 10.6.3 (which is the version on the installer) to 10.6.8 with the combo update; http://support.apple.com/kb/DL1399
    Be warned that the upgrades will render a lot of older software unusable, and will need new drivers for printers and scanners in all likelihood.

  • I can't get the latest version of iTunes 12.0.1 for Windows (64-bit) to work on my laptop.

    I can't get the latest version of iTunes 12.0.1 for Windows (64-bit) to work on my laptop. It says it has successfully downloaded and installed, but when I try to connect my new iPhone 6 to iTunes i get an error message 'need to install new software' but i thought i had! Please help me as i have had to connect my phone to someone else's mac and therefor don't have my music etc on my phone HELP!  I have had IT support at work trying to figure it out and no one can!

    For general advice see Troubleshooting issues with iTunes for Windows updates.
    The steps in the second box are a guide to removing everything related to iTunes and then rebuilding it which is often a good starting point unless the symptoms indicate a more specific approach. Review the other boxes and the list of support documents further down page in case one of them applies.
    Your library should be unaffected by these steps but there is backup and recovery advice elsewhere in the user tip.
    tt2

  • Why when I uncheck the McAfee box I am forced to download McAfee to get the latest version of Flash Player? What is the work around here?

    Why when I uncheck the McAfee box I am forced to download McAfee to get the latest version of Flash Player? What is the work around here?

    I received your PM.  Yes, the links have changed:
    Flash Player for ActiveX (Internet Explorer)
    Flash Player Plug-in (All other browsers)
    Flash Player for Mac OS X

  • Code to get the latest version of an entitiy using wcf?

    Hi,
    I am using WCF services to get entities in MDS. I want to get the entities only in the l;atest version. Can you please share code/api asto how to get the latest version of a model in MDS using WCF?
    Thanks

    You can use MetadataGet API to get all versions associated with the model and then sort them by CreatedDateTime or
    UpdatedDateTime depending how you define "the latest version" .
    The code would be like :
    var mdGetRequest = new MetadataGetRequest
    SearchCriteria =
    Models = new Collection<DC.Identifier>()
    new DC.Identifier() {Name = "Account"}
    ResultOptions = {Versions = DC.ResultType.Identifiers},
    // Call MDS service
    MetadataGetResponse mdGetResponse = MyServicesApiUtility.MdsService.MetadataGet(mdGetRequest);
    if (mdGetResponse.OperationResult.Errors.Count == 0 && mdGetResponse.Metadata.Versions.Count != 0)
    var versions = new List<DC.Version>(mdGetResponse.Metadata.Versions);
    versions.Sort(
    (ver1, ver2) => ver2.AuditInfo.CreatedDateTime.CompareTo(ver1.AuditInfo.CreatedDateTime));
    return versions.First();
    else
    throw new Exception(string.Format("No version related to model {0}.", "Account"));

  • I need to get the latest version of Adobe Reader.  Is this still free?  How can I receive automatic upgrades?

    I need to get the latest version of Adobe Reader.  Is this still free?  How can I receive automatic upgrades?

    Of course the free Reader is still available: http://get.adobe.com/reader/
    To get automatic updates set the Updater in the Reader Preferences.

  • How to get the latest versions of Internet Explorer t

    We are running Single Primary Site with SCCM 2012 R2. 
    I
    cant seem to find out how to get the latest versions of Internet Explorer to be included in software updates?  Do you have to download this separately and install manually?

    Like Henrik said, make sure you have the Classifications selected that you want to address. IE 11 is the current browser, that was added to the catalog a few months back. If you are searching for this month you wont find it.

  • Adobe reader xi cannot be 'enabled' once it is installed. FF told me to get the latest version as old version was unsafe.what do i do?

    adobe reader xi cannot be 'enabled' in FF once it is installed. FF told me to get the latest version as old version was unsafe.
    what do i do?
    (win7) (FF18.01).

    There are other things that need your attention.
    Please Update to the latest Firefox 20.0.x version.
    *Help > About Firefox
    *https://support.mozilla.org/kb/update-firefox-latest-version
    *https://support.mozilla.com/kb/Updating+Firefox
    The Firefox Firefox/18.0 version that you currently run is no longer supported with security updates
    *http://kb.mozillazine.org/Adobe_Reader
    *http://get.adobe.com/reader/otherversions/

  • Why Can't I get the latest version of Adobe Muse?!

    After reading countless forums, I came to the conclusion that if I have a 32-bit operating system, I am unable to get the latest version of Muse? Can somebody please confirm this for me or at least give me instruction on how to get the latest version. Currently I have version 7.4 Build 30, CL 784238 AIR Runtime: 17.0.0.144
    I have tried countless workarounds:
    1. Uninstalled & re-installed Adobe Creative Cloud
    2. Deleted the OOBE.db files
    3. Run Adobe Creative Cloud Utility Tool
    4.Manually installing Adobe Muse (separate from the Creative Cloud)

    System requirements | Muse

  • I have downloaded 15 versions of Firefox this week - 8 were on Wednesday. Why can't I get the latest version the first time I download instead of having to download each change even though you make 8 changes in one day. This is getting tiresome.!!!!!

    When I first pull up Firefox it often says I'm not using the latest version. I go to the download page and start the download. Then when its completed and the start page comes up it usually says I don't have the latest version. I went through 8 downloads on Wednesday January 4 before I got the latest. Why didn't I get the latest version the first time I downloaded.?

    Firefox 9.0.1 is the latest release version.
    Which page do you have set as your Homepage or Start Page?
    This - http://www.google.com/firefox - is the old Firefox Start Page used by the Firefox 3.6 and earlier versions of Firefox. Not being maintained by Google and it shows all versions of Firefox as needing to be updated, even pre-release tester versions that are months away from being released to the public at large.
    Starting with the Firefox 4 version, Firefox is using a "local" Start Page with the address of '''about:home'''. It looks similar to the old Start Page, but it isn't exactly the same.

  • I can't get the latest version of iTunes installed on Mac.

    I can't get the latest version of itunes to update correctly. It seems to hang up on installing 2 items, running package scripts.
    I've tried downloading through the browser with no luck too. Suggestions?

    Have you tried removing your existing version of iTunes prior to doing the install, willie? See the following document for instructions:
    Removing iTunes for Mac OS X

  • Cannot install v11.7.700.202 - error stating "get the latest version." WIN 7 Pro 64-bit

    The latest version caused issues with ADP administrators, and we have to go back to previous version.  But I keep getting error with the only options to  "click here" to get the lateste version or Quit the install. Urgh!

    Older versions (standalone installers) are here:  http://helpx.adobe.com/flash-player/kb/archived-flash-player-versions.html

  • I have Photoshop Elements 6.0 for mac. What do I need to do to get the latest version?

    I havePhotoshop Elements 6.0 for mac. What do I need to do to get the latest version?

    See http://www.adobe.com/products/photoshop-elements/buying-guide.displayTab3.html

  • I am running on OS X 10.5.8, how to I get the latest version of iCal??? I have my computer since 2009, I am running on OS X 10.5.8, how to I get the latest version of iCal??? I have my computer since 2009

    I am running on OS X 10.5.8, how to I get the latest version of iCal??? I have my computer since 2009, I am running on OS X 10.5.8, how to I get the latest version of iCal??? I have my computer since 2009

    You need to install a newer Mac OS X version to get a newer version of iCal. If you're using a PowerPC Mac, you can't upgrade the OS or iCal further.
    (60198)

  • How do i get the latest version of safari on my macbook pro? where is this in the apple store??

    How do I get the latest version of safari on my macbook pro?

    Upgrading to Yosemite
    You can upgrade to Yosemite from Lion or directly from Snow Leopard. Yosemite can be downloaded from the Mac App Store for FREE.
    Upgrading to Yosemite
    To upgrade to Yosemite you must have Snow Leopard 10.6.8 or Lion installed. Download Yosemite from the App Store. Sign in using your Apple ID. Yosemite is free. The file is quite large, over 5 GBs, so allow some time to download. It would be preferable to use Ethernet because it is nearly four times faster than wireless.
        OS X Mavericks- System Requirements
          Macs that can be upgraded to OS X Yosemite
             1. iMac (Mid 2007 or newer) - Model Identifier 7,1 or later
             2. MacBook (Late 2008 Aluminum, or Early 2009 or newer) - Model Identifier 5,1 or later
             3. MacBook Pro (Mid/Late 2007 or newer) - Model Identifier 3,1 or later
             4. MacBook Air (Late 2008 or newer) - Model Identifier 2,1 or later
             5. Mac mini (Early 2009 or newer) - Model Identifier 3,1 or later
             6. Mac Pro (Early 2008 or newer) - Model Identifier 3,1 or later
             7. Xserve (Early 2009) - Model Identifier 3,1 or later
    To find the model identifier open System Profiler in the Utilities folder. It's displayed in the panel on the right.
         Are my applications compatible?
             See App Compatibility Table - RoaringApps.

Maybe you are looking for

  • Changing filename of attachment using GOS

    Hi Gurus , I am in the middle of the technical issue related to the  GOS_EXECUTE_SERVICE function module. Everything is working fine but when i am attaching a file from my pc, it's taking the filename as it is. it sounds good. But here comes the prob

  • GoLive on MacBook Pro

    I have been running GoLive on my Powerbooks since v.4.0 with no problems. Now I have an Intel Macbook Pro and updated the OS to Leopard and GoLive remains dormant when launched i.e. the icon appears in the Dock and jumps around for a while then nothi

  • How to check wheather a workflow is assigned to a transcation in CRM

    Hi I want to find out wheather a workflow is assiged to a transcation type. Please let me know all the possiabilites to check. Note: Iam aware of action profile. Incase workflow is not assigned to a action profile what are the other ways to find. Rig

  • Video on background layer is not showing up behind motion graphic.

    Hello Premiere experts, I'm using Premiere 6 on Windows machine and in the last few days I created a motion graphic that has an alpha channel for background transparency and it is placed on video track 2 and it works when I put a static image underne

  • Getting error when inserting Web API/Table Inteface class in Web Template !

    Hi Experts, I am getting error -- Invalid Renderer class: '&1' ZTEST_BWREPORT_ADI while inserting the <param name="ITEM_CLASS" value="ZTEST_BWREPORT_ADI"/> in HTML code of the template, having one table web item, before  <param name="ITEM_CLASS" valu