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. Database & SysAdmin
  3. Database
  4. Problem in Query

Problem in Query

Scheduled Pinned Locked Moved Database
helpdatabasequestion
5 Posts 4 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.
  • N Offline
    N Offline
    nagendrathecoder
    wrote on last edited by
    #1

    Hello all, I am trying to execute following query but getting an error as amount us not a valid column:

    select itemname,rate,qty,discount,(rate * qty) as amount,(amount -(amount * (disount/100))) as Total from ItemMaster;

    Then i try:

    select itemname,rate,qty,discount,(rate * qty) as amount,((rate * qty) - ((rate * qty) * (disount/100))) as Total from ItemMaster;

    and it worked fine. I need to execute queries which involves more complex expressions within them. My doubt is, is there any way to run query by first method so that we don't need to write complex expressions again and again? Thanks, Nagendra.

    I M 2 Replies Last reply
    0
    • N nagendrathecoder

      Hello all, I am trying to execute following query but getting an error as amount us not a valid column:

      select itemname,rate,qty,discount,(rate * qty) as amount,(amount -(amount * (disount/100))) as Total from ItemMaster;

      Then i try:

      select itemname,rate,qty,discount,(rate * qty) as amount,((rate * qty) - ((rate * qty) * (disount/100))) as Total from ItemMaster;

      and it worked fine. I need to execute queries which involves more complex expressions within them. My doubt is, is there any way to run query by first method so that we don't need to write complex expressions again and again? Thanks, Nagendra.

      I Offline
      I Offline
      i i i
      wrote on last edited by
      #2

      try making Sql Server UDF (User Defined Functions)

      Best Of Regards, SOFTDEV If you have knowledge, let others light their candles at it

      1 Reply Last reply
      0
      • N nagendrathecoder

        Hello all, I am trying to execute following query but getting an error as amount us not a valid column:

        select itemname,rate,qty,discount,(rate * qty) as amount,(amount -(amount * (disount/100))) as Total from ItemMaster;

        Then i try:

        select itemname,rate,qty,discount,(rate * qty) as amount,((rate * qty) - ((rate * qty) * (disount/100))) as Total from ItemMaster;

        and it worked fine. I need to execute queries which involves more complex expressions within them. My doubt is, is there any way to run query by first method so that we don't need to write complex expressions again and again? Thanks, Nagendra.

        M Offline
        M Offline
        Mycroft Holmes
        wrote on last edited by
        #3

        There are a number of methods to achieve a semblance of this, UDF is one, in the above simplistic sample on you could create a view that calcs the amount column, you could create a table variable to hold the equivalent of the view, this would only be valid for the current proc whereas a view persists to the database. I am in the habit of creating a view for my transaction tables, creating any calculated columns (amount) and joining to the foreign key tables (an example would be an OrderLine to Product table on the ProductID to get the product name in the view).

        I A 2 Replies Last reply
        0
        • M Mycroft Holmes

          There are a number of methods to achieve a semblance of this, UDF is one, in the above simplistic sample on you could create a view that calcs the amount column, you could create a table variable to hold the equivalent of the view, this would only be valid for the current proc whereas a view persists to the database. I am in the habit of creating a view for my transaction tables, creating any calculated columns (amount) and joining to the foreign key tables (an example would be an OrderLine to Product table on the ProductID to get the product name in the view).

          I Offline
          I Offline
          i i i
          wrote on last edited by
          #4

          I agree with you View will be a good option

          Best Of Regards, SOFTDEV If you have knowledge, let others light their candles at it

          1 Reply Last reply
          0
          • M Mycroft Holmes

            There are a number of methods to achieve a semblance of this, UDF is one, in the above simplistic sample on you could create a view that calcs the amount column, you could create a table variable to hold the equivalent of the view, this would only be valid for the current proc whereas a view persists to the database. I am in the habit of creating a view for my transaction tables, creating any calculated columns (amount) and joining to the foreign key tables (an example would be an OrderLine to Product table on the ProductID to get the product name in the view).

            A Offline
            A Offline
            Ashfield
            wrote on last edited by
            #5

            I would go with the view approach, a UDF can add a large overhead to a simple query, slowing it right down - much more than you would expect.

            Bob Ashfield Consultants Ltd Proud to be a 2009 Code Project MVP

            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