Results 1 to 3 of 3

Thread: Query help

  1. #1
    Join Date
    Nov 2006
    Posts
    20

    Unanswered: Query help

    Hey

    I have a simple
    "CD" Table (ID, CD_Name, Cost)

    it consists of 6 different cds.

    I am trying to write a query that lists the name and cost of each cd. and i wish to add a column that compares the cost to the average cost. i.e difference between cost and AVG cost of all cds.

    currently i have.
    Code:
    select cd_name, cost, avg(cost) - cost as AVG_Difference
    from cd
    group by cost, cd_name
    but no luck

    any guidance would be appreciated

  2. #2
    Join Date
    Jan 2004
    Location
    Croatia, Europe
    Posts
    4,094
    Provided Answers: 4
    You might try with something like this:
    Code:
    SELECT x.cd_name, x.cost, x.avg_cost - x.cost avg_difference
    FROM (SELECT cd_name, cost, (SELECT AVG(cost) FROM CD) avg_cost
          FROM CD
         ) x;

  3. #3
    Join Date
    Mar 2007
    Posts
    37
    yeah, the above query works well...
    an easier way is-
    Code:
    SELECT cd_name, cost, (SELECT AVG(cost) FROM cd)-cost AS AVG_difference 
    FROM cd 
    GROUP BY cost,cd_name;
    this will work too...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •