Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Select unique column???

Select unique column???

Scheduled Pinned Locked Moved Database
helpquestion
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Doan Quynh
    wrote on last edited by
    #1

    I have a table : ID ___ IDUser ___ Year 1 ___ 1 ___ 2005 2___ 1 ___ 2007 3 ___ 1___ 2003 4___ 2___ 2008 5 ___ 2___ 2005 I want to filt : unique IDUser with Max year : ID ___ IDUser ___ Year 2___ 1 ___ 2007 4 ___ 2 ___ 2008 Please help me. Thanks.

    C 1 Reply Last reply
    0
    • D Doan Quynh

      I have a table : ID ___ IDUser ___ Year 1 ___ 1 ___ 2005 2___ 1 ___ 2007 3 ___ 1___ 2003 4___ 2___ 2008 5 ___ 2___ 2005 I want to filt : unique IDUser with Max year : ID ___ IDUser ___ Year 2___ 1 ___ 2007 4 ___ 2 ___ 2008 Please help me. Thanks.

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      Something like this:

      SELECT o.ID, s.IDUser, s.Year
      FROM MyTable AS o
      INNER JOIN (SELECT IDUser, MAX(Year)
      FROM MyTable
      GROUP BY Year) AS s
      ON o.IDUser = s.IDUser AND o.Year = s.Year


      Upcoming events: * Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ... * Reading: Developer Day 5 Ready to Give up - Your help will be much appreciated. My website

      D 2 Replies Last reply
      0
      • C Colin Angus Mackay

        Something like this:

        SELECT o.ID, s.IDUser, s.Year
        FROM MyTable AS o
        INNER JOIN (SELECT IDUser, MAX(Year)
        FROM MyTable
        GROUP BY Year) AS s
        ON o.IDUser = s.IDUser AND o.Year = s.Year


        Upcoming events: * Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ... * Reading: Developer Day 5 Ready to Give up - Your help will be much appreciated. My website

        D Offline
        D Offline
        Doan Quynh
        wrote on last edited by
        #3

        Error s: SELECT IDUser, MAX(Year) FROM MyTable GROUP BY Year Exception : "Column IDUser is invalid in the select list because it is not contained in erther an aggregate function or the Group by clause"???

        1 Reply Last reply
        0
        • C Colin Angus Mackay

          Something like this:

          SELECT o.ID, s.IDUser, s.Year
          FROM MyTable AS o
          INNER JOIN (SELECT IDUser, MAX(Year)
          FROM MyTable
          GROUP BY Year) AS s
          ON o.IDUser = s.IDUser AND o.Year = s.Year


          Upcoming events: * Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ... * Reading: Developer Day 5 Ready to Give up - Your help will be much appreciated. My website

          D Offline
          D Offline
          Doan Quynh
          wrote on last edited by
          #4

          SELECT o.ID, s.IDUser, s.Year FROM MyTable AS o INNER JOIN (SELECT IDUser, MAX(Year) FROM MyTable GROUP BY IDUser) AS s ON o.IDUser = s.IDUser AND o.Year = s.Year It is OK. Thanks you very much !

          C 1 Reply Last reply
          0
          • D Doan Quynh

            SELECT o.ID, s.IDUser, s.Year FROM MyTable AS o INNER JOIN (SELECT IDUser, MAX(Year) FROM MyTable GROUP BY IDUser) AS s ON o.IDUser = s.IDUser AND o.Year = s.Year It is OK. Thanks you very much !

            C Offline
            C Offline
            Colin Angus Mackay
            wrote on last edited by
            #5

            Ah... Sorry - I mistyped the query. Glad you figured it out.


            Upcoming events: * Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ... * Reading: Developer Day 5 Ready to Give up - Your help will be much appreciated. My website

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups