Hi all,
In MyDatabase, I have a TABLE dbo.LabData created by the following SQLQuery.sql:
USE MyDatabase
GO
CREATE TABLE dbo.LabResults
(SampleID int PRIMARY KEY NOT NULL,
SampleName varchar(25) NOT NULL,
AnalyteName varchar(25) NOT NULL,
Concentration decimal(6.2) NULL)
GO
--Inserting data into a table
INSERT dbo.LabResults (SampleID, SampleName, AnalyteName, Concentration)
VALUES (1, 'MW2', 'Acetone', 1.00)
INSERT … ) VALUES (2, 'MW2', 'Dichloroethene', 1.00)
INSERT … ) VALUES (3, 'MW2', 'Trichloroethene', 20.00)
INSERT … ) VALUES (4, 'MW2', 'Chloroform', 1.00)
INSERT … ) VALUES (5, 'MW2', 'Methylene Chloride', 1.00)
INSERT … ) VALUES (6, 'MW6S', 'Acetone', 1.00)
INSERT … ) VALUES (7, 'MW6S', 'Dichloroethene', 1.00)
INSERT … ) VALUES (8, 'MW6S', 'Trichloroethene', 1.00)
INSERT … ) VALUES (9, 'MW6S', 'Chloroform', 1.00)
INSERT … ) VALUES (10, 'MW6S', 'Methylene Chloride', 1.00)
INSERT … ) VALUES (11, 'MW7', 'Acetone', 1.00)
INSERT … ) VALUES (12, 'MW7', 'Dichloroethene', 1.00)
INSERT … ) VALUES (13, 'MW7', 'Trichloroethene', 1.00)
INSERT … ) VALUES (14, 'MW7', 'Chloroform', 1.00)
INSERT … ) VALUES (15, 'MW7', 'Methylene Chloride', 1.00)
INSERT … ) VALUES (16, 'TripBlank', 'Acetone', 1.00)
INSERT … ) VALUES (17, 'TripBlank', 'Dichloroethene', 1.00)
INSERT … ) VALUES (18, 'TripBlank', 'Trichloroethene', 1.00)
INSERT … ) VALUES (19, 'TripBlank', 'Chloroform', 0.76)
INSERT … ) VALUES (20, 'TripBlank', 'Methylene Chloride', 0.51)
GO
A desired Pivot Table is like:
MW2 MW6S MW7 TripBlank
Acetone 1.00 1.00 1.00 1.00
Dichloroethene 1.00 1.00 1.00 1.00
Trichloroethene 20.00 1.00 1.00 1.00
Chloroform 1.00 1.00 1.00 0.76
Methylene Chloride 1.00 1.00 1.00 0.51
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
I write the following SQLQuery.sql code for creating a Pivot Table from the Table dbo.LabData by using the PIVOT operator:
USE
MyDatabaseGO
USE
TABLE dbo.LabDataGO
SELECT
AnalyteName, [1] AS MW2,FROM
(SELECT SampleName, AnalyteName, Concentration FROM dbo.LabData) pPIVOT
( SUM (Concentration) FOR AnalyteName IN ([1],ORDER
BY SampleNameGO
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
I executed the above-mentioned code and I got the following error messages:
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'TABLE'.
Msg 207, Level 16, State 1, Line 1
Invalid column name 'AnalyteName'.
I do not know what is wrong in the code statements of my SQLQuery.sql. Please help and advise me how to make it right and work for me.Thanks in advance,
Scott Chang

SSMS Express: Using PIVOT operator to Create Pivot Table - Error Messages 156 & 207
New Man
--
Adam Machanic
Pro SQL Server 2005, available now
http://www..apress.com/book/bookDisplay.html bID=457
--
Keandre
Adam,
Is OPENROWSET supported in the Express version
If it is then that would be a way to import access,text, excel and csv files without upgrading to SQL Server 2005 Standard Edition
Denis the SQL Menace
http://sqlservercode.blogspot.com/Frank Palinkas
--
Adam Machanic
Pro SQL Server 2005, available now
http://www..apress.com/book/bookDisplay.html bID=457
--
A fok
USE TABLE dbo.LabData
GO
You can just remove that -- there is no USE TABLE syntax in T-SQL.
Second, I think you need to re-write your pivot query to the following:
SELECT
AnalyteName, [MW2], [MW6S], [MW7], [TripBlank]FROM
(
SELECT SampleName, AnalyteName, ConcentrationFROM
dbo.LabResults) pPIVOT
(
SUM
(Concentration)FOR
SampleName IN ([MW2], [MW6S], [MW7], [TripBlank]))
AS pvtORDER
BY AnalyteNameGO
--
Adam Machanic
Pro SQL Server 2005, available now
http://www..apress.com/book/bookDisplay.html bID=457
--
hivesoft
Hi Adam, Thank you very much for your valuable response and corrections on my code.
The revised code works nicely like a charm!!!
I am a chemist and I deal with lots of chemical data frequently. Currently, I get the electronic data in the extensions of .mdb, .xls or .csv from commercial chemical laboratories in USA and I feed into the Microsoft Access 2003/Excel 2003 based software programs for precessing. Now, the Express Edition and other editions of Visual Studio 2005 use the data with the .mdf type of data. To input the .mdf type of data to me is very hard manually as I tried recently. I have wondered for quite a while about how to input the .mdf type of data electronically. I like to ask you the following 2 questions:
(1) Is it possible to convert the electronic data with the extensions of .mdb, .xls or .cvs to the .mdf typed data
(2) Can we convert the .mdb, .xls or .csv typed data to the XML typed data Can we convert the XML typed data to the .mdf typed data
Please advise.
Thanks again,
Scott Chang