tags:

views:

390

answers:

3

Using the following query:

    SELECT pe.prodtree_element_name_l, MAX(rs.resource_value) AS resource_value
    FROM prodtree_element pe
    LEFT JOIN resource_shortstrings rs
        ON pe.prodtree_element_name_l_rk = rs.resource_key
    WHERE rs.language_id = '5'
        AND pe.prodtree_element_name_l <> ''
    GROUP BY prodtree_element_name_l

I'm trying to figure out how to grab ANY of the "resource_value". The problem being that while this works for a number of other queries, I have one particular table that uses ntext datatypes instead of varchars (which can't utilize the MAX function). So basically, MAX doesn't work here. Is there a substitute I can use on MS SQL Server 2005?

I need the prodtree_element_name_l column grouped, but I only need one value from the resource_value column, and I don't care what it is as most of them are identical regardless (although some are not, hence I can't group that one as well).

UPDATE:

Whoops, I was wrong, prodtree_element_name_l is ALSO an NTEXT. That might help a little :p

A: 
  SELECT pe.prodtree_element_name_l, MAX(CAST(rs.resource_value AS NVARCHAR(MAX))) AS resource_value
    FROM prodtree_element pe
    LEFT JOIN resource_shortstrings rs
        ON pe.prodtree_element_name_l_rk = rs.resource_key
    WHERE rs.language_id = '5'
        AND pe.prodtree_element_name_l <> ''
    GROUP BY prodtree_element_name_l
Dave Markle
A: 

I received the error:

The data types ntext and varchar are incompatible in the not equal to operator.

Unless I missed something?

Edit: CHECK TOP.

Organiccat
Is pe.prodtree_element_name_l also NTEXT? If so maybe you need a CAST on the line with the not-equals condition.
Dave Costa
No, prodtree_element_name_l is an NVARCHAR, resource value is the only NTEXT
Organiccat
Tried adding the cast on the <> line and got this now:An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference.
Organiccat
Did you try my modified version from below?
Mitchel Sellers
I have now, that error is my original:Column 'resource_shortstrings.resource_key' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Organiccat
Are you sure you copied in the proper ()'s? That should work, as it is a subquery that is doing the lookup of the resource_value, therefore not fiolating the group by try the slight modifiation now.
Mitchel Sellers
Yep, same error on the lack of aggregate function and/or missing in the GROUP BY clause.
Organiccat
oops! I had a typo in mine! Try it now!
Mitchel Sellers
Tried it with the same results, however, after removing the GROUP BY, I get my results (albeit, not-unique). Consulting with some guys in the office, the problem is that the GROUP BY automatically might compare strings in the background.
Organiccat
In that case you could use a DISTINCT on the select to get your distinct listing.
Mitchel Sellers
I updated my post to reflect that
Mitchel Sellers
+1  A: 

This will get the first random entry

SELECT DISTINCT 
     pe.prodtree_element_name_l, 
    (SELECT TOP 1 rs2.resource_value
    FROM resource_shortstrings rs2
    WHERE rs2.language_id = '5'
      AND rs2.resource_key = pe.prodtree_element_name_l_rk) AS "resource_value"
FROM prodtree_element pe
LEFT JOIN resource_shortstrings rs
    ON pe.prodtree_element_name_l_rk = rs.resource_key
WHERE rs.language_id = '5'
    AND pe.prodtree_element_name_l IS NOT NULL
--GROUP BY prodtree_element_name_l

NOTE

In your query you aare using a LEFT JOIN but also a filter on the left joined table, therefore limiting the recordset. I LEFT that in place as I figured it would change your results...but there is not point in doing the LEFT JOIN.

EDIT

Based on feedback in comments, I commented out the group by and switched to a distinct

Mitchel Sellers
I got the same result as last time (incompatible data types).
Organiccat
Try the update I just made
Mitchel Sellers