Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Using avg function for new constructed column?

Using avg function for new constructed column?

Scheduled Pinned Locked Moved C#
salesquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    omegazafer
    wrote on last edited by
    #1

    Hi friends, I have a two columns as unitprice and quantity in salesreport table. I am constructing new column by multplying this two column. I gave the name of sales for this new column. I want to ge average of sales column. I tried below code but program says the column name of sales is invalid. select unitprice,quantity,unitprice*quantitiy as sales from salesreport where sales>avg(sales)

    J 1 Reply Last reply
    0
    • O omegazafer

      Hi friends, I have a two columns as unitprice and quantity in salesreport table. I am constructing new column by multplying this two column. I gave the name of sales for this new column. I want to ge average of sales column. I tried below code but program says the column name of sales is invalid. select unitprice,quantity,unitprice*quantitiy as sales from salesreport where sales>avg(sales)

      J Offline
      J Offline
      J 0
      wrote on last edited by
      #2

      I think you have to change the structure of your query a bit in order to get the results you would like. First off, Sales is a calculated column, so if you would like to filter by that value, you will have to filter using the calculation instead of the alias you give to that calculation. Secondly, to get the average sales, you'll have to include a sub-query in your where clause that will calculate the average. This subquery will then be used to compare values against to determine if it meets the criteria in your where clause Your query should look similar to: select   unitprice,   quantity,   unitprice * quantitiy as sales from   salesreport where   **(unitprice * quantitiy)** > **(select avg(unitprice * quantitiy) from salesreport)**

      O 2 Replies Last reply
      0
      • J J 0

        I think you have to change the structure of your query a bit in order to get the results you would like. First off, Sales is a calculated column, so if you would like to filter by that value, you will have to filter using the calculation instead of the alias you give to that calculation. Secondly, to get the average sales, you'll have to include a sub-query in your where clause that will calculate the average. This subquery will then be used to compare values against to determine if it meets the criteria in your where clause Your query should look similar to: select   unitprice,   quantity,   unitprice * quantitiy as sales from   salesreport where   **(unitprice * quantitiy)** > **(select avg(unitprice * quantitiy) from salesreport)**

        O Offline
        O Offline
        omegazafer
        wrote on last edited by
        #3

        Thanks so much for your quick and detailed response but i have another problem. First of all i wonder why we can't write avg(sales) instead of avg(unitprice * quantitiy). I am working with nortwind sample. I have the column sum("order details".unitprice*quantity*(1-discount)) as totalsales (I don't write all all code, if it is not clear what I want , i can write all my code trying.) as accumulated new column. I want to get average of this.and I used group by function also. I wrote avg(sum("order details".unitprice*quantity*(1-discount)) ) .Program says you can not sum in....)

        1 Reply Last reply
        0
        • J J 0

          I think you have to change the structure of your query a bit in order to get the results you would like. First off, Sales is a calculated column, so if you would like to filter by that value, you will have to filter using the calculation instead of the alias you give to that calculation. Secondly, to get the average sales, you'll have to include a sub-query in your where clause that will calculate the average. This subquery will then be used to compare values against to determine if it meets the criteria in your where clause Your query should look similar to: select   unitprice,   quantity,   unitprice * quantitiy as sales from   salesreport where   **(unitprice * quantitiy)** > **(select avg(unitprice * quantitiy) from salesreport)**

          O Offline
          O Offline
          omegazafer
          wrote on last edited by
          #4

          I used below code series and I got "An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference." error. select employees.Firstname,employees.lastname,orders.OrderID, sum("order details".unitprice*quantity*(1-discount)) as Sales from (Employees inner join orders on employees.employeeID=orders.employeeID) inner join "order details" on orders.orderID="order details".orderID where sum("order details".unitprice*quantity*(1-discount))>(select avg(sum("order details".unitprice*quantity*(1-discount))) from "order details") group by employees.lastname,orders.orderID,employees.firstname

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups