Better way to write this SQL

Posted by AngryHacker on Stack Overflow See other posts from Stack Overflow or by AngryHacker
Published on 2010-04-10T18:45:46Z Indexed on 2010/04/10 18:53 UTC
Read the original article Hit count: 302

Filed under:
|
|

I have the following table:

create table ARDebitDetail(ID_ARDebitDetail int identity, 
                  ID_Hearing int, ID_AdvancedRatePlan int)

I am trying to get the latest ID_AdvancedRatePlan based on a ID_Hearing. By latest I mean with the largest ID_ARDebitDetail. I have this query and it works fine.

    select ID_AdvancedRatePlan
    from ARDebitDetails
    where ID_Hearing = 135878
    and ID_ARDebitDetail = 
            (   select max(ID_ARDebitDetail) 
                from ARDebitDetails 
                where ID_AdvancedRatePlan > 0 and ID_Hearing = 135878
            )

However, it just looks ugly and smells bad. Is there a way to rewrite it in a more concise manner?

© Stack Overflow or respective owner

Related posts about sql-server-2005

Related posts about refactor