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 2000 Forums
 SQL Server Development (2000)
 Crosstab in SQL Svr 2000 schema view

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 . . .

Jay

EDIT

quote:

These are the statements to create the Pivot table and populate it with the data from the first table:

USE Northwind
GO

CREATE TABLE Pivot
( Year SMALLINT,
Quarter TINYINT,
Amount DECIMAL(2,1) )
GO

INSERT 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 Q4
FROM Northwind.dbo.Pivot
GROUP BY Year
GO




Edited by - Jay99 on 03/15/2002 15:19:31
Go to Top of Page

AjarnMark
SQL Slashing Gunting Master

3246 Posts

Posted - 2002-03-15 : 16:09:35
You might also want to read robvolk's awesome article on crosstabs here on SQLTeam.com

------------------------
GENERAL-ly speaking...
Go to Top of Page

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!

Go to Top of Page
   

- Advertisement -