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. Selecting the latest records

Selecting the latest records

Scheduled Pinned Locked Moved Database
databasealgorithmshelpquestion
5 Posts 3 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.
  • K Offline
    K Offline
    Kevin Brydon
    wrote on last edited by
    #1

    Hi, I have a table that lists the incomes and expenditures of various organizations. Organization | Report Year | Income | Expenditure Org 1 | 1/4/2008 | £20 | £30 Org 1 | 1/4/2007 | £20 | £30 Org 2 | 1/4/2003 | £20 | £30 Org 2 | 1/4/2002 | £20 | £30 Org 2 | 1/4/2001 | £20 | £30 Org 3 | 1/4/2006 | £20 | £30 Org 4 | 1/4/2002 | £20 | £30 I have been trying to write a query that will return the latest record for each organization. So the result would be Org 1 | 1/4/2008 | £20 | £30 Org 2 | 1/4/2003 | £20 | £30 Org 3 | 1/4/2006 | £20 | £30 Org 4 | 1/4/2002 | £20 | £30 Tried searching but came up with no uselful answers. Can someone help? It needs to be compatible with Microsoft Access X|

    B H 2 Replies Last reply
    0
    • K Kevin Brydon

      Hi, I have a table that lists the incomes and expenditures of various organizations. Organization | Report Year | Income | Expenditure Org 1 | 1/4/2008 | £20 | £30 Org 1 | 1/4/2007 | £20 | £30 Org 2 | 1/4/2003 | £20 | £30 Org 2 | 1/4/2002 | £20 | £30 Org 2 | 1/4/2001 | £20 | £30 Org 3 | 1/4/2006 | £20 | £30 Org 4 | 1/4/2002 | £20 | £30 I have been trying to write a query that will return the latest record for each organization. So the result would be Org 1 | 1/4/2008 | £20 | £30 Org 2 | 1/4/2003 | £20 | £30 Org 3 | 1/4/2006 | £20 | £30 Org 4 | 1/4/2002 | £20 | £30 Tried searching but came up with no uselful answers. Can someone help? It needs to be compatible with Microsoft Access X|

      B Offline
      B Offline
      Blue_Boy
      wrote on last edited by
      #2

      here it is : select distinct organisation, (select top 1 t1.rerportyear from myTable as t1 where t1.organisation = myTable.organisation order by rerportyear desc) as reportYear,income,expenditure from myTable


      I Love T-SQL

      1 Reply Last reply
      0
      • K Kevin Brydon

        Hi, I have a table that lists the incomes and expenditures of various organizations. Organization | Report Year | Income | Expenditure Org 1 | 1/4/2008 | £20 | £30 Org 1 | 1/4/2007 | £20 | £30 Org 2 | 1/4/2003 | £20 | £30 Org 2 | 1/4/2002 | £20 | £30 Org 2 | 1/4/2001 | £20 | £30 Org 3 | 1/4/2006 | £20 | £30 Org 4 | 1/4/2002 | £20 | £30 I have been trying to write a query that will return the latest record for each organization. So the result would be Org 1 | 1/4/2008 | £20 | £30 Org 2 | 1/4/2003 | £20 | £30 Org 3 | 1/4/2006 | £20 | £30 Org 4 | 1/4/2002 | £20 | £30 Tried searching but came up with no uselful answers. Can someone help? It needs to be compatible with Microsoft Access X|

        H Offline
        H Offline
        Harini N K
        wrote on last edited by
        #3

        Hi Try this: SELECT Organization, [Report Year], [Income], [Expenditure] FROM Table1 a WHERE [Report Year] >= ( SELECT TOP 1 [Report Year] FROM Table1 b WHERE a.Organization = b.Organization ORDER BY [Report Year] DESC ) ORDER BY Organization Hope you are getting results as expected.

        Harini

        K 1 Reply Last reply
        0
        • H Harini N K

          Hi Try this: SELECT Organization, [Report Year], [Income], [Expenditure] FROM Table1 a WHERE [Report Year] >= ( SELECT TOP 1 [Report Year] FROM Table1 b WHERE a.Organization = b.Organization ORDER BY [Report Year] DESC ) ORDER BY Organization Hope you are getting results as expected.

          Harini

          K Offline
          K Offline
          Kevin Brydon
          wrote on last edited by
          #4

          Thanks for replying blue_boy and harini. I used blue_boys solution in the end and it works as i want it. it actually fits into another much longer sql statement.

          B 1 Reply Last reply
          0
          • K Kevin Brydon

            Thanks for replying blue_boy and harini. I used blue_boys solution in the end and it works as i want it. it actually fits into another much longer sql statement.

            B Offline
            B Offline
            Blue_Boy
            wrote on last edited by
            #5

            Glad to help you... ;)


            I Love T-SQL

            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