Question

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

Was it helpful?

Solution

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

OTHER TIPS

  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

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top