MS SQL Aggregate Function

  • Hello,

    I have a query to select title_id, type, price and average of prices by type from titles in the pubs database. The goal is show all the fields plus the avg(price). I tried with group by clause and not able to get desired result. Could some one help me please?

    Thanks

    beginner-sql :w00t:

  • Please post what you tried so we can see things like what you tried to GROUP BY. Thanks.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Hello there,

    This is the Task:

    Select the title_id, type, and price from the titles table along with an average price for all tables

  • kmohan39 (11/19/2008)


    Hello there,

    This is the Task:

    Select the title_id, type, and price from the titles table along with an average price for all tables

    Not what I asked for... I want to see the code that you've actually tried... not pseudo code.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • select title_id, type, price, avg(price)

    from titles

    group by title_id, type, price

    order by type

  • SELECT t.Title_ID, t.Type, t.Price, d.AvgPrice

    FROM dbo.Titles t

    INNER JOIN

    (--==== Derived table "d" finds average Price for each Type

    SELECT Type, AVG(Price) AS AvgPrice

    FROM dbo.Titles

    GROUP BY Type) d

    ON t.Type = d.Type

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Thank you Jeff Moden

    newbie

    😎

  • You bet. Thank you for the feedback.

    Now you know why I asked to see your query. For more information on how to get better answers quicker on future posts, take a look at the link in my signature line.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

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

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