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. Problems creating View

Problems creating View

Scheduled Pinned Locked Moved Database
databasesql-serversysadminhelptutorial
3 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.
  • X Offline
    X Offline
    xfitr2
    wrote on last edited by
    #1

    DB: SQL Server 2000 I am having trouble writing a SQL statement to pull the most current data about a record out of a table and would appreciate if someone could shed some light on the situation. Example tables below: **tbl_Item ItemID** Color Type CreatedDate CreatedBy **tbl_ItemValue ValueID** ItemID Value UpdatedDate UpdatedBy Everytime the value changes for an item, as new record is created in the tbl_ItemValue table. I need to create a query that joins the two tables and shows the most current value for each item in the tbl_Item table. Most current value would be the record with the most current UpdatedDate that has the same ItemID in tbl_ItemValue. Therefore, my view should look something like this: **vw_Item_Value_Current ItemID** Color Type CreatedDate CreatedBy Value UpdatedDate UpdatedBy Any help would greatly be appreciated.

    E 1 Reply Last reply
    0
    • X xfitr2

      DB: SQL Server 2000 I am having trouble writing a SQL statement to pull the most current data about a record out of a table and would appreciate if someone could shed some light on the situation. Example tables below: **tbl_Item ItemID** Color Type CreatedDate CreatedBy **tbl_ItemValue ValueID** ItemID Value UpdatedDate UpdatedBy Everytime the value changes for an item, as new record is created in the tbl_ItemValue table. I need to create a query that joins the two tables and shows the most current value for each item in the tbl_Item table. Most current value would be the record with the most current UpdatedDate that has the same ItemID in tbl_ItemValue. Therefore, my view should look something like this: **vw_Item_Value_Current ItemID** Color Type CreatedDate CreatedBy Value UpdatedDate UpdatedBy Any help would greatly be appreciated.

      E Offline
      E Offline
      Eric Dahlvang
      wrote on last edited by
      #2

      Here's one way:

      select * from tbl_Item itm
      inner join tbl_ItemValue val on itm.itemid = val.itemid
      inner join (Select max(updateddate) as MaxDate, itemid
      from tbl_itemValue
      group by itemid) as val2 on val.UpdatedDate = val2.MaxDate and val.ItemID = val2.ItemID

      --EricDV Sig--------- Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peters

      X 1 Reply Last reply
      0
      • E Eric Dahlvang

        Here's one way:

        select * from tbl_Item itm
        inner join tbl_ItemValue val on itm.itemid = val.itemid
        inner join (Select max(updateddate) as MaxDate, itemid
        from tbl_itemValue
        group by itemid) as val2 on val.UpdatedDate = val2.MaxDate and val.ItemID = val2.ItemID

        --EricDV Sig--------- Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peters

        X Offline
        X Offline
        xfitr2
        wrote on last edited by
        #3

        Thanks for the help. That worked.

        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