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.
| Author |
Topic |
|
fisab68
Starting Member
1 Post |
Posted - 2005-03-21 : 04:57:57
|
| I've come up with a way of generating a crosstab that I can apply a filter to. It uses a cursor to build up an SQL statement.Is this a poor way to do it ?Example of it using the Northwind DB :-Declare@year nvarchar(5),@sql nvarchar(1000)set @sql = 'SELECT MONTH(OrderDate) AS OrderMonth'declare den_name CURSOR forselect distinct year(orderDate) from ordersopen den_namefetch next from den_name into @yearwhile @@fetch_status = 0beginset @sql = @sql + ',SUM(CASE YEAR(OrderDate) WHEN ' + @year + ' THEN 1 ELSE 0 END) AS c' + @yearfetch next from den_name into @yearendclose den_namedeallocate den_nameset @sql = @sql + ' FROM OrdersGROUP BY MONTH(OrderDate)ORDER BY MONTH(OrderDate)'exec sp_executesql @sql |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
|
|
|
|
|