Solved

SQL Statement error - cannot call methods on char. using rank() in subquery"

Posted on 2013-01-29
3
1,704 Views
Last Modified: 2013-01-29
Hi,

We are trying to use a rank() and partition by statement in this SQL statement below in MS SQL but it seems to be failing on the group by line.

The Original Statement is:
SELECT DISTINCT 
Keyword_1.Level_One AS Keyword, 
Keyword_1.Level_One_Desc AS Keyword_Description,
Count(Incident.Incident_Number) AS Count  
FROM ((Incident  LEFT JOIN Keyword_1 ON Incident.Keyword_1=Keyword_1.Keyword left join period ON (Incident.Opened_Date >= period.start and  Incident.Opened_Date <= period.Salesend)) )
WHERE (Incident.Opened_Date >='21 Jan 2013' AND Incident.Opened_Date<='21 Jan 2013 23:59:59')  
GROUP BY Keyword_1.Level_One,Keyword_1.Level_One_Desc  
ORDER BY Count(Incident.Incident_Number) DESC

Open in new window


Below is the New Statement with Rank() and partition subquery to list only unique Incident_numbers  where RANK = 1. The subquery is returning the correct records but I then am unable to group those records as in the original statement.

SELECT DISTINCT
tmp.Keyword_1.Level_One AS Keyword,        
tmp.Keyword_1.Level_One_Desc AS Keyword_Description, 
Count(tmp.Incident_Number) AS Count 
from (
      select incident.*, keyword_1.*, Rank() over  (partition by incident.incident_number order by incident.roll ) as RANK
FROM ((Incident  LEFT JOIN Keyword_1 ON Incident.Keyword_1=Keyword_1.Keyword left join period ON (Incident.Opened_Date >= period.start and  Incident.Opened_Date <= period.Salesend))) 
      WHERE (Incident.Opened_Date >='21 Jan 2013' AND Incident.Opened_Date<='21 Jan 2013 23:59:59')
) tmp
where RANK = 1 
GROUP BY tmp.Keyword_1.Level_One,tmp.Keyword_1.Level_One_Desc
ORDER BY Count(tmp.Incident_Number) DESC

Open in new window


Running the above satement produces the error:

Msg 258, Level 15, State 1, Line 11
Cannot call methods on char.


The error message is complaining about the GROUP BY line, if I comment out the group by line, I get the same “Cannot call methods on char” error on the first line in the select, and then on the second line. The statement will only execute if I remove the first 2 components of the select and just leave the Count(tmp.Incident_number) which is just returning a count.

Can someone suggest a way to be able to still use the rank() and partition statement in a subquery and allow a group by as per the original statement.

Thanks
0
Comment
Question by:jcwiatr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 25

Assisted Solution

by:chaau
chaau earned 250 total points
ID: 38833529
I think you need to convert the statement as per below:

SELECT DISTINCT
tmp.Level_One AS Keyword,        
tmp.Level_One_Desc AS Keyword_Description, 
Count(tmp.Incident_Number) AS Count 
from (
      select incident.Incident_Number as Incident_Number, keyword_1.Level_One as Level_One, keyword_1.Level_One_Desc as Level_One_Desc Rank() over  (partition by incident.incident_number order by incident.roll ) as RANK
FROM ((Incident  LEFT JOIN Keyword_1 ON Incident.Keyword_1=Keyword_1.Keyword left join period ON (Incident.Opened_Date >= period.start and  Incident.Opened_Date <= period.Salesend))) 
      WHERE (Incident.Opened_Date >='21 Jan 2013' AND Incident.Opened_Date<='21 Jan 2013 23:59:59')
) tmp
where RANK = 1 
GROUP BY tmp.Level_One, tmp.Level_One_Desc
ORDER BY Count(tmp.Incident_Number) DESC

Open in new window


Explanation: when you created a subquery tmp the column names in this subquery will not be like "Keyword_1.Level_One". They will most like be just "Level_One". For the conflicting column the engine will auto-generate the column names like "Level_one_1" (if the same column exists in Keyword_1 and Incident). I do not have the whole structures of your tables you used, therefore I have just included into "tmp" those columns you have used in the outer query. I believe it is a good practice anyway. There is no reason to retrieve whole tables Keyword_1 and Incident if you are only going to use a couple of columns.

So, to summarise, the error was in this statement: "tmp.Keyword_1.Level_One"
0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 250 total points
ID: 38833535
Try it this way:
SELECT  tmp.Level_One AS Keyword,
        tmp.Level_One_Desc AS Keyword_Description,
        COUNT(*) AS [Count]
FROM    (SELECT k.Level_One,
                k.Level_One_Desc,
                RANK() OVER (PARTITION BY i.incident_number ORDER BY i.roll) AS [RANK]
         FROM   Incident i
                LEFT JOIN Keyword_1 k ON i.Keyword_1 = k.Keyword
                LEFT JOIN period p ON i.Opened_Date BETWEEN p.start AND p.Salesend
         WHERE  i.Opened_Date BETWEEN '21 Jan 2013' AND '21 Jan 2013 23:59:59'
        ) tmp
WHERE   tmp.[RANK] = 1
GROUP BY tmp.Level_One,
        tmp.Level_One_Desc
ORDER BY [Count] DESC

Open in new window

0
 
LVL 1

Author Closing Comment

by:jcwiatr
ID: 38833634
Thanks to both, I needed to remove the Table refernce in the topmost Select statment and just use the tmp alias to refernce the fields. Acperkins statement ran sucessfully.
0

Featured Post

Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have a large data set and a SSIS package. How can I load this file in multi threading?
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question