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. Complex Query

Complex Query

Scheduled Pinned Locked Moved Database
databasequestionalgorithmsxml
3 Posts 2 Posters 1 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.
  • realJSOPR Offline
    realJSOPR Offline
    realJSOP
    wrote on last edited by
    #1

    I have a table called Inventory with the following schema ID, ItemID, Name, Value, CollectedDate All of the columns allow duplicates except ID, so there may be multiple instances of a given Name, but with a different CollectedDate For a given ItemID, I need to return the Value with the latest CollectedDate for each distinct Name. To add to the complexity, I need to put all of the returned items into a single row, using the Name as the column name. So, what I'm starting with is this:

    ID ITEMID NAME VALUE COLLECTEDDATE
    1 12 Test1 abc 01/01/2014 00:00:00
    2 12 Test2 def 01/01/2014 00:00:00
    3 12 Test3 ghi 01/01/2014 00:00:00
    4 12 Test4 jkl 01/01/2014 00:00:00
    5 12 Test1 mno 01/03/2014 00:00:00
    6 12 Test3 stu 01/05/2014 00:00:00
    7 12 Test4 vwx 01/06/2014 00:00:00
    8 12 Test4 yz 01/09/2014 00:00:00
    9 14 Test1 123 01/09/2014 00:00:00

    And my goal is to get to this:

    ITEMID Test1 Test2 Test3 Test4
    12 mno def stu yz
    14 123 NULL NULL NULL

    I created the following temp tables: - @tempNames - contains a list of all of the distinct Names - @tempIDs that contains all of the unique ItemIDs - @tempInventory that contains all of the desired schema shown above. How do I get one row of info for each ItemID to the @tempInventory table? (I don't even know what to call what I'm trying to do. Otherwise, I'd google it).

    ".45 ACP - because shooting twice is just silly" - JSOP, 2010
    -----
    You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
    -----
    When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

    S 1 Reply Last reply
    0
    • realJSOPR realJSOP

      I have a table called Inventory with the following schema ID, ItemID, Name, Value, CollectedDate All of the columns allow duplicates except ID, so there may be multiple instances of a given Name, but with a different CollectedDate For a given ItemID, I need to return the Value with the latest CollectedDate for each distinct Name. To add to the complexity, I need to put all of the returned items into a single row, using the Name as the column name. So, what I'm starting with is this:

      ID ITEMID NAME VALUE COLLECTEDDATE
      1 12 Test1 abc 01/01/2014 00:00:00
      2 12 Test2 def 01/01/2014 00:00:00
      3 12 Test3 ghi 01/01/2014 00:00:00
      4 12 Test4 jkl 01/01/2014 00:00:00
      5 12 Test1 mno 01/03/2014 00:00:00
      6 12 Test3 stu 01/05/2014 00:00:00
      7 12 Test4 vwx 01/06/2014 00:00:00
      8 12 Test4 yz 01/09/2014 00:00:00
      9 14 Test1 123 01/09/2014 00:00:00

      And my goal is to get to this:

      ITEMID Test1 Test2 Test3 Test4
      12 mno def stu yz
      14 123 NULL NULL NULL

      I created the following temp tables: - @tempNames - contains a list of all of the distinct Names - @tempIDs that contains all of the unique ItemIDs - @tempInventory that contains all of the desired schema shown above. How do I get one row of info for each ItemID to the @tempInventory table? (I don't even know what to call what I'm trying to do. Otherwise, I'd google it).

      ".45 ACP - because shooting twice is just silly" - JSOP, 2010
      -----
      You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
      -----
      When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

      S Offline
      S Offline
      Simon_Whale
      wrote on last edited by
      #2

      you are looking for a pivot query[^]

      Every day, thousands of innocent plants are killed by vegetarians. Help end the violence EAT BACON

      realJSOPR 1 Reply Last reply
      0
      • S Simon_Whale

        you are looking for a pivot query[^]

        Every day, thousands of innocent plants are killed by vegetarians. Help end the violence EAT BACON

        realJSOPR Offline
        realJSOPR Offline
        realJSOP
        wrote on last edited by
        #3

        Awesome hint - fingered it out.

        ".45 ACP - because shooting twice is just silly" - JSOP, 2010
        -----
        You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
        -----
        When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

        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