473,396 Members | 2,038 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

SQL Server 2005 Pivot Table

Hello generous SQL experts!

I'm new to the SQL Server environment, but am trying to get up to speed. I am trying to help a coworker create a pivot table for use with an Infragistics chart. I have data in the form of:

[Timestamp] | GYR_ID | RCOUNT

[Timestamp] is a smalldatetime
GYR_ID is an int which represents a category (1=green, 2=yellow, 3=red)
RCOUNT is how many of that category are present

For example at [TIMESTAMP] = 10/13/2008 12:32 PM there are three records

[Timestamp] , GYR_ID , RCOUNT
10/13/2008 12:32:00 PM , 1, 4
10/13/2008 12:32:00 PM , 2, 5
10/13/2008 12:32:00 PM , 3, 6

The above indicates that there are 4 green, 5 yellow and 3 red records.


I need to pivot this data so I have an output of

[Timestamp] | Green | Yellow | Red
10/13/2008 12:32:00 PM, 4 5 6

I'm having difficulty troubleshooting my pivot stored procedure. This is what I have:

Expand|Select|Wrap|Line Numbers
  1. BEGIN
  2.     -- SET NOCOUNT ON added to prevent extra result sets from
  3.     -- interfering with SELECT statements.
  4.     SET NOCOUNT ON;
  5.  
  6.     SELECT [TIMESTAMP], [1] AS Green, [2] AS Yellow, [3] AS Red
  7.     FROM 
  8.      (SELECT [TIMESTAMP], GYR_ID, RCOUNT
  9.         FROM dbo.HISTORIC_STOPLIGHT
  10.         WHERE PROGRAM=@PROGRAM) AS p
  11.     PIVOT
  12.     (
  13.         SUM(RCOUNT) FOR [TIMESTAMP] IN ( [1], [2], [3] )
  14.     ) AS pvt
  15.     ORDER BY [TIMESTAMP]
  16. END
I am getting errors on the line 13 with
Expand|Select|Wrap|Line Numbers
  1. SUM(RCOUNT) FOR [TIMESTAMP] IN( [1],[2],[3] )
The error messages read:
1. Error converting data type nvarchar to smalldatetime.
2. The incorrect value "1" is supplied in the PIVOT operator.
3. Invalid column name 'TIMESTAMP'.

Any help/advise you can give me would be great.

Thanks in advance!

Nickvans
Oct 15 '08 #1
1 6279
I figured out my problem. I was using the wrong Pivot arguments. The correct code for what I was trying to do is:
Expand|Select|Wrap|Line Numbers
  1.     SELECT [TIMESTAMP], [1] AS Green, [2] AS Yellow, [3] AS Red
  2.     FROM 
  3.      (SELECT [TIMESTAMP], GYR_ID, RCOUNT
  4.         FROM dbo.HISTORIC_STOPLIGHT
  5.         WHERE PROGRAM=@PROGRAM) AS p
  6.     PIVOT
  7.     (
  8.         MAX(RCOUNT) FOR [GYR_ID] IN ( [1], [2], [3] )
  9.     ) AS pvt
  10.     ORDER BY [TIMESTAMP]
Thanks anyway. Perhaps this will help someone else down the road.

Nickvans
Oct 15 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Rob | last post by:
I'm just getting around to using pivot tables and charts. I find the Pivot table interface to be INCREDIBLY frustrating. When I view a table in Design view, then choose Pivot table view, I get...
1
by: Grey | last post by:
I have created a asp.net form for user to input data. After input the data, user need to click a button to export the input data to excel for data analysis with excel pivot table function. is it...
9
by: billmiami2 | last post by:
I was playing around with the new SQL 2005 CLR functionality and remembered this discussion that I had with Erland Sommarskog concerning performance of scalar UDFs some time ago (See "Calling...
9
by: PeteCresswell | last post by:
I've got something called "Reference Rates". The idea is that on a given day, we have various rates of return for various entities. e.g. Libor 3-month return, Libor 6-month return, US Treasury...
9
by: dotnetguru | last post by:
Hi SMART GUYS, Please help me write a query. Actually I want to convert my rows into columns. Can anyone kindly give me the query to do it? My rows are about employees. There can be any number of...
5
by: Pourya99 | last post by:
Hello, I have an Access Data Access Page which has a pivot table. The data source of the pivot table is a SQL database table. The data in the pivot table itself is not a problem. I have a text...
0
by: SteveM | last post by:
I have created a form in Pivot Table View. The form runs fine on all local workstations but does not show any data on Server 2003. Anyone know of any issues with Server 2003 and Access forms in...
0
by: ravindarjobs | last post by:
Hello friends, I am using ms access 2003, and Vb6. i am displaying a table in a form as pivot table (following "Auto Form: Pivot Table" Wizard) Usually i select the items i want to be...
18
by: smckibbe01 | last post by:
I have Pivot Table query in MS Access that I would like to run in SQL Server 2005. Can anyone help me to convert the syntax over to SQL Server. The following is the query in MS Access: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.