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. PIVOT in SQL 2005 problem

PIVOT in SQL 2005 problem

Scheduled Pinned Locked Moved Database
databasecsharpsql-serversysadminhelp
1 Posts 1 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.
  • H Offline
    H Offline
    Hakan Nilsson k
    wrote on last edited by
    #1

    I'm trying to use PIVOT instead of CASE but can't make it, what's wrong with the pivot query below? create table t1 (a int, b char(5)) go insert t1 values(1, 'SVE') insert t1 values(1, 'FIN') insert t1 values(1, 'DAN') insert t1 values(2, 'SVE') insert t1 values(2, 'FIN') insert t1 values(3, 'SVE') select * from t1 a b ----------- ----- 1 SVE 1 FIN 1 DAN 2 SVE 2 FIN 3 SVE (6 row(s) affected) -- SQL 2000 with case works select a, max(case when b='SVE' then b else '' end) as b_sve, max(case when b='FIN' then b else '' end) as b_fin, max(case when b='DAN' then b else '' end) as b_dan from t1 group by a a b_sve b_fin b_dan ----------- ----- ----- ----- 1 SVE FIN DAN 2 SVE FIN 3 SVE (3 row(s) affected) -- SQL 2005 with pivot, this select doesn't work! select a, isnull(['SVE'], '') as b_sve, isnull(['FIN'], '') as b_fin, isnull(['DAN'], '') as b_dan from (select a, b from t1) p pivot ( max(b) for b in ( ['SVE'], ['FIN'], ['DAN'] ) ) pvt; a b_sve b_fin b_dan ----------- ----- ----- ----- 1 2 3 (3 row(s) affected)

    Håkan Nilsson -- Swedish developer, DBA, C#, SQL Server, soccer trainer, old C64 player

    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