Welcome to the dBforums forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact support.

If you prefer not to see double-underlined words and corresponding ads, place your cursor
here for ContentLink opt out.

Go Back  dBforums > Database Server Software > Microsoft SQL Server > Cross Tab Query giving erros

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-08-02, 02:08
vaidyanathanpc vaidyanathanpc is offline
Registered User
 
Join Date: Feb 2002
Posts: 28
Cross Tab Query giving erros

Hi,
I'm having a problem while writing cross tab queries. The fact is I'm getting errors when I execute the query.
The query is as follows.

SELECT skill_Id,
count(CASE final_rating WHEN 5 THEN assoc_name ELSE 0 END) AS "5",
count(CASE final_rating WHEN 4 THEN assoc_name ELSE 0 END) AS "4",
count(CASE final_rating WHEN 3 THEN assoc_name ELSE 0 END) AS "3",
count(CASE final_rating WHEN 2 THEN assoc_name ELSE 0 END) AS "2"
FROM viewfinalrating
GROUP BY skill_id

My purpose is to get the count of the assoc_name for each final_rating grouped by skill_id. When I execute the above query I'm getting the error

Server: Msg 245, Level 16, State 1, Line 1
Syntax error converting the varchar value 'Name1' to a column of data type int.

Name1 is an assoc_name in the view viewfinalrating. Why is SQL server trying to convert the assoc_name to int. Isn't the query supposed to get the count of assoc name based on the CASE statements?

What could be the problem?

Thanks in advance
Regards,
P.C. Vaidyanathan
Reply With Quote
  #2 (permalink)  
Old 05-08-02, 08:47
Paul Young Paul Young is offline
Registered User
 
Join Date: Feb 2002
Location: Houston, TX
Posts: 809
When you use CASE statments you must return like data types. You need to change the "ELSE 0" to a "ELSE '0'" or "Null". However if you change you approach just a bit I think you can achive your goal...

Try
--------------------------------------------------------------------------------------------------
SELECT skill_Id,
sum(CASE final_rating WHEN 5 THEN 1 ELSE 0 END) AS "5",
sum(CASE final_rating WHEN 4 THEN 1 ELSE 0 END) AS "4",
sum(CASE final_rating WHEN 3 THEN 1 ELSE 0 END) AS "3",
sum(CASE final_rating WHEN 2 THEN 1 ELSE 0 END) AS "2"
FROM viewfinalrating
GROUP BY skill_id
--------------------------------------------------------------------------------------------
__________________
Paul Young
(Knowledge is power! Get some!)
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On