Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Decode

Author  Topic 

neeraj1401
Starting Member

36 Posts

Posted - 2009-06-02 : 05:22:39
I have a table status_history in which has the following data
id prior_statusId new_status_id date_modified
94535 NULL 1 2008-01-29 10:07:54.557
94535 1 2 2008-04-29 14:12:13.590
94535 2 3 2008-08-30 15:35:12.000
94535 3 4 2008-010-30 15:56:07.140
94535 4 5 2008-012-30 16:09:50.423

I want to write a query which will give me following result

Id status_on_31_03_2008 status_on_31_06_2008 status_on_31_09_2008 status_on_31_12_2008
94535 1 2 3 4

thanks in advance
Neeraj

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-06-02 : 05:46:09
You will have to use dynamic SQL for this.
See www.sommarskog.se


E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

neeraj1401
Starting Member

36 Posts

Posted - 2009-06-02 : 05:49:56
thanks for reply.
I can not not use dynamic query as I have only select grant on database i cannot create any object.
Go to Top of Page

SQLRatankalwa
Starting Member

42 Posts

Posted - 2009-06-02 : 05:52:19
Hi

Please look at this example:

You can use PIVOT to do this.

This should solve your problem.

http://www.logiclabz.com/sql-server/transpose-rows-to-columns-in-sql-server-2005-using-pivot.aspx


Ratan Kalwa
SQL Server Professionals User Group

http://www.revalsys.com
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-06-02 : 12:35:15
quote:
Originally posted by SQLRatankalwa

Hi

Please look at this example:

You can use PIVOT to do this.

This should solve your problem.

http://www.logiclabz.com/sql-server/transpose-rows-to-columns-in-sql-server-2005-using-pivot.aspx


Ratan Kalwa
SQL Server Professionals User Group

http://www.revalsys.com


only if your date values are static otherwise you need to use dynamic sql as suggested
Go to Top of Page
   

- Advertisement -