Distinct / Order By / Group By??

Last post 07-29-2009 3:01 AM by Madhivanan. 7 replies.
Page 1 of 1 (8 items)
Active Topics My Discussions Unanswered Sort Posts: Previous Next
  • 07-01-2009 5:17 PM

    Distinct / Order By / Group By??

    I have a bunch of PhoneDeals in my vDeals view...

    when i query this, i do something similar to:

    select * from vDeals where HandsetID = '1234'

    This returns a load of results.
    A lot of them, have the same tariffID, but a different phonePrice.
    what i want to do, is select only ONE row for each tariffID, ordered by the lowest price of the phone...

    For example:

    DealID  |  TariffID  |  HandsetID  |  PhonePrice |

    1  |  t1  |  1234  |  12.99
    2  |  t1  |  1234  |  102.44
    3  |  t1  |  1234  |  139.45
    4  |  t2  |  1234  |  23.52
    5  |  t2  |  1234  |  123.52
    6  |  t2  |  1234  |  2.52
    7  |  t3  |  1234  |  23.52




    So from the data above, the returned results should be:

    1  |  t1  |  1234  |  12.99
    6  |  t2  |  1234  |  2.52
    7  |  t3  |  1234  |  23.52

     
    how can i do that?

    Filed under:
  • 07-01-2009 8:32 PM In reply to

    Re: Distinct / Order By / Group By??

     how about this:

     select dealid, tariffid, handsetid, min(phoneprice) from vDeals where handsetid = '1234' group by dealid, tariffid, handsetid

     

  • 07-02-2009 1:17 AM In reply to

    • Mangal
    • Not Ranked
    • Joined on 07-02-2009
    • India
    • Posts 1

    Re: Distinct / Order By / Group By??

    Try this

     

    WITH CTE AS

    (

    select dealid,

    tariffid,

    handsetid,

    phoneprice,

    ROW_NUMBER()OVER(PARTITION BY tariffid ORDER BY phoneprice ASC) AS seq

    from vDeals

    where handsetid = '1234'

    )

    SELECT dealid,

    tariffid,

    handsetid,

    phoneprice

    FROM CTE

    WHERE seq=1

  • 07-15-2009 8:25 AM In reply to

    Re: Distinct / Order By / Group By??

    alexjamesbrown:

    For example:

    DealID  |  TariffID  |  HandsetID  |  PhonePrice |

    1  |  t1  |  1234  |  12.99
    2  |  t1  |  1234  |  102.44
    3  |  t1  |  1234  |  139.45
    4  |  t2  |  1234  |  23.52
    5  |  t2  |  1234  |  123.52
    6  |  t2  |  1234  |  2.52
    7  |  t3  |  1234  |  23.52


    You can try any of the below

    select min(DealID), TariffID, HandsetID, min(PhonePrice)
    from test_table
    group by TariffID, HandsetID

    or

    select max(DealID), TariffID, HandsetID, min(PhonePrice)
    from test_table
    group by TariffID, HandsetID

     

    Ram
    MCDBA
    View Rohit Paliwal's profile on LinkedIn
  • 07-15-2009 9:27 AM In reply to

    Re: Distinct / Order By / Group By??

    SELECT vD.*
    FROM  vDeals vD join (
    SELECT TariffID, MIN(PhonePrice) AS minPrice FROM  vDeals GROUP BY TariffID) as PminPrice
    on vD.TariffID = PminPrice.TariffID and
    vD. PhonePrice= PminPrice.minPrice
    where vD.HandSetID = '1234'

  • 07-28-2009 6:24 AM In reply to

    Re: Distinct / Order By / Group By??

    Hi,

    Pls try the below query...hope it will be useful for you to display the record which has minimum phone price based on Trafficid and display the phone price from the lowest value..

    Select b.dealid,a.trafficid,b.Handsetid,a.phoneprice from tr b, (Select trafficid ,min(phoneprice)as Phoneprice from tr group by trafficid,handsetid)a where a.trafficid = b.trafficid and a.phoneprice = b.phoneprice order by b.phoneprice asc

    Thanks & Regards

    Sakthimeenakshi.S
  • 07-29-2009 3:00 AM In reply to

    Re: Distinct / Order By / Group By??

    Madhivanan

    Failing to plan is Planning to fail
  • 07-29-2009 3:01 AM In reply to

    Re: Distinct / Order By / Group By??

    sakthi.tnj:
    Hi,

    Pls try the below query...hope it will be useful for you to display the record which has minimum phone price based on Trafficid and display the phone price from the lowest value..

    Select b.dealid,a.trafficid,b.Handsetid,a.phoneprice from tr b, (Select trafficid ,min(phoneprice)as Phoneprice from tr group by trafficid,handsetid)a where a.trafficid = b.trafficid and a.phoneprice = b.phoneprice order by b.phoneprice asc

    Thanks & Regards

    Sakthimeenakshi.S

     

    I think you can avoid subquery in this case

    Madhivanan

    Failing to plan is Planning to fail
Page 1 of 1 (8 items)
Active Topics   My Discussions    Unanswered Posts


© 2010 Jude O'Kelly All Rights Reserved.