Microsoft SQL Server
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesMicrosoft SQL Server

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old October 28th, 2004, 12:10 PM
ddz ddz is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 2 ddz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Need Help with Crosstab Queries

I'm a database newbie and I need to create queries (I think crosstabs) using SQL Server. One query needs to take a count and another needs to take a sum. I've never done these type of queries before, so I tried doing them first in Access using the wizard. The queries work just fine in Access. So I copied the SQL code that was generated in Access and pasted it as a stored procedure in SQL Server. But I'm running into several problems that I don't know how to fix.

Here's what my table (ContactMethods) looks like:
(I used dashes so that the data would line up under the column.)

ID- ContactHelpDesk
1 - email
2 - walk
3 - form
4 - phone
5 - email
6 - walk
7 - form

My ultimate goal is to be able to find out the total number of people who selected email, walk, form, and phone (in the ContactHelpDesk field). I also need to have email, walk, form, and phone to be column headings. I need it in this format so that I can take a sum for each column and refer to the column by name when using ColdFusion (a scripting language).

My first query (called ContactHelpDeskCrosstab) does a count:

TRANSFORM Count(ContactMethods.ContactStudent) AS CountOfContactStudent
SELECT ContactMethods.ID, Count(ContactMethods.ContactStudent) AS [Total Of ContactStudent]
FROM ContactMethods
GROUP BY ContactMethods.ID
PIVOT ContactMethods.ContactHelpDesk;


The result looks like this:

ID- Total of ContactStudent- email- form- phone- walk
1 - 1 ---------------------- 1
2 - 1 ------------------------------------------ 1
3 - 1 ----------------------------- 1
4 - 1 ----------------------------------- 1
5 - 1 ---------------------- 1
6 - 1 ------------------------------------------ 1
7 - 1 ----------------------------- 1

This is exactly what I need so that I can take a sum.

Next, I take a sum for each column (email, form, phone, walk). Here's the query that takes the sum:

SELECT DISTINCTROW Sum(ContactHelpDeskCrosstab.email) AS EmailTotal, Sum(ContactHelpDeskCrosstab.form) AS FormTotal, Sum(ContactHelpDeskCrosstab.phone) AS PhoneTotal, Sum(ContactHelpDeskCrosstab.walk) AS WalkTotal
FROM ContactHelpDeskCrosstab;


The result looks like this:

EmailTotal- FormTotal- PhoneTotal- WalkTotal
2 --------- 2 -------- 1 ---------- 2

This is exactly what I need. Now that it is in this format, I will be able to use ColdFusion to print out the total for each answer.

However, when I copy the SQL code generated in Access from the first query and paste it to a new stored procedure, I receive two error messages from SQL Server:
  • Incorrect syntax near TRANSFORM.
  • Incorrect syntax near PIVOT.
When I copy the SQL gode generated in Access from the second query and paste it to a new stored procedure, I receive this error message from SQL Server:
  • Incorrect syntax near ContactHelpDeskCrosstab. (I'm not sure if something is wrong with the syntax or if it's throwing that message because it can't find a query by that name. As I mentioned, it won't let me create that query because of the syntax problem.)
I'd appreciate any help with how I can fix these queries so that they will work with SQL Server. Thanks.
Attached Files
File Type: doc CountandSumQueries.doc (54.5 KB, 285 views)

Reply With Quote
  #2  
Old November 11th, 2004, 09:42 PM
smartbs smartbs is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Location: Michigan
Posts: 7 smartbs User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
SQL Server Does not support Crosstab Queries

Unfortunately SQL Server 2000 does not support crosstab queries like Access.
There are a few freeware procedures that claim they mimc the crosstab query behavior
but I have not used them.

This is why you get errors with keywords Transform etc.

Depending on what exactly you are doing
you may have to use temp tables or write a series of queries to get the information you need.
I.e. I would suggest going back to the drawing board....or link the tables into access database and do your query there.

Smartbs
www.accessneeds.com

Reply With Quote
  #3  
Old December 9th, 2004, 11:55 AM
Harold Harold is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 2 Harold User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Crosstab queries in SQL Server

I had the same problem. I simply Linked a front end Access database to it, used the same tables (now in Access). I still had a problem doing crosstabs with a parameter but if you don't have any parameters it is easier.

I then created a make-table query with my parameters and then based my crosstab query on the newly created table. If you do not have any parameters to address simply create the crosstab query using the tables in Access.

Harold

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesMicrosoft SQL Server > Need Help with Crosstab Queries


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT