Help with query

  • Hi,

    I’ve got a query I’m working on and need some help.

    Below is the query and what it does is assigns a sequence number to the line based on the designated partition. What I want to be able to do now is only return those line items that = 2 for the resulting linenumber sequence.

    Any ideas? I trying wrapping it in a having clause but that’s not workinguse [SampleDB]

    SELECT LocationID, LanguageID ,LocationLanguageRecID,

    row_number() over (partition by LOCATIONID

    order by LocationLanguageRecID) LineNumber

    FROM LocationLanguages

  • Have you tried to use a subquery or CTE?

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Hi Luis,

    Not sure how I would do that..

  • SELECT * FROM

    (

    SELECT LocationID, LanguageID ,LocationLanguageRecID,

    row_number() over (partition by LOCATIONID

    order by LocationLanguageRecID) LineNumber

    FROM LocationLanguages

    ) X

    WHERE LineNumber = 2

    "I cant stress enough the importance of switching from a sequential files mindset to set-based thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code."

    -- Itzik Ben-Gan 2001

  • Thanks Alan. Much appreciated.

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply