Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7183239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:56:20+00:00 2026-05-28T17:56:20+00:00

I structured my sql table like this: ItemID Price MaxPeople CalculationUnit 1 10 4

  • 0

I structured my sql table like this:

ItemID   Price   MaxPeople   CalculationUnit
1        10      4           people/item
2        70      2           item
3        30      8           week/item
4        50      2           week

etc.

Now I would like to run a basic stored procedure that looks something like

sp_return_items_total
@Days as int,
@Items as int
AS
select itemid, price, total from table

Total would be a value based on this calculation (number of days * calculation unit * price).

For example for @Days = 5 and ‘@Items = 2’ results would be:

1, 10, 400  (10 * 4 people * 2 items * 5 days)
2, 70, 140 (70 * 1 * 2 items * 1)
3, 30, 42.85 (30 * 1 * 2 items * 5/7 days)
4, 50, 35.71 (50 * 1 * 1 items * 5/7 days)

I am trying to find a solution, how to get the total value based on the sp parameters and calculation unit.

Thanks for participating

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T17:56:21+00:00Added an answer on May 28, 2026 at 5:56 pm

    EDIT 2/10/12: Revised query to correspond to revised sample output:

    -- Set up the test data. 
    declare @AmalgamatedStuff as table ( ItemId int, Price int, MaxPeople int, CalculationUnit varchar(16) ) 
    insert into @AmalgamatedStuff ( ItemId, Price, MaxPeople, CalculationUnit ) values 
      ( 1, 10, 4, 'people/item' ), 
      ( 2, 70, 2, 'item' ), 
      ( 3, 30, 8, 'week/item' ), 
      ( 4, 50, 2, 'week' ) 
    
    -- Stored procedure parameters. 
    declare @Days as int = 5 
    declare @Items as int = 2 
    
    -- The query. 
    select ItemId, Price, 
      case CalculationUnit 
        when 'item' then Price * @Items
        when 'people/item' then Price * MaxPeople * @Items * @Days 
        when 'week' then round( Price * @Days / 7.0, 2 )
        when 'week/item' then round( Price * @Items * @Days / 7.0, 2 )
        else NULL 
        end as Total 
      from @AmalgamatedStuff
    

    Note that 42.857142 fails to round to 42.85 in the third result row.


    EDIT: Bearing in mind that the suggested results with a variable number of columns and unspecified calculations cannot be obtained:

    -- Set up the test data.
    declare @AmalgamatedStuff as table ( ItemId int, Price int, MaxPeople int, CalculationUnit varchar(16) )
    insert into @AmalgamatedStuff ( ItemId, Price, MaxPeople, CalculationUnit ) values
      ( 1, 10, 4, 'people/item' ),
      ( 2, 70, 2, 'item' ),
      ( 3, 30, 8, 'week/item' ),
      ( 4, 50, 2, 'week' )
    
    -- Stored procedure parameters.
    declare @Days as int = 5
    declare @Items as int = 2
    
    -- The query, give or take the correct calculations.
    declare @SpuriousFactorToGetSuggestedResult as int = 2  
    select ItemId, Price,
      case CalculationUnit
        when 'item' then Price * @Items
        when 'people/item' then Price * MaxPeople * @Items * @Days
        when 'week' then Price * @Items * @Days / 7
        when 'week/item' then Price * @Items * @Days * @SpuriousFactorToGetSuggestedResult / 7
        else NULL
        end as Total
      from @AmalgamatedStuff
    

    Actually getting the query into a stored procedure is left as an exercise for the OP.

    The “design” remains somewhere below rancid, and festering ever faster.


    EDIT: Answer to an earlier edit of the “question” remains below:

    You can do things using a CASE like:

    select ItemId, Price,
      case
        when CalculationUnit = 'day' then @Days * Price
        when CalculationUnit = 'week' then @Days / 7 * Price
        else NULL
        end as 'Total'
      from MyIllConceivedTable
    

    As previously noted, it’s a bad design.

    In some cases it might make sense to have a lookup table, e.g. something that lets you map various units of measure to some common base. Think weights and their gram equivalents. (Also a handy place for storing the full name “Ounces” and abbreviation “Oz”, … .) Your data table would contain references to the units table.

    In some cases it may make sense with units of time. Scheduled events might recur daily, weekly, monthly, quarterly and annually. The lengths of the units are somewhat flexible, and the uses tend to be peculiar. (I have a lunch on the 3rd Wednesday of each month. See you there?)

    Regarding performance, calculations that return results aren’t bad. You could use a computed column or a view to achieve your (nefarious) end. Performance takes a hit when you have functions called for each row, e.g. a WHERE clause that converts a DATETIME column to a string and uses LIKE to determine if there is an ‘R’ in the string.

    Whatever you choose, please don’t use anything as daffy as:

    declare @Today as Date
    set @Today = SysDateTime()
    select @Today,
      DateDiff(day, @Today, DateAdd( "day", 1, @Today ) ) as 'Days in a Day',
      DateDiff(day, @Today, DateAdd( "week", 1, @Today ) ) as 'Days in a Week',
      DateDiff(day, @Today, DateAdd( "month", 1, @Today ) ) as 'Days in a Month' -- Sometimes!
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table in SQL Server that is structured like this: id Name
I have a table structured like this: <table> <tr id=id1 class=parent></tr> <tr class=id1></tr> <tr
I have some nicely-structured data that looks like this: CREATE TABLE SourceBodyPartColors ( person_ID
My table structure (in SQL Server) looks something like this: (D1 is more recent
I have a database table structured like this (irrelevant fields omitted for brevity): rankings
I got a really simple table structure like this: Table Page Hits id |
I have a SQL database with two tables like this: Users Id (PK) Name
My table structure looks like this: tbl.users tbl.issues +--------+-----------+ +---------+------------+-----------+ | userid | real_name
I have a table with around 15 columns. What I would like to be
I have questions which are represented like this in my SQL database : CREATE

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.