Optimizing the data

  • Dear Alll,

    I have one query, can anybody help in this..

    I have one table and i need to get records from table on below mentioned cases.

    My table is like :

    No_PriorityItem Store

    124434891000012024

    124434891000022024

    124534901000012024

    124534901000022024

    124534901000032024

    Now i want data like :

    If i have multiple Items for same Store then the item whose adjacent priority is lower i should select only that record.

    Result should look like this :

    No_PriorityItem Store

    124434891000012024

    124434891000022024

    124534901000032024

    please help i am not able to get this.

    Regards,

    Deepika

  • Can anybody help in this..???

  • do you want to show one data irrespective of different priority right?

    Regards
    Durai Nagarajan

  • Hi,

    I want to select those items whose priority is low in a combinatino of item and Store.

    and if only one item present in the same store then that record should also fetch

  • please help in this

  • give us some time some body will help

    Regards
    Durai Nagarajan

  • I need it urgently..

    i will be glad if somebody could be helpful....

  • can you push this data to a temp table

    select min(Priority) 'Priority', Item, Store

    from tableName

    group by Item, Store

    and then join with main table to get No_ column

    old idea but i cant think of better now.

    Regards
    Durai Nagarajan

  • Hi,

    This i have already tried.

    But after moving to temp table, i again have to join with my master table for the required fields.

    Now challenge come when i have single Item present in store, in this case group by Store and Item will not give my required output.

    and Priority column i am using for my comparison, my last output will have No_, Item, Store which have single item in the store with lowest priority..

    Please help..i am not able to fetch the data.

    Thanks a lot!!!

    Regards,

    Deepika

  • [quote-0Now challenge come when i have single Item present in store, in this case group by Store and Item will not give my required output.

    [/quote-0]

    100003 is once in your example how min will not show this data by the above query , am i missing something or you are referring something.

    Regards
    Durai Nagarajan

  • Using the Windowed functions (ROW_NUMBER, RANK etc), is normally the easiest way to resolve this type of query.

    -- *** Test Data ***

    -- You should provide test data in this format so people can test it.

    CREATE TABLE #t

    (

    No_ int NOT NULL

    ,Priority int NOT NULL

    ,Item int NOT NULL

    ,Store int NOT NULL

    );

    INSERT INTO #t

    SELECT 1244, 3489, 100001, 2024

    UNION ALL SELECT 1244, 3489, 100002, 2024

    UNION ALL SELECT 1245, 3490, 100001, 2024

    UNION ALL SELECT 1245, 3490, 100002, 2024

    UNION ALL SELECT 1245, 3490, 100003, 2024;

    -- *** Test Data ***

    WITH OrderedItems

    AS

    (

    SELECT No_, Priority, Item, Store

    ,ROW_NUMBER() OVER (PARTITION BY Store, Item ORDER BY Priority) AS rn

    FROM #t

    )

    SELECT No_, Priority, Item, Store

    FROM OrderedItems

    WHERE rn = 1

  • This has to be homework because it's very simple.

    Try ROW_NUMBER() OVER (PARTITION BY something ORDER BY somethingelse)

    You will need to set it up as a CTE or derived table in order to filter by the values returned by ROW_NUMBER().

    โ€œWrite the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.โ€ - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • thanks a lot Ken!!!.

    It worked...

    Thanks once again ๐Ÿ™‚

Viewing 13 posts - 1 through 13 (of 13 total)

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