0

I have a problem and can't find a way to solve it.

This is my part of my query that I cannot solve.

   Select Datepart(day,date) as Day, 
          Department,SUM(hourly) as workhours
     from MIVDb.dbo.evid as p 
     Inner Join MIVdb.dbo.workers as r 
        On p.rad=r.rad
    Group by Department,
             Datepart(day,date)

Nothing special about it. My problem is that I get table looking like

   Days   Dept     Hours
    1     Sales    8
    2     Cust     10

And i need:

     Dept   1  2 ....etc
     Sales  8 10 ....etc

So I need days to be columns. Problem is that Days column is dynamic, it can have on day, of 20 day, it can be week or even month. Can someone point help me with this.

Christian Phillips
  • 18,399
  • 8
  • 53
  • 82
  • 1
    http://sqlserveradvisor.blogspot.in/2009/03/sql-server-convert-rows-to-columns.html – rags Jul 30 '13 at 11:01
  • 1
    possible duplicate of [Simple way to transpose columns and rows in Sql?](http://stackoverflow.com/questions/13372276/simple-way-to-transpose-columns-and-rows-in-sql) – Roman Pekar Jul 30 '13 at 11:01
  • 1
    Duplicate: http://stackoverflow.com/questions/1187460/sql-rows-to-columns – rags Jul 30 '13 at 11:01
  • 1
    take a look at **Pivot** http://blogs.msdn.com/b/spike/archive/2009/03/03/pivot-tables-in-sql-server-a-simple-sample.aspx – Christian Phillips Jul 30 '13 at 11:03

0 Answers0