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 |
|
ChrisF
Starting Member
2 Posts |
Posted - 2002-03-15 : 15:02:31
|
| I'm somewhat new at developing with SQL Server 2000. I'm coming from a MS Access background. In Access it was pretty simple to build a Crosstab query. I want to do the same thing in SQL Server 2000, using a schema view (rather than a stored procedure) Is this possible?Thanks |
|
|
Jay99
468 Posts |
Posted - 2002-03-15 : 15:17:06
|
yep.Go to books-on-line and search the index for 'cross-tab' . . . post a specific question if that doesn't cover it for you . . .JayEDITquote: These are the statements to create the Pivot table and populate it with the data from the first table:USE NorthwindGOCREATE TABLE Pivot( Year SMALLINT, Quarter TINYINT, Amount DECIMAL(2,1) )GOINSERT INTO Pivot VALUES (1990, 1, 1.1)INSERT INTO Pivot VALUES (1990, 2, 1.2)INSERT INTO Pivot VALUES (1990, 3, 1.3)INSERT INTO Pivot VALUES (1990, 4, 1.4)INSERT INTO Pivot VALUES (1991, 1, 2.1)INSERT INTO Pivot VALUES (1991, 2, 2.2)INSERT INTO Pivot VALUES (1991, 3, 2.3)INSERT INTO Pivot VALUES (1991, 4, 2.4)GO This is the SELECT statement to create the rotated results:SELECT Year, SUM(CASE Quarter WHEN 1 THEN Amount ELSE 0 END) AS Q1, SUM(CASE Quarter WHEN 2 THEN Amount ELSE 0 END) AS Q2, SUM(CASE Quarter WHEN 3 THEN Amount ELSE 0 END) AS Q3, SUM(CASE Quarter WHEN 4 THEN Amount ELSE 0 END) AS Q4FROM Northwind.dbo.PivotGROUP BY YearGO
Edited by - Jay99 on 03/15/2002 15:19:31 |
 |
|
|
AjarnMark
SQL Slashing Gunting Master
3246 Posts |
|
|
ChrisF
Starting Member
2 Posts |
Posted - 2002-03-15 : 17:50:53
|
| I saw that example in the books-on-line. When I tried applying the format to my query, I got an error - something telling me I can't use "CASE." Sorry, I don't have the exact error message. I appreciate the help, though. I will check out that article next.If I continue to run into trouble, I post again. Thanks for the help! |
 |
|
|
|
|
|
|
|