Row_Number question

  • Here is a CTE query

    With mstrTable(ItemNo, Sales)

    as (

    Query1

    Union All

    Query2

    )

    Select Row_Number () Over(Partition by ItemNo order by ItemNo)as RowNo, ItemNo, Sales

    From mstrTable

    order by ItemNo

    The results from Query1 and Query2 overlap sometimes.

    The result set looks like:

    1 Item1 10000

    2 Item1 10000

    1 Item2 20000

    1 Item3 30000

    I only want the first occurrence of each item. The desired result set is:

    1 Item1 10000

    1 Item2 20000

    1 Item3 30000

    I am not able to add a "Where RowNo = 1" to the query. SQL returns an "invalid field name".

    How would I obtain a record set that returns just the first occurrence of the ItemNo field?

    Thanks,

    pat

  • Quick suggestion, add another CTC and filter the output from there.

    😎

  • mpdillon (6/9/2015)


    Here is a CTE query

    With mstrTable(ItemNo, Sales)

    as (

    Query1

    Union All

    Query2

    )

    Select Row_Number () Over(Partition by ItemNo order by ItemNo)as RowNo, ItemNo, Sales

    From mstrTable

    order by ItemNo

    The results from Query1 and Query2 overlap sometimes.

    The result set looks like:

    1 Item1 10000

    2 Item1 10000

    1 Item2 20000

    1 Item3 30000

    I only want the first occurrence of each item. The desired result set is:

    1 Item1 10000

    1 Item2 20000

    1 Item3 30000

    I am not able to add a "Where RowNo = 1" to the query. SQL returns an "invalid field name".

    How would I obtain a record set that returns just the first occurrence of the ItemNo field?

    Thanks,

    pat

    Have you tried just using UNION rather than UNION ALL? Using UNION on its own doesn't include duplicate rows. You also can't use the ROW_NUMBER in the where clause of the query where it is created. If you put

    Select Row_Number () Over(Partition by ItemNo order by ItemNo)as RowNo, ItemNo, Sales

    From mstrTable

    order by ItemNo

    in another CTE then select from that where RowNo = 1 it should work.


    On two occasions I have been asked, "Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?" ... I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.
    β€”Charles Babbage, Passages from the Life of a Philosopher

    How to post a question to get the most help http://www.sqlservercentral.com/articles/Best+Practices/61537

  • Thanks for your replies. I should have thought about the UNION. I will try that now.

    I do not understand how to create a CTE from a CTE. Could you reference a web page with an example or modify my code briefly.

    Thank you,

    pat

  • Couldn't find a great link but I've heard it referred to as "chained CTEs" before

    ;With mstrTable(ItemNo, Sales)

    as (

    Query1

    Union All

    Query2

    )

    , mstrTable_2 (RowNo, ItemNo, Sales )

    AS (

    Select Row_Number () Over(Partition by ItemNo order by ItemNo)as RowNo, ItemNo, Sales

    From mstrTable

    )

    SELECT*

    FROMmstrTable_2

    WHERERowNo = 1

    order by ItemNo;

    _____________________________________________________________________
    - Nate

    @nate_hughes
  • mpdillon (6/9/2015)


    Thanks for your replies. I should have thought about the UNION. I will try that now.

    I do not understand how to create a CTE from a CTE. Could you reference a web page with an example or modify my code briefly.

    Thank you,

    pat

    Quick example

    😎

    With mstrTable(ItemNo, Sales)

    as (

    Query1

    Union All

    Query2

    )

    ,CTE2 AS

    (

    Select Row_Number () Over(Partition by ItemNo order by ItemNo)as RowNo, ItemNo, Sales

    From mstrTable

    )

    SELECT

    *

    FROM CTE2

    WHERE RowNo = 1

    order by ItemNo

  • Both solutions worked. The Union only worked fine. So did the double CTE. I didn't know that (a second CTE) could be done. The key seems to be the comma. Thank you for demonstrating that for me.

    pat

  • mpdillon (6/9/2015)


    Both solutions worked. The Union only worked fine. So did the double CTE. I didn't know that (a second CTE) could be done. The key seems to be the comma. Thank you for demonstrating that for me.

    pat

    No worries and glad to help. The rule with the CTE's is that it has to be an unbroken chain, separated by a comma with no other statements allowed in between.

    😎

Viewing 8 posts - 1 through 7 (of 7 total)

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