0

So the query below works really well except for one major flaw, the pr.notes field is text and you can't distinct on text as it is not comparable, is there a way for me to still select it?

SELECT * FROM (

    select distinct d.PhoneNum,d.sourcetable,N.FullName,C.fk_applicationid as ref,t.Subject,t.CreatedDate,pr.notes 
    , RANK() OVER ( PARTITION BY  N.FullName ORDER BY t.CreatedDate DESC ) AS iRank


    from Dial d
    join Database.dbo.DM_PhoneNumbers p on p.PhoneNum1 = d.PhoneNum collate latin1_general_CI_AS
    join Database.dbo.DM_PhoneNumbers on p.PhoneNum2 = d.PhoneNum collate latin1_general_CI_AS
    join Database.dbo.DM_ClientApplicants C on C.FK_ClientID = P.FK_ApplicationID
    join Database.dbo.DM_Names N on c.FK_ClientID = N.FK_ApplicationID
    join Database.dbo.Tasks T on T.FK_ApplicationID = c.FK_ApplicationID
    join database.dbo.dm_projects pr on pr.fk_applicationid = T.fk_applicationid
where c.FK_ClientID in (39157,39160)

) AS t 
WHERE t.iRank = 1
2
  • 2
    stackoverflow.com/questions/1028061/… Commented Apr 27, 2015 at 16:26
  • ah well, what is done is done, the way the title is phrased made it hard to search, either way my question is answered. Commented Apr 27, 2015 at 16:33

1 Answer 1

1

Cast pr.notes to VARCHAR(MAX) or NVARCHAR(MAX) depending of data that it holds (I assume that you use SQL Server - it looks that this is the case when I see your code).

    SELECT * FROM (

        select distinct d.PhoneNum,d.sourcetable,N.FullName,C.fk_applicationid as ref,
        t.Subject,t.CreatedDate,CAST(pr.notes AS NVARCHAR(MAX)) AS notes, 
        RANK() OVER ( PARTITION BY  N.FullName ORDER BY t.CreatedDate DESC ) AS iRank

        from Dial d
        join Database.dbo.DM_PhoneNumbers p on p.PhoneNum1 = d.PhoneNum collate latin1_general_CI_AS
        join Database.dbo.DM_PhoneNumbers on p.PhoneNum2 = d.PhoneNum collate latin1_general_CI_AS
        join Database.dbo.DM_ClientApplicants C on C.FK_ClientID = P.FK_ApplicationID
        join Database.dbo.DM_Names N on c.FK_ClientID = N.FK_ApplicationID
        join Database.dbo.Tasks T on T.FK_ApplicationID = c.FK_ApplicationID
        join database.dbo.dm_projects pr on pr.fk_applicationid = T.fk_applicationid
    where c.FK_ClientID in (39157,39160)

    ) AS t 
    WHERE t.iRank = 1;
Sign up to request clarification or add additional context in comments.

3 Comments

as soon as I have the ability too (7 minutes) your getitng the answered on this one :D thank you!
We use app which DB is nightmare - most of the string columns are 'text' datatype. PK are also text fields, so for me was easy to answer such question :). You can also try to change datatype of that column - in our case it works, but you have to test.
tell me about it, the program we work with has so many quirks its unbelievable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.