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. certain rows to column

certain rows to column

Scheduled Pinned Locked Moved Database
databasesql-serversysadmintutorial
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.
  • R Offline
    R Offline
    Richard Berry100
    wrote on last edited by
    #1

    Hi Using SQL Server 2008 R2. I have a stock table, with the following fields: warehouse, product, long_description, physical_qty. All products exist in warehouse '01'. Some of the products exist in warehouse '03' Faulty (or NG) goods get transferred to warehouse 03. I need a query to show distinct(product), long_description, physical_qty (for warehouse 01), physical_qty (for warehouse 03) Example data: warehouse product long_description physical_qty 01 00-00001 Item 1 100 03 00-00001 Item 1 5 01 00-00002 Item 2 200 Desired Result: Product Description Stock_Qty NG_Qty 00-00001 Item 1 100 5 00-00002 Item 2 200 I have tried this, but don't get distinct products (i.e. Item 1 appears in two rows)

    Select distinct(product), long_description, case warehouse when '01' then physical_qty else 0 end as [Stock_Qty], case warehouse when '03' then physical_qty else 0 end as [NG_Qty] from vektron2.scheme.stockm group by product, warehouse, long_description, physical_qty order by product

    M 1 Reply Last reply
    0
    • R Richard Berry100

      Hi Using SQL Server 2008 R2. I have a stock table, with the following fields: warehouse, product, long_description, physical_qty. All products exist in warehouse '01'. Some of the products exist in warehouse '03' Faulty (or NG) goods get transferred to warehouse 03. I need a query to show distinct(product), long_description, physical_qty (for warehouse 01), physical_qty (for warehouse 03) Example data: warehouse product long_description physical_qty 01 00-00001 Item 1 100 03 00-00001 Item 1 5 01 00-00002 Item 2 200 Desired Result: Product Description Stock_Qty NG_Qty 00-00001 Item 1 100 5 00-00002 Item 2 200 I have tried this, but don't get distinct products (i.e. Item 1 appears in two rows)

      Select distinct(product), long_description, case warehouse when '01' then physical_qty else 0 end as [Stock_Qty], case warehouse when '03' then physical_qty else 0 end as [NG_Qty] from vektron2.scheme.stockm group by product, warehouse, long_description, physical_qty order by product

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      You need to work through this Pivot two or more columns in SQL Server 2005[^]article blatant plug of own article

      Never underestimate the power of human stupidity RAH

      R 1 Reply Last reply
      0
      • M Mycroft Holmes

        You need to work through this Pivot two or more columns in SQL Server 2005[^]article blatant plug of own article

        Never underestimate the power of human stupidity RAH

        R Offline
        R Offline
        Richard Berry100
        wrote on last edited by
        #3

        Thanks Mycroft - will give it a try tonight

        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