You are here: Re: Transpose Rows to Columns ? « MsSQL Server « IT news, forums, messages
Re: Transpose Rows to Columns ?

Posted by Serge Rielau on 03/30/06 03:11

bogtom@gmail.com wrote:
> I have records
>
> ID Sku Name Date
> 2 41 Blair 01/04/03
> 3 45 John 03/04/03
>
> that should look like...
>
> ID 2 3
> Sku 41 45
> Name Blair John
> ....
>
> and so on. Number of source rows will be fixed (12) so no of target
> columns will be 12 too.
>
> Anyone know of a quick way to do this via simple SQL ?
>
This should work on all RDBMS:
CREATE TABLE Sales(Year INTEGER,
Quarter INTEGER,
Results INTEGER);
SELECT Year,
MAX(CASE WHEN Quarter = 1
THEN Results END) AS Q1,
MAX(CASE WHEN Quarter = 2
THEN Results END) AS Q2,
MAX(CASE WHEN Quarter = 3
THEN Results END) AS Q3,
MAX(CASE WHEN Quarter = 4
THEN Results END) AS Q4
FROM Sales
GROUP BY Year

--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация