Help with query

  • Yes, correct also I tried to order by item now is correct! But if you compare the first result set with the last one no the same results.

    ============================================================
    SELECT YOUR PROBLEM FROM SSC.com WHERE PROBLEM DESCRIPTION =
    http://www.sqlservercentral.com/articles/Best+Practices/61537/[/url]

  • That works, thanks Pyay!

  • _simon_, try the script in my last post, it should only return 6 rows, rather than 7 rows..

  • Hey,

    please try this. the union is used to get the last record which wll not be returned in the first query

    select a.* from

    (select ROW_NUMBER() OVER (ORDER BY item,date) as UID,* from #temp ) a

    inner join

    (select ROW_NUMBER() OVER (ORDER BY item,date) as UID,* from #temp) b

    on (a.UID+1=b.UID and (a.itemb.item or a.price1b.price1 or a.price2b.price2))

    union select * from (select top 1 * from

    (select ROW_NUMBER() OVER (ORDER BY item,date) as UID,* from #temp ) a

    order by UID desc) b

  • Both scripts are OK, thank you very much 🙂

  • thetodaisies (6/18/2009)


    Hey,

    please try this. the union is used to get the last record which wll not be returned in the first query

    select a.* from

    (select ROW_NUMBER() OVER (ORDER BY item,date) as UID,* from #temp ) a

    inner join

    (select ROW_NUMBER() OVER (ORDER BY item,date) as UID,* from #temp) b

    on (a.UID+1=b.UID and (a.itemb.item or a.price1b.price1 or a.price2b.price2))

    union select * from (select top 1 * from

    (select ROW_NUMBER() OVER (ORDER BY item,date) as UID,* from #temp ) a

    order by UID desc) b

    Now, seems everything ok... still a I don't know exactly ..._simone_ will tell us for the correct answer, but as I can see in your code here seems very compact solution!

    Nice code thetodaisies! 😉

    ============================================================
    SELECT YOUR PROBLEM FROM SSC.com WHERE PROBLEM DESCRIPTION =
    http://www.sqlservercentral.com/articles/Best+Practices/61537/[/url]

  • _simon_ (6/18/2009)


    Both scripts are OK, thank you very much 🙂

    yes, only on the assumption that no price changes will be made on the same day, otherwise both scripts will break 😀

  • thetodaisies (6/18/2009)


    Hey,

    please try this. the union is used to get the last record which wll not be returned in the first query

    select a.* from

    (select ROW_NUMBER() OVER (ORDER BY item,date) as UID,* from #temp ) a

    inner join

    (select ROW_NUMBER() OVER (ORDER BY item,date) as UID,* from #temp) b

    on (a.UID+1=b.UID and (a.itemb.item or a.price1b.price1 or a.price2b.price2))

    union select * from (select top 1 * from

    (select ROW_NUMBER() OVER (ORDER BY item,date) as UID,* from #temp ) a

    order by UID desc) b

    I would have done it similarily:

    SELECT A.item, A.price1, A.price2, A.date

    FROM (SELECT ROW_NUMBER() OVER (ORDER BY item, date) AS UID, * FROM #temp) A

    LEFT JOIN (SELECT ROW_NUMBER() OVER (ORDER BY item, date) AS UID, * FROM #temp) B

    ON A.UID+1=B.UID AND A.item=B.item AND A.price1=B.price1 AND A.price2=B.price2

    WHERE B.item IS NULL

    What we are looking for are rows to which there is no sucessor (next date for same item) with the same price. So we look for rows where the JOIN with this criteria (next date for the same item with the same price) returns no rows. This is the case when B.item IS NULL.

    Therefore we simply filter these using "B.item IS NULL" in the WHERE clause.

    Best Regards,

    Chris Büttner

  • Yep nice solution here without UNION! Nice analyzing Chris!

    ============================================================
    SELECT YOUR PROBLEM FROM SSC.com WHERE PROBLEM DESCRIPTION =
    http://www.sqlservercentral.com/articles/Best+Practices/61537/[/url]

  • Yeah, nice one, Chris.

  • Pyay Nyein (6/18/2009)


    Yeah, nice one, Chris.

    Well, you guys did the hard part 🙂

    Best Regards,

    Chris Büttner

Viewing 11 posts - 16 through 25 (of 25 total)

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