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. Calculations

Calculations

Scheduled Pinned Locked Moved Database
helpdatabasequestionannouncement
4 Posts 3 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.
  • T Offline
    T Offline
    Terick
    wrote on last edited by
    #1

    Hi, I need to create a table in SQL that will compute costs. I have to read in some information from two other tables and use this to populate the new table from which the costs are calculated. As the data is from a website, it is possible that it will change and I need the costs to update if the data changes. Do I need to create a function to do this, or will the table do it on its own? ( I elected to do a function, but if there is a better way I am open to that) This is what I have so far:

    set ANSI_NULLS ON
    go
    set QUOTED_IDENTIFIER ON
    go

    CREATE FUNCTION [dbo].[fcn_computecosts]
    ( @ProjectID int, @userID int, @startDate datetime, @enddate datetime, @quarter tinyint,
    @Cost money, @percent int, @totalDays int, @totalcost)

    RETURNS @ComputeCosts TABLE
    ( ProjectID int, userID int, startDate datetime, enddate datetime, quarter tinyint,
    Cost money, percent int, totalDays int, totalcost)

    AS
    BEGIN
    SELECT
    P.ProjectID, P.userID, P.startDate, P.enddate, C.quarter,P.Cost, P.percent,
    COUNT(C.date) AS totalDays, ((percent/100)*totaldays*Cost*8) AS totalcost

    FROM Project AS P INNER JOIN Calendar AS C ON Calendar.date >P.startDate AND Calendar.date <=
    P.enddate
    WHERE (C.isWeekday =1) AND (C.isHoliday=0)
    GROUP BY P.ProjectID, P.userID, P.startDate, P.enddate, C.quarter,P.Cost, P.percent,
    totalDays, totalcost
    ORDER BY P.ProjectID
    RETURN
    END

    However when I run this I am getting the following error: "msg 207, Level 16, state 1: Invalid column name 'totalDays', 'totalcost', 'Cost' Please help!

    D L 3 Replies Last reply
    0
    • T Terick

      Hi, I need to create a table in SQL that will compute costs. I have to read in some information from two other tables and use this to populate the new table from which the costs are calculated. As the data is from a website, it is possible that it will change and I need the costs to update if the data changes. Do I need to create a function to do this, or will the table do it on its own? ( I elected to do a function, but if there is a better way I am open to that) This is what I have so far:

      set ANSI_NULLS ON
      go
      set QUOTED_IDENTIFIER ON
      go

      CREATE FUNCTION [dbo].[fcn_computecosts]
      ( @ProjectID int, @userID int, @startDate datetime, @enddate datetime, @quarter tinyint,
      @Cost money, @percent int, @totalDays int, @totalcost)

      RETURNS @ComputeCosts TABLE
      ( ProjectID int, userID int, startDate datetime, enddate datetime, quarter tinyint,
      Cost money, percent int, totalDays int, totalcost)

      AS
      BEGIN
      SELECT
      P.ProjectID, P.userID, P.startDate, P.enddate, C.quarter,P.Cost, P.percent,
      COUNT(C.date) AS totalDays, ((percent/100)*totaldays*Cost*8) AS totalcost

      FROM Project AS P INNER JOIN Calendar AS C ON Calendar.date >P.startDate AND Calendar.date <=
      P.enddate
      WHERE (C.isWeekday =1) AND (C.isHoliday=0)
      GROUP BY P.ProjectID, P.userID, P.startDate, P.enddate, C.quarter,P.Cost, P.percent,
      totalDays, totalcost
      ORDER BY P.ProjectID
      RETURN
      END

      However when I run this I am getting the following error: "msg 207, Level 16, state 1: Invalid column name 'totalDays', 'totalcost', 'Cost' Please help!

      D Offline
      D Offline
      David Mujica
      wrote on last edited by
      #2

      Isn't @totalcost missing the datatype declaration ? CREATE FUNCTION [dbo].[fcn_computecosts]( @ProjectID int, @userID int, @startDate datetime, @enddate datetime, @quarter tinyint, @Cost money, @percent int, @totalDays int, @totalcost)

      1 Reply Last reply
      0
      • T Terick

        Hi, I need to create a table in SQL that will compute costs. I have to read in some information from two other tables and use this to populate the new table from which the costs are calculated. As the data is from a website, it is possible that it will change and I need the costs to update if the data changes. Do I need to create a function to do this, or will the table do it on its own? ( I elected to do a function, but if there is a better way I am open to that) This is what I have so far:

        set ANSI_NULLS ON
        go
        set QUOTED_IDENTIFIER ON
        go

        CREATE FUNCTION [dbo].[fcn_computecosts]
        ( @ProjectID int, @userID int, @startDate datetime, @enddate datetime, @quarter tinyint,
        @Cost money, @percent int, @totalDays int, @totalcost)

        RETURNS @ComputeCosts TABLE
        ( ProjectID int, userID int, startDate datetime, enddate datetime, quarter tinyint,
        Cost money, percent int, totalDays int, totalcost)

        AS
        BEGIN
        SELECT
        P.ProjectID, P.userID, P.startDate, P.enddate, C.quarter,P.Cost, P.percent,
        COUNT(C.date) AS totalDays, ((percent/100)*totaldays*Cost*8) AS totalcost

        FROM Project AS P INNER JOIN Calendar AS C ON Calendar.date >P.startDate AND Calendar.date <=
        P.enddate
        WHERE (C.isWeekday =1) AND (C.isHoliday=0)
        GROUP BY P.ProjectID, P.userID, P.startDate, P.enddate, C.quarter,P.Cost, P.percent,
        totalDays, totalcost
        ORDER BY P.ProjectID
        RETURN
        END

        However when I run this I am getting the following error: "msg 207, Level 16, state 1: Invalid column name 'totalDays', 'totalcost', 'Cost' Please help!

        D Offline
        D Offline
        David Mujica
        wrote on last edited by
        #3

        In your group by clause, you are trying to group by totalDays and totalcost which are aliases to computed columns. I've seen it where you have to repeat the function again in the group by clause instead of referencing the alias name. Something like below. GROUP BY P.ProjectID, P.userID, P.startDate, P.enddate, C.quarter,P.Cost, P.percent, COUNT(C.date), ((percent/100)*totaldays*Cost*8) Do you really need to group by these values ?

        1 Reply Last reply
        0
        • T Terick

          Hi, I need to create a table in SQL that will compute costs. I have to read in some information from two other tables and use this to populate the new table from which the costs are calculated. As the data is from a website, it is possible that it will change and I need the costs to update if the data changes. Do I need to create a function to do this, or will the table do it on its own? ( I elected to do a function, but if there is a better way I am open to that) This is what I have so far:

          set ANSI_NULLS ON
          go
          set QUOTED_IDENTIFIER ON
          go

          CREATE FUNCTION [dbo].[fcn_computecosts]
          ( @ProjectID int, @userID int, @startDate datetime, @enddate datetime, @quarter tinyint,
          @Cost money, @percent int, @totalDays int, @totalcost)

          RETURNS @ComputeCosts TABLE
          ( ProjectID int, userID int, startDate datetime, enddate datetime, quarter tinyint,
          Cost money, percent int, totalDays int, totalcost)

          AS
          BEGIN
          SELECT
          P.ProjectID, P.userID, P.startDate, P.enddate, C.quarter,P.Cost, P.percent,
          COUNT(C.date) AS totalDays, ((percent/100)*totaldays*Cost*8) AS totalcost

          FROM Project AS P INNER JOIN Calendar AS C ON Calendar.date >P.startDate AND Calendar.date <=
          P.enddate
          WHERE (C.isWeekday =1) AND (C.isHoliday=0)
          GROUP BY P.ProjectID, P.userID, P.startDate, P.enddate, C.quarter,P.Cost, P.percent,
          totalDays, totalcost
          ORDER BY P.ProjectID
          RETURN
          END

          However when I run this I am getting the following error: "msg 207, Level 16, state 1: Invalid column name 'totalDays', 'totalcost', 'Cost' Please help!

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          You Not Grouping Properly SELECT P.ProjectID, P.userID, P.startDate, P.enddate, C.quarter,P.Cost, P.percent, COUNT(C.date) AS totalDays, ((percent/100)*COUNT(C.date)*Cost*8) AS totalcost FROM Project AS P INNER JOIN Calendar AS C ON Calendar.date >P.startDate AND Calendar.date <= P.enddate WHERE (C.isWeekday =1) AND (C.isHoliday=0) GROUP BY P.ProjectID, P.userID, P.startDate, P.enddate, C.quarter,P.Cost, P.percent ORDER BY P.ProjectID You Have No Need To It totalDays,totalCost in Group By

          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